Skip to content

Commit e70e216

Browse files
author
Olivier Dolbeau
committed
Add Esendex Notifier bridge
1 parent c699b9c commit e70e216

File tree

12 files changed

+256
-0
lines changed

12 files changed

+256
-0
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@
9191
use Symfony\Component\Messenger\Transport\TransportInterface;
9292
use Symfony\Component\Mime\MimeTypeGuesserInterface;
9393
use Symfony\Component\Mime\MimeTypes;
94+
use Symfony\Component\Notifier\Bridge\Esendex\EsendexTransportFactory;
9495
use Symfony\Component\Notifier\Bridge\Firebase\FirebaseTransportFactory;
9596
use Symfony\Component\Notifier\Bridge\FreeMobile\FreeMobileTransportFactory;
9697
use Symfony\Component\Notifier\Bridge\Mattermost\MattermostTransportFactory;
@@ -2047,6 +2048,7 @@ private function registerNotifierConfiguration(array $config, ContainerBuilder $
20472048
FreeMobileTransportFactory::class => 'notifier.transport_factory.freemobile',
20482049
OvhCloudTransportFactory::class => 'notifier.transport_factory.ovhcloud',
20492050
SinchTransportFactory::class => 'notifier.transport_factory.sinch',
2051+
EsendexTransportFactory::class => 'notifier.transport_factory.esendex',
20502052
];
20512053

