Skip to content

Commit

Permalink
Apply same changes as in PR for 6.4 (symfony/symfony#50131)
Browse files Browse the repository at this point in the history
  • Loading branch information
mikaelkael committed Apr 29, 2023
1 parent e3d939d commit aefb365
Show file tree
Hide file tree
Showing 10 changed files with 122 additions and 75 deletions.
1 change: 1 addition & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ jobs:
- '8.0'
- '8.1'
- '8.2'
symfony: ['*']
include:
- description: 'Symfony 5.4'
php: '7.4'
Expand Down
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,22 @@ Ntfy Notifier
[![Build Status](https://github.com/mikaelkael/ntfy-notifier/workflows/Build/badge.svg)](https://github.com/mikaelkael/ntfy-notifier/actions)
[![License](https://poser.pugx.org/mikaelkael/ntfy-notifier/license.png)](https://packagist.org/packages/mikaelkael/ntfy-notifier)

Provides [Ntfy](https://docs.ntfy.sh/) integration for Symfony Notifier.
Provides [Ntfy](https://docs.ntfy.sh/) integration for Symfony Notifier. The component should be introduced in Symfony 6.4 with this [PR #50131](https://github.com/symfony/symfony/pull/50131). This bundle provides same functionalities for Symfony 5.4.x to 6.3.x.

DSN example
-----------

```
# .env
NTFY_DSN=ntfy://[NTFY_USER:NTFY_PASSWORD]@NTFY_URL[:NTFY_PORT]/NTFY_TOPIC?[secureHttp=[on]]
NTFY_DSN=ntfy://[USER:PASSWORD]@default[:PORT]/TOPIC?[secureHttp=[on]]
```

where:
- `NTFY_URL` is the ntfy server which you are using
- `URL` is the ntfy server which you are using
- if `default` is provided, this will default to the public ntfy server hosted on [ntfy.sh](https://ntfy.sh/).
- `NTFY_TOPIC` is the topic on this ntfy server.
- `NTFY_PORT` is an optional specific port.
- `NTFY_USER`and `NTFY_PASSWORD` are username and password in case of access control supported by the server
- `TOPIC` is the topic on this ntfy server.
- `PORT` is an optional specific port.
- `USER`and `PASSWORD` are username and password in case of access control supported by the server

In case of a non-secure server, you can disable https by setting `secureHttp=off`.

Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
],
"require": {
"php": ">=7.4",
"ext-json": "*",
"symfony/framework-bundle": "^5.4|^6.0",
"symfony/http-client": "^5.0|^6.0",
"symfony/notifier": "^5.4|^6.0"
Expand Down
6 changes: 3 additions & 3 deletions src/DependencyInjection/MkkNtfyExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@

namespace Mkk\NtfyBundle\DependencyInjection;

use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Loader;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;

class MkkNtfyExtension extends Extension
{
/**
* {@inheritdoc}
*/
public function load(array $configs, ContainerBuilder $container)
public function load(array $configs, ContainerBuilder $container): void
{
$loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../../config'));
$loader->load('services.yml');
Expand Down
47 changes: 35 additions & 12 deletions src/Message/NtfyOptions.php
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
<?php

declare(strict_types=1);

namespace Mkk\NtfyBundle\Message;

use Symfony\Component\Notifier\Exception\LogicException;
use Symfony\Component\Notifier\Message\MessageOptionsInterface;
use Symfony\Component\Notifier\Notification\Notification;

final class NtfyOptions implements MessageOptionsInterface
{
private array $options;
const PRIORITY_MAX = 5;
const PRIORITY_URGENT = 5;
const PRIORITY_HIGH = 4;
const PRIORITY_DEFAULT = 3;
const PRIORITY_LOW = 2;
const PRIORITY_MIN = 1;
public const PRIORITY_URGENT = 5;
public const PRIORITY_HIGH = 4;
public const PRIORITY_DEFAULT = 3;
public const PRIORITY_LOW = 2;
public const PRIORITY_MIN = 1;

public function __construct(array $options = [])
{
Expand All @@ -28,6 +29,7 @@ public static function fromNotification(Notification $notification): self
$options->setMessage($notification->getContent());
$options->setStringPriority($notification->getImportance());
$options->addTag($notification->getEmoji());

return $options;
}

Expand All @@ -44,12 +46,14 @@ public function getRecipientId(): ?string
public function setMessage(string $message): self
{
$this->options['message'] = $message;

return $this;
}

public function setTitle(string $title): self
{
$this->options['title'] = $title;

return $this;
}

Expand All @@ -69,82 +73,101 @@ public function setStringPriority(string $priority): self

public function setPriority(int $priority): self
{
if (in_array($priority, [
self::PRIORITY_MIN, self::PRIORITY_LOW, self::PRIORITY_DEFAULT, self::PRIORITY_HIGH, self::PRIORITY_URGENT, self::PRIORITY_MAX
if (\in_array($priority, [
self::PRIORITY_MIN, self::PRIORITY_LOW, self::PRIORITY_DEFAULT, self::PRIORITY_HIGH, self::PRIORITY_URGENT,
])) {
$this->options['priority'] = $priority;
}

return $this;
}

public function addTag(string $tag): self
{
$this->options['tags'][] = $tag;

return $this;
}

public function setTags(array $tags): self
{
$this->options['tags'] = $tags;

return $this;
}

public function setDelay(\DateTimeInterface $dateTime): self
{
$this->options['delay'] = $dateTime->getTimestamp();
if ($dateTime > (new \DateTime())) {
$this->options['delay'] = (string) $dateTime->getTimestamp();
} else {
throw new LogicException('Delayed date must be defined in the future.');
}

return $this;
}

public function setActions(array $actions): self
{
$this->options['actions'] = $actions;

return $this;
}

public function addAction(array $action): self
{
$this->options['actions'][] = $action;

return $this;
}

public function setClick(string $url): self
{
$this->options['click'] = $url;

return $this;
}

public function setAttachment(string $attachment): self
{
$this->options['attach'] = $attachment;

return $this;
}

public function setFilename(string $filename): self
{
$this->options['filename'] = $filename;

return $this;
}

public function setEmail(string $email): self
{
$this->options['email'] = $email;

return $this;
}

public function setCache(bool $enable)
public function setCache(bool $enable): self
{
if (!$enable) {
$this->options['cache'] = 'no';
} else {
unset($this->options['cache']);
}

return $this;
}
public function setFirebase(bool $enable)

public function setFirebase(bool $enable): self
{
if (!$enable) {
$this->options['firebase'] = 'no';
} else {
unset($this->options['firebase']);
}

return $this;
}
}
}
33 changes: 13 additions & 20 deletions src/Transport/NtfyTransport.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

declare(strict_types=1);

namespace Mkk\NtfyBundle\Transport;
Expand All @@ -21,11 +22,12 @@ final class NtfyTransport extends AbstractTransport
private ?string $user = null;
private ?string $password = null;
private string $topic;
private bool $secureHttp = true;
private bool $secureHttp;

public function __construct(string $topic, HttpClientInterface $client = null, EventDispatcherInterface $dispatcher = null)
public function __construct(string $topic, bool $secureHttp = true, HttpClientInterface $client = null, EventDispatcherInterface $dispatcher = null)
{
$this->topic = $topic;
$this->secureHttp = $secureHttp;

parent::__construct($client, $dispatcher);
}
Expand All @@ -35,26 +37,17 @@ public function getTopic(): string
return $this->topic;
}

public function isSecureHttp(): bool
{
return $this->secureHttp;
}

public function setSecureHttp(bool $secureHttp): self
{
$this->secureHttp = $secureHttp;
return $this;
}

public function setPassword(?string $password): self
{
$this->password = $password;

return $this;
}

public function setUser(?string $user): self
{
$this->user = $user;

return $this;
}

Expand All @@ -65,7 +58,7 @@ protected function doSend(MessageInterface $message): SentMessage
}

if ($message->getOptions() && !$message->getOptions() instanceof NtfyOptions) {
throw new LogicException(sprintf('The "%s" transport only supports instances of "%s" for options.', __CLASS__, NtfyOptions::class));
throw new LogicException(\sprintf('The "%s" transport only supports instances of "%s" for options.', __CLASS__, NtfyOptions::class));
}

if (!($opts = $message->getOptions()) && $notification = $message->getNotification()) {
Expand All @@ -86,10 +79,10 @@ protected function doSend(MessageInterface $message): SentMessage
$headers = [];

if (null !== $this->user && null !== $this->password) {
$headers['Authorization'] = 'Basic '.rtrim(base64_encode($this->user.':'.$this->password), '=');
$headers['Authorization'] = 'Basic '.\rtrim(\base64_encode($this->user.':'.$this->password), '=');
}

$response = $this->client->request('POST', ($this->isSecureHttp() ? 'https' : 'http').'://'.$this->getEndpoint(), [
$response = $this->client->request('POST', ($this->secureHttp ? 'https' : 'http').'://'.$this->getEndpoint(), [
'headers' => $headers,
'json' => $options,
]);
Expand All @@ -101,13 +94,13 @@ protected function doSend(MessageInterface $message): SentMessage
}

if (200 !== $statusCode) {
throw new TransportException(sprintf('Unable to send the Ntfy push notification: "%s".', $response->getContent(false)), $response);
throw new TransportException(\sprintf('Unable to send the Ntfy push notification: "%s".', $response->getContent(false)), $response);
}

$result = $response->toArray(false);

if (empty($result['id'])) {
throw new TransportException(sprintf('Unable to send the Ntfy push notification: "%s".', $response->getContent(false)), $response);
throw new TransportException(\sprintf('Unable to send the Ntfy push notification: "%s".', $response->getContent(false)), $response);
}

$sentMessage = new SentMessage($message, (string) $this);
Expand All @@ -124,6 +117,6 @@ public function supports(MessageInterface $message): bool

public function __toString(): string
{
return sprintf('ntfy://%s/%s', $this->getEndpoint(), $this->getTopic());
return \sprintf('ntfy://%s/%s', $this->getEndpoint(), $this->getTopic());
}
}
}
19 changes: 11 additions & 8 deletions src/Transport/NtfyTransportFactory.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

declare(strict_types=1);

namespace Mkk\NtfyBundle\Transport;
Expand All @@ -17,15 +18,17 @@ public function create(Dsn $dsn): TransportInterface
}

$host = 'default' === $dsn->getHost() ? null : $dsn->getHost();
$topic = substr($dsn->getPath(), 1);
$transport = (new NtfyTransport($topic))->setHost($host);
if (!empty($port = $dsn->getPort())) {
$transport->setPort($port);
$topic = \substr($dsn->getPath(), 1);

if (\in_array($dsn->getOption('secureHttp', true), [0, false, 'false', 'off', 'no'])) {
$secureHttp = false;
} else {
$secureHttp = true;
}

$secureHttp = $dsn->getOption('secureHttp', true);
if (in_array($secureHttp, [0, false, 'false', 'off', 'no'])) {
$transport->setSecureHttp(false);
$transport = (new NtfyTransport($topic, $secureHttp))->setHost($host);
if (!empty($port = $dsn->getPort())) {
$transport->setPort($port);
}

if (!empty($user = $dsn->getUser()) && !empty($password = $dsn->getPassword())) {
Expand All @@ -40,4 +43,4 @@ protected function getSupportedSchemes(): array
{
return ['ntfy'];
}
}
}

0 comments on commit aefb365

Please sign in to comment.