Skip to content

Commit 50749dd

Browse files
committed
feature #44303 Add Engagespot bridge (danut007ro)
This PR was squashed before being merged into the 6.1 branch. Discussion ---------- Add Engagespot bridge | Q | A | ------------- | --- | Branch? | 6.1 | Bug fix? | no | New feature? | yes | Deprecations? | no | Tickets | no ticket | License | MIT | Doc PR | symfony/symfony-docs#16165 Add [Engagespot](https://engagespot.co/) integration for push notifications. Commits ------- c7e1d1a Add Engagespot bridge
2 parents c817141 + c7e1d1a commit 50749dd

File tree

16 files changed

+485
-0
lines changed

16 files changed

+485
-0
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@
119119
use Symfony\Component\Notifier\Bridge\AmazonSns\AmazonSnsTransportFactory;
120120
use Symfony\Component\Notifier\Bridge\Clickatell\ClickatellTransportFactory;
121121
use Symfony\Component\Notifier\Bridge\Discord\DiscordTransportFactory;
122+
use Symfony\Component\Notifier\Bridge\Engagespot\EngagespotTransportFactory;
122123
use Symfony\Component\Notifier\Bridge\Esendex\EsendexTransportFactory;
123124
use Symfony\Component\Notifier\Bridge\Expo\ExpoTransportFactory;
124125
use Symfony\Component\Notifier\Bridge\FakeChat\FakeChatTransportFactory;
@@ -2446,6 +2447,7 @@ private function registerNotifierConfiguration(array $config, ContainerBuilder $
24462447
AmazonSnsTransportFactory::class => 'notifier.transport_factory.amazon-sns',
24472448
ClickatellTransportFactory::class => 'notifier.transport_factory.clickatell',
24482449
DiscordTransportFactory::class => 'notifier.transport_factory.discord',
2450+
EngagespotTransportFactory::class => 'notifier.transport_factory.engagespot',
24492451
EsendexTransportFactory::class => 'notifier.transport_factory.esendex',
24502452
ExpoTransportFactory::class => 'notifier.transport_factory.expo',
24512453
FakeChatTransportFactory::class => 'notifier.transport_factory.fake-chat',

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Symfony\Component\Notifier\Bridge\AmazonSns\AmazonSnsTransportFactory;
1616
use Symfony\Component\Notifier\Bridge\Clickatell\ClickatellTransportFactory;
1717
use Symfony\Component\Notifier\Bridge\Discord\DiscordTransportFactory;
18+
use Symfony\Component\Notifier\Bridge\Engagespot\EngagespotTransportFactory;
1819
use Symfony\Component\Notifier\Bridge\Esendex\EsendexTransportFactory;
1920
use Symfony\Component\Notifier\Bridge\Expo\ExpoTransportFactory;
2021
use Symfony\Component\Notifier\Bridge\FakeChat\FakeChatTransportFactory;
@@ -261,5 +262,9 @@
261262
->set('notifier.transport_factory.kaz-info-teh', KazInfoTehTransportFactory::class)
262263
->parent('notifier.transport_factory.abstract')
263264
->tag('texter.transport_factory')
265+
266+
->set('notifier.transport_factory.engagespot', EngagespotTransportFactory::class)
267+
->parent('notifier.transport_factory.abstract')
268+
->tag('texter.transport_factory')
264269
;
265270
};
Lines 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
Lines 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
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
CHANGELOG
2+
=========
3+
4+
6.1
5+
---
6+
7+
* Add the bridge
Lines 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\Engagespot;
13+
14+
use Symfony\Component\Notifier\Message\MessageOptionsInterface;
15+
16+
/**
17+
* @author Daniel GORGAN <https://github.com/danut007ro>
18+
*
19+
* @see https://docs.engagespot.co/how-to-send-notifications-via-engagespot-api/how-to-send-notifications-using-engagespot-rest-api
20+
*/
21+
final class EngagespotOptions implements MessageOptionsInterface
22+
{
23+
protected $options;
24+
25+
public function __construct(array $options = [])
26+
{
27+
$this->options = $options;
28+
}
29+
30+
public function toArray(): array
31+
{
32+
return $this->options;
33+
}
34+
35+
public function getRecipientId(): ?string
36+
{
37+
return $this->options['to'];
38+
}
39+
40+
/**
41+
* @return $this
42+
*/
43+
public function campaignName(string $campaignName): static
44+
{
45+
$this->options['campaign_name'] = $campaignName;
46+
47+
return $this;
48+
}
49+
50+
/**
51+
* @return $this
52+
*/
53+
public function to(string $to): static
54+
{
55+
$this->options['to'] = $to;
56+
57+
return $this;
58+
}
59+
60+
/**
61+
* @return $this
62+
*/
63+
public function identifiers(array $identifiers): static
64+
{
65+
$this->options['identifiers'] = $identifiers;
66+
67+
return $this;
68+
}
69+
70+
/**
71+
* @return $this
72+
*/
73+
public function everyone(bool $everyone): static
74+
{
75+
$this->options['everyone'] = $everyone;
76+
77+
return $this;
78+
}
79+
80+
/**
81+
* @return $this
82+
*/
83+
public function icon(string $icon): static
84+
{
85+
$this->options['icon'] = $icon;
86+
87+
return $this;
88+
}
89+
90+
/**
91+
* @return $this
92+
*/
93+
public function url(string $url): static
94+
{
95+
$this->options['url'] = $url;
96+
97+
return $this;
98+
}
99+
}
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
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\Engagespot;
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\MessageInterface;
18+
use Symfony\Component\Notifier\Message\PushMessage;
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 Daniel GORGAN <https://github.com/danut007ro>
27+
*/
28+
final class EngagespotTransport extends AbstractTransport
29+
{
30+
protected const HOST = 'api.engagespot.co/2/campaigns';
31+
32+
private $apiKey;
33+
private $campaignName;
34+
35+
public function __construct(string $apiKey, string $campaignName, HttpClientInterface $client = null, EventDispatcherInterface $dispatcher = null)
36+
{
37+
$this->apiKey = $apiKey;
38+
$this->campaignName = $campaignName;
39+
$this->client = $client;
40+
41+
parent::__construct($client, $dispatcher);
42+
}
43+
44+
public function __toString(): string
45+
{
46+
return sprintf('engagespot://%s?campaign_name=%s', $this->getEndpoint(), $this->campaignName);
47+
}
48+
49+
public function supports(MessageInterface $message): bool
50+
{
51+
return $message instanceof PushMessage;
52+
}
53+
54+
protected function doSend(MessageInterface $message): SentMessage
55+
{
56+
if (!$message instanceof PushMessage) {
57+
throw new UnsupportedMessageTypeException(__CLASS__, PushMessage::class, $message);
58+
}
59+
60+
$endpoint = sprintf('https://%s', $this->getEndpoint());
61+
$options = ($opts = $message->getOptions()) ? $opts->toArray() : [];
62+
if (!isset($options['to'])) {
63+
$options['to'] = $message->getRecipientId();
64+
}
65+
66+
$sendToEveryone = $options['everyone'] ?? false;
67+
if (!$sendToEveryone) {
68+
// Use either "to" or "identifiers" as recipient list.
69+
if (null !== $options['to']) {
70+
$identifiers = [$options['to']];
71+
} elseif (!\is_array($options['identifiers'] ?? null)) {
72+
throw new InvalidArgumentException(sprintf('The "%s" transport required the "to" or "identifiers" option to be set when not sending to everyone.', __CLASS__));
73+
} else {
74+
$identifiers = $options['identifiers'];
75+
}
76+
}
77+
78+
$response = $this->client->request('POST', $endpoint, [
79+
'headers' => [
80+
'API-Key' => $this->apiKey,
81+
],
82+
'json' => [
83+
'campaign_name' => $options['campaign_name'] ?? $this->campaignName,
84+
'notification' => [
85+
'title' => $message->getSubject(),
86+
'message' => $message->getContent(),
87+
'icon' => $options['icon'] ?? '',
88+
'url' => $options['url'] ?? '#',
89+
],
90+
'send_to' => $sendToEveryone ? 'everyone' : 'identifiers',
91+
'identifiers' => $identifiers ?? null,
92+
],
93+
]);
94+
95+
try {
96+
$statusCode = $response->getStatusCode();
97+
if (200 !== $statusCode) {
98+
throw new TransportException('Invalid status code received from Engagespot server: '.$statusCode, $response);
99+
}
100+
} catch (TransportExceptionInterface $e) {
101+
throw new TransportException('Could not reach the remote Engagespot server.', $response, 0, $e);
102+
}
103+
104+
$jsonContents = $response->toArray(false);
105+
if ('ok' !== $jsonContents['status'] ?? null) {
106+
$errorMessage = $jsonContents['message'] ?? $response->getContent(false);
107+
108+
throw new TransportException('Unable to post the Engagespot message: '.$errorMessage, $response);
109+
}
110+
111+
return new SentMessage($message, (string) $this);
112+
}
113+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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\Engagespot;
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 Daniel GORGAN <https://github.com/danut007ro>
21+
*/
22+
final class EngagespotTransportFactory extends AbstractTransportFactory
23+
{
24+
public function create(Dsn $dsn): EngagespotTransport
25+
{
26+
$scheme = $dsn->getScheme();
27+
28+
if ('engagespot' !== $scheme) {
29+
throw new UnsupportedSchemeException($dsn, 'engagespot', $this->getSupportedSchemes());
30+
}
31+
32+
$apiKey = $dsn->getUser();
33+
$campaignName = $dsn->getRequiredOption('campaign_name');
34+
$host = 'default' === $dsn->getHost() ? null : $dsn->getHost();
35+
$port = $dsn->getPort();
36+
37+
return (new EngagespotTransport($apiKey, $campaignName, $this->client, $this->dispatcher))->setHost($host)->setPort($port);
38+
}
39+
40+
protected function getSupportedSchemes(): array
41+
{
42+
return ['engagespot'];
43+
}
44+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
Copyright (c) 2022 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.
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
Engagespot Notifier
2+
===================
3+
4+
Provides [Engagespot](https://docs.engagespot.co/how-to-send-notifications-via-engagespot-api/how-to-send-notifications-using-engagespot-rest-api) integration for Symfony Notifier.
5+
6+
DSN example
7+
-----------
8+
9+
```
10+
ENGAGESPOT_DSN=engagespot://API_KEY@default?campaign_name=CAMPAIGN_NAME
11+
```
12+
13+
where:
14+
- `API_KEY` is your Engagespot API Key
15+
- `CAMPAIGN_NAME` is your default campaign name
16+
17+
Resources
18+
---------
19+
20+
* [Contributing](https://symfony.com/doc/current/contributing/index.html)
21+
* [Report issues](https://github.com/symfony/symfony/issues) and
22+
[send Pull Requests](https://github.com/symfony/symfony/pulls)
23+
in the [main Symfony repository](https://github.com/symfony/symfony)

0 commit comments

Comments
 (0)