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 ed8f2fa

Browse filesBrowse files
committed
Add Expo Notifier
1 parent f36d0e0 commit ed8f2fa
Copy full SHA for ed8f2fa

16 files changed

+547
-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
@@ -116,6 +116,7 @@
116116
use Symfony\Component\Notifier\Bridge\Clickatell\ClickatellTransportFactory;
117117
use Symfony\Component\Notifier\Bridge\Discord\DiscordTransportFactory;
118118
use Symfony\Component\Notifier\Bridge\Esendex\EsendexTransportFactory;
119+
use Symfony\Component\Notifier\Bridge\Expo\ExpoTransportFactory;
119120
use Symfony\Component\Notifier\Bridge\FakeChat\FakeChatTransportFactory;
120121
use Symfony\Component\Notifier\Bridge\FakeSms\FakeSmsTransportFactory;
121122
use Symfony\Component\Notifier\Bridge\Firebase\FirebaseTransportFactory;
@@ -2452,6 +2453,7 @@ private function registerNotifierConfiguration(array $config, ContainerBuilder $
24522453
ClickatellTransportFactory::class => 'notifier.transport_factory.clickatell',
24532454
DiscordTransportFactory::class => 'notifier.transport_factory.discord',
24542455
EsendexTransportFactory::class => 'notifier.transport_factory.esendex',
2456+
ExpoTransportFactory::class => 'notifier.transport_factory.expo',
24552457
FakeChatTransportFactory::class => 'notifier.transport_factory.fakechat',
24562458
FakeSmsTransportFactory::class => 'notifier.transport_factory.fakesms',
24572459
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;
@@ -235,5 +236,9 @@
235236
->set('notifier.transport_factory.onesignal', OneSignalTransportFactory::class)
236237
->parent('notifier.transport_factory.abstract')
237238
->tag('texter.transport_factory')
239+
240+
->set('notifier.transport_factory.expo', ExpoTransportFactory::class)
241+
->parent('notifier.transport_factory.abstract')
242+
->tag('chatter.transport_factory')
238243
;
239244
};
+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
+174Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
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+
final class ExpoOptions implements MessageOptionsInterface
22+
{
23+
private $to;
24+
25+
/**
26+
* @see https://docs.expo.dev/push-notifications/sending-notifications/#message-request-format
27+
*/
28+
protected $options;
29+
30+
private $data;
31+
32+
public function __construct(string $to, array $options = [], array $data = [])
33+
{
34+
$this->to = $to;
35+
$this->options = $options;
36+
$this->data = $data;
37+
}
38+
39+
public function toArray(): array
40+
{
41+
return array_merge(
42+
$this->options,
43+
[
44+
'to' => $this->to,
45+
'data' => $this->data,
46+
]
47+
);
48+
}
49+
50+
public function getRecipientId(): ?string
51+
{
52+
return $this->to;
53+
}
54+
55+
/**
56+
* @return $this
57+
*/
58+
public function title(string $title): self
59+
{
60+
$this->options['title'] = $title;
61+
62+
return $this;
63+
}
64+
65+
/**
66+
* @return $this
67+
*/
68+
public function subtitle(string $subtitle): self
69+
{
70+
$this->options['subtitle'] = $subtitle;
71+
72+
return $this;
73+
}
74+
75+
/**
76+
* @return $this
77+
*/
78+
public function priority(string $priority): self
79+
{
80+
$this->options['priority'] = $priority;
81+
82+
return $this;
83+
}
84+
85+
/**
86+
* @return $this
87+
*/
88+
public function sound(string $sound): self
89+
{
90+
$this->options['sound'] = $sound;
91+
92+
return $this;
93+
}
94+
95+
/**
96+
* @return $this
97+
*/
98+
public function badge(int $badge): self
99+
{
100+
$this->options['badge'] = $badge;
101+
102+
return $this;
103+
}
104+
105+
/**
106+
* @return $this
107+
*/
108+
public function channelId(string $channelId): self
109+
{
110+
$this->options['channelId'] = $channelId;
111+
112+
return $this;
113+
}
114+
115+
/**
116+
* @return $this
117+
*/
118+
public function categoryId(string $categoryId): self
119+
{
120+
$this->options['categoryId'] = $categoryId;
121+
122+
return $this;
123+
}
124+
125+
/**
126+
* @return $this
127+
*/
128+
public function mutableContent(bool $mutableContent): self
129+
{
130+
$this->options['mutableContent'] = $mutableContent;
131+
132+
return $this;
133+
}
134+
135+
/**
136+
* @return $this
137+
*/
138+
public function body(string $body): self
139+
{
140+
$this->options['body'] = $body;
141+
142+
return $this;
143+
}
144+
145+
/**
146+
* @return $this
147+
*/
148+
public function ttl(int $ttl): self
149+
{
150+
$this->options['ttl'] = $ttl;
151+
152+
return $this;
153+
}
154+
155+
/**
156+
* @return $this
157+
*/
158+
public function expiration(int $expiration): self
159+
{
160+
$this->options['expiration'] = $expiration;
161+
162+
return $this;
163+
}
164+
165+
/**
166+
* @return $this
167+
*/
168+
public function data(array $data): self
169+
{
170+
$this->data = $data;
171+
172+
return $this;
173+
}
174+
}
+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.