20522054
foreach ($classToServices as $class => $service) {

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@
5050
<tag name="texter.transport_factory" />
5151
</service>
5252

53+
<service id="notifier.transport_factory.esendex" class="Symfony\Component\Notifier\Bridge\Esendex\EsendexTransportFactory" parent="notifier.transport_factory.abstract">
54+
<tag name="texter.transport_factory" />
55+
</service>
56+
5357
<service id="notifier.transport_factory.null" class="Symfony\Component\Notifier\Transport\NullTransportFactory" parent="notifier.transport_factory.abstract">
5458
<tag name="chatter.transport_factory" />
5559
<tag name="texter.transport_factory" />
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: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
CHANGELOG
2+
=========
3+
4+
5.0.0
5+
-----
6+
7+
* Added the bridge
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
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\Esendex;
13+
14+
use Symfony\Component\Notifier\Exception\LogicException;
15+
use Symfony\Component\Notifier\Exception\TransportException;
16+
use Symfony\Component\Notifier\Message\MessageInterface;
17+
use Symfony\Component\Notifier\Message\SmsMessage;
18+
use Symfony\Component\Notifier\Transport\AbstractTransport;
19+
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
20+
use Symfony\Contracts\HttpClient\HttpClientInterface;
21+
22+
/**
23+
* @experimental in 5.1
24+
*/
25+
final class EsendexTransport extends AbstractTransport
26+
{
27+
protected const HOST = 'api.esendex.com';
28+
29+
private $token;
30+
private $accountReference;
31+
private $from;
32+
33+
public function __construct(string $token, string $accountReference, string $from, HttpClientInterface $client = null, EventDispatcherInterface $dispatcher = null)
34+
{
35+
$this->token = $token;
36+
$this->accountReference = $accountReference;
37+
$this->from = $from;
38+
39+
parent::__construct($client, $dispatcher);
40+
}
41+
42+
public function __toString(): string
43+
{
44+
return sprintf('esendex://%s', $this->getEndpoint());
45+
}
46+
47+
public function supports(MessageInterface $message): bool
48+
{
49+
return $message instanceof SmsMessage;
50+
}
51+
52+
protected function doSend(MessageInterface $message): void
53+
{
54+
if (!$message instanceof SmsMessage) {
55+
throw new LogicException(sprintf('The "%s" transport only supports instances of "%s" (instance of "%s" given).', __CLASS__, SmsMessage::class, get_debug_type($message)));
56+
}
57+
58+
$messageData = [
59+
'to' => $message->getPhone(),
60+
'body' => $message->getSubject(),
61+
];
62+
if (null !== $this->from) {
63+
$messageData['from'] = $this->from;
64+
}
65+
66+
$response = $this->client->request('POST', 'https://'.$this->getEndpoint().'/v1.0/messagedispatcher', [
67+
'auth_basic' => $this->token,
68+
'headers' => [
69+
'Content-Type' => 'application/json',
70+
'Accept' => 'application/json',
71+
],
72+
'body' => json_encode([
73+
'accountreference' => $this->accountReference,
74+
'messages' => [$messageData],
75+
]),
76+
]);
77+
78+
if (401 === $response->getStatusCode() || 403 === $response->getStatusCode()) {
79+
throw new TransportException(sprintf('Unable to send the SMS: %s. Check your credentials.', $message->getSubject()), $response);
80+
}
81+
82+
$result = $response->toArray(false);
83+
if (!empty($result['errors'])) {
84+
$error = $result['errors'][0];
85+
86+
throw new TransportException(sprintf('Unable to send the SMS: %s (%s: "%s").', $message->getSubject(), $error['code'], $error['description']), $response);
87+
}
88+
}
89+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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\Esendex;
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+
* @experimental in 5.1
21+
*/
22+
final class EsendexTransportFactory extends AbstractTransportFactory
23+
{
24+
/**
25+
* @return EsendexTransport
26+
*/
27+
public function create(Dsn $dsn): TransportInterface
28+
{
29+
$scheme = $dsn->getScheme();
30+
$token = $this->getUser($dsn).':'.$this->getPassword($dsn);
31+
$accountReference = $dsn->getOption('accountreference');
32+
$from = $dsn->getOption('from');
33+
$host = 'default' === $dsn->getHost() ? null : $dsn->getHost();
34+
$port = $dsn->getPort();
35+
36+
if ('esendex' === $scheme) {
37+
return (new EsendexTransport($token, $accountReference, $from, $this->client, $this->dispatcher))->setHost($host)->setPort($port);
38+
}
39+
40+
throw new UnsupportedSchemeException($dsn, 'esendex', $this->getSupportedSchemes());
41+
}
42+
43+
protected function getSupportedSchemes(): array
44+
{
45+
return ['esendex'];
46+
}
47+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
Copyright (c) 2019-2020 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: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
Esendex Notifier
2+
================
3+
4+
Provides Esendex integration for Symfony Notifier.
5+
6+
Resources
7+
---------
8+
9+
* [Contributing](https://symfony.com/doc/current/contributing/index.html)
10+
* [Report issues](https://github.com/symfony/symfony/issues) and
11+
[send Pull Requests](https://github.com/symfony/symfony/pulls)
12+
in the [main Symfony repository](https://github.com/symfony/symfony)
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
{
2+
"name": "symfony/esendex-notifier",
3+
"type": "symfony-bridge",
4+
"description": "Symfony Esendex Notifier Bridge",
5+
"keywords": ["sms", "esendex", "notifier"],
6+
"homepage": "https://symfony.com",
7+
"license": "MIT",
8+
"authors": [
9+
{
10+
"name": "Fabien Potencier",
11+
"email": "fabien@symfony.com"
12+
},
13+
{
14+
"name": "Symfony Community",
15+
"homepage": "https://symfony.com/contributors"
16+
}
17+
],
18+
"require": {
19+
"php": "^7.2.5",
20+
"symfony/http-client": "^4.3|^5.0",
21+
"symfony/notifier": "^5.0"
22+
},
23+
"autoload": {
24+
"psr-4": { "Symfony\\Component\\Notifier\\Bridge\\Esendex\\": "" },
25+
"exclude-from-classmap": [
26+
"/Tests/"
27+
]
28+
},
29+
"minimum-stability": "dev",
30+
"extra": {
31+
"branch-alias": {
32+
"dev-master": "5.1-dev"
33+
}
34+
}
35+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/5.2/phpunit.xsd"
5+
backupGlobals="false"
6+
colors="true"
7+
bootstrap="vendor/autoload.php"
8+
failOnRisky="true"
9+
failOnWarning="true"
10+
>
11+
<php>
12+
<ini name="error_reporting" value="-1" />
13+
</php>
14+
15+
<testsuites>
16+
<testsuite name="Symfony Esendex Notifier Bridge Test Suite">
17+
<directory>./Tests/</directory>
18+
</testsuite>
19+
</testsuites>
20+
21+
<filter>
22+
<whitelist>
23+
<directory>./</directory>
24+
<exclude>
25+
<directory>./Resources</directory>
26+
<directory>./Tests</directory>
27+
<directory>./vendor</directory>
28+
</exclude>
29+
</whitelist>
30+
</filter>
31+
</phpunit>

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy