Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit cc39c71

Browse filesBrowse files
committed
Add Expo Notifier
1 parent 810599d commit cc39c71
Copy full SHA for cc39c71

16 files changed

+548
-0
lines changed

‎src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php
+2Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@
114114
use Symfony\Component\Notifier\Bridge\Clickatell\ClickatellTransportFactory;
115115
use Symfony\Component\Notifier\Bridge\Discord\DiscordTransportFactory;
116116
use Symfony\Component\Notifier\Bridge\Esendex\EsendexTransportFactory;
117+
use Symfony\Component\Notifier\Bridge\Expo\ExpoTransportFactory;
117118
use Symfony\Component\Notifier\Bridge\FakeChat\FakeChatTransportFactory;
118119
use Symfony\Component\Notifier\Bridge\FakeSms\FakeSmsTransportFactory;
119120
use Symfony\Component\Notifier\Bridge\Firebase\FirebaseTransportFactory;
@@ -2432,6 +2433,7 @@ private function registerNotifierConfiguration(array $config, ContainerBuilder $
24322433
ClickatellTransportFactory::class => 'notifier.transport_factory.clickatell',
24332434
DiscordTransportFactory::class => 'notifier.transport_factory.discord',
24342435
EsendexTransportFactory::class => 'notifier.transport_factory.esendex',
2436+
ExpoTransportFactory::class => 'notifier.transport_factory.expo',
24352437
FakeChatTransportFactory::class => 'notifier.transport_factory.fakechat',
24362438
FakeSmsTransportFactory::class => 'notifier.transport_factory.fakesms',
24372439
FirebaseTransportFactory::class => 'notifier.transport_factory.firebase',

‎src/Symfony/Bundle/FrameworkBundle/Resources/config/notifier_transports.php

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/Resources/config/notifier_transports.php
+5Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Symfony\Component\Notifier\Bridge\Clickatell\ClickatellTransportFactory;
1717
use Symfony\Component\Notifier\Bridge\Discord\DiscordTransportFactory;
1818
use Symfony\Component\Notifier\Bridge\Esendex\EsendexTransportFactory;
19+
use Symfony\Component\Notifier\Bridge\Expo\ExpoTransportFactory;
1920
use Symfony\Component\Notifier\Bridge\FakeChat\FakeChatTransportFactory;
2021
use Symfony\Component\Notifier\Bridge\FakeSms\FakeSmsTransportFactory;
2122
use Symfony\Component\Notifier\Bridge\Firebase\FirebaseTransportFactory;
@@ -225,5 +226,9 @@
225226
->set('notifier.transport_factory.turbosms', TurboSmsTransportFactory::class)
226227
->parent('notifier.transport_factory.abstract')
227228
->tag('texter.transport_factory')
229+
230+
->set('notifier.transport_factory.expo', ExpoTransportFactory::class)
231+
->parent('notifier.transport_factory.abstract')
232+
->tag('chatter.transport_factory')
228233
;
229234
};
+4Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/Tests export-ignore
2+
/phpunit.xml.dist export-ignore
3+
/.gitattributes export-ignore
4+
/.gitignore export-ignore
+3Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
vendor/
2+
composer.lock
3+
phpunit.xml
+7Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
CHANGELOG
2+
=========
3+
4+
5.4
5+
---
6+
7+
* Add the bridge
+175Lines changed: 175 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Notifier\Bridge\Expo;
13+
14+
use Symfony\Component\Notifier\Message\MessageOptionsInterface;
15+
16+
/**
17+
* @author Imad ZAIRIG <https://github.com/zairigimad>
18+
*
19+
* @see https://docs.expo.dev/push-notifications/sending-notifications/
20+
*
21+
*/
22+
final class ExpoOptions implements MessageOptionsInterface
23+
{
24+
private $to;
25+
26+
/**
27+
* @see https://docs.expo.dev/push-notifications/sending-notifications/#message-request-format
28+
*/
29+
protected $options;
30+
31+
private $data;
32+
33+
public function __construct(string $to, array $options = [], array $data = [])
34+
{
35+
$this->to = $to;
36+
$this->options = $options;
37+
$this->data = $data;
38+
}
39+
40+
public function toArray(): array
41+
{
42+
return array_merge(
43+
$this->options,
44+
[
45+
'to' => $this->to,
46+
'data' => $this->data,
47+
]
48+
);
49+
}
50+
51+
public function getRecipientId(): ?string
52+
{
53+
return $this->to;
54+
}
55+
56+
/**
57+
* @return $this
58+
*/
59+
public function title(string $title): self
60+
{
61+
$this->options['title'] = $title;
62+
63+
return $this;
64+
}
65+
66+
/**
67+
* @return $this
68+
*/
69+
public function subtitle(string $subtitle): self
70+
{
71+
$this->options['subtitle'] = $subtitle;
72+
73+
return $this;
74+
}
75+
76+
/**
77+
* @return $this
78+
*/
79+
public function priority(string $priority): self
80+
{
81+
$this->options['priority'] = $priority;
82+
83+
return $this;
84+
}
85+
86+
/**
87+
* @return $this
88+
*/
89+
public function sound(string $sound): self
90+
{
91+
$this->options['sound'] = $sound;
92+
93+
return $this;
94+
}
95+
96+
/**
97+
* @return $this
98+
*/
99+
public function badge(int $badge): self
100+
{
101+
$this->options['badge'] = $badge;
102+
103+
return $this;
104+
}
105+
106+
/**
107+
* @return $this
108+
*/
109+
public function channelId(string $channelId): self
110+
{
111+
$this->options['channelId'] = $channelId;
112+
113+
return $this;
114+
}
115+
116+
/**
117+
* @return $this
118+
*/
119+
public function categoryId(string $categoryId): self
120+
{
121+
$this->options['categoryId'] = $categoryId;
122+
123+
return $this;
124+
}
125+
126+
/**
127+
* @return $this
128+
*/
129+
public function mutableContent(bool $mutableContent): self
130+
{
131+
$this->options['mutableContent'] = $mutableContent;
132+
133+
return $this;
134+
}
135+
136+
/**
137+
* @return $this
138+
*/
139+
public function body(string $body): self
140+
{
141+
$this->options['body'] = $body;
142+
143+
return $this;
144+
}
145+
146+
/**
147+
* @return $this
148+
*/
149+
public function ttl(int $ttl): self
150+
{
151+
$this->options['ttl'] = $ttl;
152+
153+
return $this;
154+
}
155+
156+
/**
157+
* @return $this
158+
*/
159+
public function expiration(int $expiration): self
160+
{
161+
$this->options['expiration'] = $expiration;
162+
163+
return $this;
164+
}
165+
166+
/**
167+
* @return $this
168+
*/
169+
public function data(array $data): self
170+
{
171+
$this->data = $data;
172+
173+
return $this;
174+
}
175+
}
+99Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Notifier\Bridge\Expo;
13+
14+
use Symfony\Component\Notifier\Exception\InvalidArgumentException;
15+
use Symfony\Component\Notifier\Exception\TransportException;
16+
use Symfony\Component\Notifier\Exception\UnsupportedMessageTypeException;
17+
use Symfony\Component\Notifier\Message\ChatMessage;
18+
use Symfony\Component\Notifier\Message\MessageInterface;
19+
use Symfony\Component\Notifier\Message\SentMessage;
20+
use Symfony\Component\Notifier\Transport\AbstractTransport;
21+
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
22+
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
23+
use Symfony\Contracts\HttpClient\HttpClientInterface;
24+
25+
/**
26+
* @author Imad ZAIRIG <https://github.com/zairigimad>
27+
*/
28+
final class ExpoTransport extends AbstractTransport
29+
{
30+
protected const HOST = 'exp.host/--/api/v2/push/send';
31+
32+
/** @var string */
33+
private $token;
34+
35+
public function __construct(string $token = null, HttpClientInterface $client = null, EventDispatcherInterface $dispatcher = null)
36+
{
37+
$this->token = $token;
38+
$this->client = $client;
39+
40+
parent::__construct($client, $dispatcher);
41+
}
42+
43+
public function __toString(): string
44+
{
45+
return sprintf('expo://%s', $this->getEndpoint());
46+
}
47+
48+
public function supports(MessageInterface $message): bool
49+
{
50+
return $message instanceof ChatMessage;
51+
}
52+
53+
protected function doSend(MessageInterface $message): SentMessage
54+
{
55+
if (!$message instanceof ChatMessage) {
56+
throw new UnsupportedMessageTypeException(__CLASS__, ChatMessage::class, $message);
57+
}
58+
59+
$endpoint = sprintf('https://%s', $this->getEndpoint());
60+
$options = ($opts = $message->getOptions()) ? $opts->toArray() : [];
61+
if (!isset($options['to'])) {
62+
$options['to'] = $message->getRecipientId();
63+
}
64+
if (null === $options['to']) {
65+
throw new InvalidArgumentException(sprintf('The "%s" transport required the "to" option to be set.', __CLASS__));
66+
}
67+
$options['body'] = $message->getSubject();
68+
$options['data'] = $options['data'] ?? [];
69+
70+
$response = $this->client->request('POST', $endpoint, [
71+
'headers' => [
72+
'Authorization' => $this->token ? sprintf('Bearer %s', $this->token) : null,
73+
],
74+
'json' => array_filter($options),
75+
]);
76+
77+
try {
78+
$statusCode = $response->getStatusCode();
79+
} catch (TransportExceptionInterface $e) {
80+
throw new TransportException('Could not reach the remote Expo server.', $response, 0, $e);
81+
}
82+
83+
$contentType = $response->getHeaders(false)['content-type'][0] ?? '';
84+
$jsonContents = 0 === strpos($contentType, 'application/json') ? $response->toArray(false) : null;
85+
86+
if (200 !== $statusCode) {
87+
$errorMessage = $jsonContents['error']['message'] ?? $response->getContent(false);
88+
89+
throw new TransportException('Unable to post the Expo message: '.$errorMessage, $response);
90+
}
91+
92+
$success = $response->toArray(false);
93+
94+
$sentMessage = new SentMessage($message, (string) $this);
95+
$sentMessage->setMessageId($success['data']['id']);
96+
97+
return $sentMessage;
98+
}
99+
}
+46Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Notifier\Bridge\Expo;
13+
14+
use Symfony\Component\Notifier\Exception\UnsupportedSchemeException;
15+
use Symfony\Component\Notifier\Transport\AbstractTransportFactory;
16+
use Symfony\Component\Notifier\Transport\Dsn;
17+
use Symfony\Component\Notifier\Transport\TransportInterface;
18+
19+
/**
20+
* @author Imad ZAIRIG <https://github.com/zairigimad>
21+
*/
22+
final class ExpoTransportFactory extends AbstractTransportFactory
23+
{
24+
/**
25+
* @return ExpoTransport
26+
*/
27+
public function create(Dsn $dsn): TransportInterface
28+
{
29+
$scheme = $dsn->getScheme();
30+
31+
if ('expo' !== $scheme) {
32+
throw new UnsupportedSchemeException($dsn, 'expo', $this->getSupportedSchemes());
33+
}
34+
35+
$token = $dsn->getUser($dsn);
36+
$host = 'default' === $dsn->getHost() ? null : $dsn->getHost();
37+
$port = $dsn->getPort();
38+
39+
return (new ExpoTransport($token, $this->client, $this->dispatcher))->setHost($host)->setPort($port);
40+
}
41+
42+
protected function getSupportedSchemes(): array
43+
{
44+
return ['expo'];
45+
}
46+
}
+19Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
Copyright (c) 2021 Fabien Potencier
2+
3+
Permission is hereby granted, free of charge, to any person obtaining a copy
4+
of this software and associated documentation files (the "Software"), to deal
5+
in the Software without restriction, including without limitation the rights
6+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7+
copies of the Software, and to permit persons to whom the Software is furnished
8+
to do so, subject to the following conditions:
9+
10+
The above copyright notice and this permission notice shall be included in all
11+
copies or substantial portions of the Software.
12+
13+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19+
THE SOFTWARE.

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.