Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit aefb365

Browse files
committed
Apply same changes as in PR for 6.4 (symfony/symfony#50131)
1 parent e3d939d commit aefb365

File tree

10 files changed

+122
-75
lines changed

10 files changed

+122
-75
lines changed

.github/workflows/build.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ jobs:
3434
- '8.0'
3535
- '8.1'
3636
- '8.2'
37+
symfony: ['*']
3738
include:
3839
- description: 'Symfony 5.4'
3940
php: '7.4'

README.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,22 @@ Ntfy Notifier
44
[![Build Status](https://github.com/mikaelkael/ntfy-notifier/workflows/Build/badge.svg)](https://github.com/mikaelkael/ntfy-notifier/actions)
55
[![License](https://poser.pugx.org/mikaelkael/ntfy-notifier/license.png)](https://packagist.org/packages/mikaelkael/ntfy-notifier)
66

7-
Provides [Ntfy](https://docs.ntfy.sh/) integration for Symfony Notifier.
7+
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.
88

99
DSN example
1010
-----------
1111

1212
```
1313
# .env
14-
NTFY_DSN=ntfy://[NTFY_USER:NTFY_PASSWORD]@NTFY_URL[:NTFY_PORT]/NTFY_TOPIC?[secureHttp=[on]]
14+
NTFY_DSN=ntfy://[USER:PASSWORD]@default[:PORT]/TOPIC?[secureHttp=[on]]
1515
```
1616

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

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

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
],
1313
"require": {
1414
"php": ">=7.4",
15+
"ext-json": "*",
1516
"symfony/framework-bundle": "^5.4|^6.0",
1617
"symfony/http-client": "^5.0|^6.0",
1718
"symfony/notifier": "^5.4|^6.0"

src/DependencyInjection/MkkNtfyExtension.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,17 @@
22

33
namespace Mkk\NtfyBundle\DependencyInjection;
44

5-
use Symfony\Component\DependencyInjection\ContainerBuilder;
65
use Symfony\Component\Config\FileLocator;
7-
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
6+
use Symfony\Component\DependencyInjection\ContainerBuilder;
87
use Symfony\Component\DependencyInjection\Loader;
8+
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
99

1010
class MkkNtfyExtension extends Extension
1111
{
1212
/**
1313
* {@inheritdoc}
1414
*/
15-
public function load(array $configs, ContainerBuilder $container)
15+
public function load(array $configs, ContainerBuilder $container): void
1616
{
1717
$loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../../config'));
1818
$loader->load('services.yml');

src/Message/NtfyOptions.php

Lines changed: 35 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,21 @@
11
<?php
2+
23
declare(strict_types=1);
34

45
namespace Mkk\NtfyBundle\Message;
56

7+
use Symfony\Component\Notifier\Exception\LogicException;
68
use Symfony\Component\Notifier\Message\MessageOptionsInterface;
79
use Symfony\Component\Notifier\Notification\Notification;
810

911
final class NtfyOptions implements MessageOptionsInterface
1012
{
1113
private array $options;
12-
const PRIORITY_MAX = 5;
13-
const PRIORITY_URGENT = 5;
14-
const PRIORITY_HIGH = 4;
15-
const PRIORITY_DEFAULT = 3;
16-
const PRIORITY_LOW = 2;
17-
const PRIORITY_MIN = 1;
14+
public const PRIORITY_URGENT = 5;
15+
public const PRIORITY_HIGH = 4;
16+
public const PRIORITY_DEFAULT = 3;
17+
public const PRIORITY_LOW = 2;
18+
public const PRIORITY_MIN = 1;
1819

1920
public function __construct(array $options = [])
2021
{
@@ -28,6 +29,7 @@ public static function fromNotification(Notification $notification): self
2829
$options->setMessage($notification->getContent());
2930
$options->setStringPriority($notification->getImportance());
3031
$options->addTag($notification->getEmoji());
32+
3133
return $options;
3234
}
3335

@@ -44,12 +46,14 @@ public function getRecipientId(): ?string
4446
public function setMessage(string $message): self
4547
{
4648
$this->options['message'] = $message;
49+
4750
return $this;
4851
}
4952

5053
public function setTitle(string $title): self
5154
{
5255
$this->options['title'] = $title;
56+
5357
return $this;
5458
}
5559

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

7074
public function setPriority(int $priority): self
7175
{
72-
if (in_array($priority, [
73-
self::PRIORITY_MIN, self::PRIORITY_LOW, self::PRIORITY_DEFAULT, self::PRIORITY_HIGH, self::PRIORITY_URGENT, self::PRIORITY_MAX
76+
if (\in_array($priority, [
77+
self::PRIORITY_MIN, self::PRIORITY_LOW, self::PRIORITY_DEFAULT, self::PRIORITY_HIGH, self::PRIORITY_URGENT,
7478
])) {
7579
$this->options['priority'] = $priority;
7680
}
81+
7782
return $this;
7883
}
7984

8085
public function addTag(string $tag): self
8186
{
8287
$this->options['tags'][] = $tag;
88+
8389
return $this;
8490
}
8591

8692
public function setTags(array $tags): self
8793
{
8894
$this->options['tags'] = $tags;
95+
8996
return $this;
9097
}
9198

9299
public function setDelay(\DateTimeInterface $dateTime): self
93100
{
94-
$this->options['delay'] = $dateTime->getTimestamp();
101+
if ($dateTime > (new \DateTime())) {
102+
$this->options['delay'] = (string) $dateTime->getTimestamp();
103+
} else {
104+
throw new LogicException('Delayed date must be defined in the future.');
105+
}
106+
95107
return $this;
96108
}
97109

98110
public function setActions(array $actions): self
99111
{
100112
$this->options['actions'] = $actions;
113+
101114
return $this;
102115
}
103116

104117
public function addAction(array $action): self
105118
{
106119
$this->options['actions'][] = $action;
120+
107121
return $this;
108122
}
109123

110124
public function setClick(string $url): self
111125
{
112126
$this->options['click'] = $url;
127+
113128
return $this;
114129
}
115130

116131
public function setAttachment(string $attachment): self
117132
{
118133
$this->options['attach'] = $attachment;
134+
119135
return $this;
120136
}
121137

122138
public function setFilename(string $filename): self
123139
{
124140
$this->options['filename'] = $filename;
141+
125142
return $this;
126143
}
127144

128145
public function setEmail(string $email): self
129146
{
130147
$this->options['email'] = $email;
148+
131149
return $this;
132150
}
133151

134-
public function setCache(bool $enable)
152+
public function setCache(bool $enable): self
135153
{
136154
if (!$enable) {
137155
$this->options['cache'] = 'no';
138156
} else {
139157
unset($this->options['cache']);
140158
}
159+
160+
return $this;
141161
}
142-
public function setFirebase(bool $enable)
162+
163+
public function setFirebase(bool $enable): self
143164
{
144165
if (!$enable) {
145166
$this->options['firebase'] = 'no';
146167
} else {
147168
unset($this->options['firebase']);
148169
}
170+
171+
return $this;
149172
}
150-
}
173+
}

src/Transport/NtfyTransport.php

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
23
declare(strict_types=1);
34

45
namespace Mkk\NtfyBundle\Transport;
@@ -21,11 +22,12 @@ final class NtfyTransport extends AbstractTransport
2122
private ?string $user = null;
2223
private ?string $password = null;
2324
private string $topic;
24-
private bool $secureHttp = true;
25+
private bool $secureHttp;
2526

26-
public function __construct(string $topic, HttpClientInterface $client = null, EventDispatcherInterface $dispatcher = null)
27+
public function __construct(string $topic, bool $secureHttp = true, HttpClientInterface $client = null, EventDispatcherInterface $dispatcher = null)
2728
{
2829
$this->topic = $topic;
30+
$this->secureHttp = $secureHttp;
2931

3032
parent::__construct($client, $dispatcher);
3133
}
@@ -35,26 +37,17 @@ public function getTopic(): string
3537
return $this->topic;
3638
}
3739

38-
public function isSecureHttp(): bool
39-
{
40-
return $this->secureHttp;
41-
}
42-
43-
public function setSecureHttp(bool $secureHttp): self
44-
{
45-
$this->secureHttp = $secureHttp;
46-
return $this;
47-
}
48-
4940
public function setPassword(?string $password): self
5041
{
5142
$this->password = $password;
43+
5244
return $this;
5345
}
5446

5547
public function setUser(?string $user): self
5648
{
5749
$this->user = $user;
50+
5851
return $this;
5952
}
6053

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

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

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

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

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

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

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

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

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

125118
public function __toString(): string
126119
{
127-
return sprintf('ntfy://%s/%s', $this->getEndpoint(), $this->getTopic());
120+
return \sprintf('ntfy://%s/%s', $this->getEndpoint(), $this->getTopic());
128121
}
129-
}
122+
}

src/Transport/NtfyTransportFactory.php

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
23
declare(strict_types=1);
34

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

1920
$host = 'default' === $dsn->getHost() ? null : $dsn->getHost();
20-
$topic = substr($dsn->getPath(), 1);
21-
$transport = (new NtfyTransport($topic))->setHost($host);
22-
if (!empty($port = $dsn->getPort())) {
23-
$transport->setPort($port);
21+
$topic = \substr($dsn->getPath(), 1);
22+
23+
if (\in_array($dsn->getOption('secureHttp', true), [0, false, 'false', 'off', 'no'])) {
24+
$secureHttp = false;
25+
} else {
26+
$secureHttp = true;
2427
}
2528

26-
$secureHttp = $dsn->getOption('secureHttp', true);
27-
if (in_array($secureHttp, [0, false, 'false', 'off', 'no'])) {
28-
$transport->setSecureHttp(false);
29+
$transport = (new NtfyTransport($topic, $secureHttp))->setHost($host);
30+
if (!empty($port = $dsn->getPort())) {
31+
$transport->setPort($port);
2932
}
3033

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

0 commit comments

Comments
 (0)