Skip to content

Navigation Menu

Sign in
Appearance settings

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 066d0f8

Browse filesBrowse files
ahmedghanem00fabpot
authored andcommitted
[Notifier] Add GoIP bridge
1 parent d6d233b commit 066d0f8
Copy full SHA for 066d0f8

File tree

Expand file treeCollapse file tree

17 files changed

+507
-0
lines changed
Filter options
Expand file treeCollapse file tree

17 files changed

+507
-0
lines changed

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

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2799,6 +2799,7 @@ private function registerNotifierConfiguration(array $config, ContainerBuilder $
27992799
NotifierBridge\FreeMobile\FreeMobileTransportFactory::class => 'notifier.transport_factory.free-mobile',
28002800
NotifierBridge\GatewayApi\GatewayApiTransportFactory::class => 'notifier.transport_factory.gateway-api',
28012801
NotifierBridge\Gitter\GitterTransportFactory::class => 'notifier.transport_factory.gitter',
2802+
NotifierBridge\GoIP\GoIPTransportFactory::class => 'notifier.transport_factory.goip',
28022803
NotifierBridge\GoogleChat\GoogleChatTransportFactory::class => 'notifier.transport_factory.google-chat',
28032804
NotifierBridge\Infobip\InfobipTransportFactory::class => 'notifier.transport_factory.infobip',
28042805
NotifierBridge\Iqsms\IqsmsTransportFactory::class => 'notifier.transport_factory.iqsms',

‎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
+3Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,5 +303,8 @@
303303
->set('notifier.transport_factory.redlink', Bridge\Redlink\RedlinkTransportFactory::class)
304304
->parent('notifier.transport_factory.abstract')
305305
->tag('texter.transport_factory')
306+
->set('notifier.transport_factory.goip', Bridge\GoIP\GoIPTransportFactory::class)
307+
->parent('notifier.transport_factory.abstract')
308+
->tag('texter.transport_factory')
306309
;
307310
};
+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+
6.4
5+
---
6+
7+
* Add the bridge
+47Lines 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\GoIP;
13+
14+
use Symfony\Component\Notifier\Message\MessageOptionsInterface;
15+
16+
/**
17+
* @author Ahmed Ghanem <ahmedghanem7361@gmail.com>
18+
*/
19+
final class GoIPOptions implements MessageOptionsInterface
20+
{
21+
private array $options = [];
22+
23+
public function toArray(): array
24+
{
25+
return $this->options;
26+
}
27+
28+
public function getRecipientId(): ?string
29+
{
30+
return null;
31+
}
32+
33+
/**
34+
* @return $this
35+
*/
36+
public function setSimSlot(int $simSlot): static
37+
{
38+
$this->options['simSlot'] = $simSlot;
39+
40+
return $this;
41+
}
42+
43+
public function getSimSlot(): ?int
44+
{
45+
return $this->options['simSlot'] ?? null;
46+
}
47+
}
+114Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
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\GoIP;
13+
14+
use Symfony\Component\Notifier\Exception\LogicException;
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\SentMessage;
19+
use Symfony\Component\Notifier\Message\SmsMessage;
20+
use Symfony\Component\Notifier\Transport\AbstractTransport;
21+
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
22+
use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface;
23+
use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface;
24+
use Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface;
25+
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
26+
use Symfony\Contracts\HttpClient\HttpClientInterface;
27+
28+
/**
29+
* @author Ahmed Ghanem <ahmedghanem7361@gmail.com>
30+
*/
31+
final class GoIPTransport extends AbstractTransport
32+
{
33+
public function __construct(
34+
private readonly string $username,
35+
#[\SensitiveParameter]
36+
private readonly string $password,
37+
private readonly int $simSlot,
38+
HttpClientInterface $client = null,
39+
EventDispatcherInterface $dispatcher = null
40+
) {
41+
parent::__construct($client, $dispatcher);
42+
}
43+
44+
public function __toString(): string
45+
{
46+
return sprintf('goip://%s?sim_slot=%s', $this->getEndpoint(), $this->simSlot);
47+
}
48+
49+
public function supports(MessageInterface $message): bool
50+
{
51+
return $message instanceof SmsMessage && (null === $message->getOptions() || $message->getOptions() instanceof GoIPOptions);
52+
}
53+
54+
/**
55+
* @throws TransportExceptionInterface
56+
* @throws ServerExceptionInterface
57+
* @throws RedirectionExceptionInterface
58+
* @throws ClientExceptionInterface
59+
*/
60+
protected function doSend(MessageInterface $message): SentMessage
61+
{
62+
if (!$message instanceof SmsMessage) {
63+
throw new UnsupportedMessageTypeException(__CLASS__, SmsMessage::class, $message);
64+
}
65+
66+
if (($options = $message->getOptions()) && !$options instanceof GoIPOptions) {
67+
throw new LogicException(sprintf('The "%s" transport only supports an instance of the "%s" as an option class.', __CLASS__, GoIPOptions::class));
68+
}
69+
70+
if ('' !== $message->getFrom()) {
71+
throw new LogicException(sprintf('The "%s" transport does not support the "From" option.', __CLASS__));
72+
}
73+
74+
$response = $this->client->request('GET', $this->getEndpoint(), [
75+
'query' => [
76+
'u' => $this->username,
77+
'p' => $this->password,
78+
'l' => $options?->getSimSlot() ?? $this->simSlot,
79+
'n' => $message->getPhone(),
80+
'm' => $message->getSubject(),
81+
],
82+
]);
83+
84+
try {
85+
$statusCode = $response->getStatusCode();
86+
} catch (TransportExceptionInterface $e) {
87+
throw new TransportException('Could not reach the GoIP gateway.', $response, 0, $e);
88+
}
89+
90+
if (200 !== $statusCode) {
91+
throw new TransportException(sprintf('The GoIP gateway has responded with a wrong http_code: "%s" on the address: "%s".', $statusCode, $this->getEndpoint()), $response);
92+
}
93+
94+
if (str_contains(strtolower($response->getContent()), 'error') || !str_contains(strtolower($response->getContent()), 'sending')) {
95+
throw new TransportException(sprintf('Could not send the message through GoIP. Response: "%s".', $response->getContent()), $response);
96+
}
97+
98+
if (!$messageId = $this->extractMessageIdFromContent($response->getContent())) {
99+
throw new TransportException(sprintf('Could not extract the message id from the GoIP response: "%s".', $response->getContent()), $response);
100+
}
101+
102+
$sentMessage = new SentMessage($message, (string) $this);
103+
$sentMessage->setMessageId($messageId);
104+
105+
return $sentMessage;
106+
}
107+
108+
private function extractMessageIdFromContent(string $content): string|bool
109+
{
110+
preg_match('/; ID:(.*?)$/i', trim($content), $result);
111+
112+
return $result[1] ?? false;
113+
}
114+
}
+50Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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\GoIP;
13+
14+
use Symfony\Component\Notifier\Exception\InvalidArgumentException;
15+
use Symfony\Component\Notifier\Exception\UnsupportedSchemeException;
16+
use Symfony\Component\Notifier\Transport\AbstractTransportFactory;
17+
use Symfony\Component\Notifier\Transport\Dsn;
18+
19+
/**
20+
* @author Ahmed Ghanem <ahmedghanem7361@gmail.com>
21+
*/
22+
final class GoIPTransportFactory extends AbstractTransportFactory
23+
{
24+
private const SCHEME_NAME = 'goip';
25+
26+
public function create(Dsn $dsn): GoIPTransport
27+
{
28+
if (self::SCHEME_NAME !== $dsn->getScheme()) {
29+
throw new UnsupportedSchemeException($dsn, self::SCHEME_NAME, $this->getSupportedSchemes());
30+
}
31+
32+
$username = $this->getUser($dsn);
33+
$password = $this->getPassword($dsn);
34+
35+
if (0 === ($simSlot = (int) $dsn->getRequiredOption('sim_slot'))) {
36+
throw new InvalidArgumentException(sprintf('The provided SIM-Slot: "%s" is not valid.', $simSlot));
37+
}
38+
39+
return (new GoIPTransport($username, $password, $simSlot, $this->client, $this->dispatcher))
40+
->setHost($dsn->getHost())
41+
->setPort($dsn->getPort());
42+
}
43+
44+
protected function getSupportedSchemes(): array
45+
{
46+
return [
47+
self::SCHEME_NAME,
48+
];
49+
}
50+
}
+19Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
Copyright (c) 2023-present 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.
+29Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
GoIP Notifier
2+
=============
3+
4+
Provides a [GoIP](https://en.wikipedia.org/wiki/GoIP) integration for the
5+
Symfony
6+
Notifier Component.
7+
8+
DSN example
9+
-----------
10+
11+
```
12+
GOIP_DSN=goip://USERNAME:PASSWORD@HOST:80?sim_slot=SIM_SLOT
13+
```
14+
15+
where:
16+
17+
- `USERNAME` GoIP Username
18+
- `PASSWORD` GoIP Password
19+
- `HOST` GoIP Hostname/IP-Address
20+
- `SIM_SLOT` SIM slot which will be used to send the messages (e.g. 1,
21+
9, 15, 2)
22+
23+
Resources
24+
---------
25+
26+
* [Contributing](https://symfony.com/doc/current/contributing/index.html)
27+
* [Report issues](https://github.com/symfony/symfony/issues) and
28+
[send Pull Requests](https://github.com/symfony/symfony/pulls)
29+
in the [main Symfony repository](https://github.com/symfony/symfony)
+55Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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\GoIP\Tests;
13+
14+
use Symfony\Component\Notifier\Bridge\GoIP\GoIPTransportFactory;
15+
use Symfony\Component\Notifier\Test\TransportFactoryTestCase;
16+
17+
/**
18+
* @author Ahmed Ghanem <ahmedghanem7361@gmail.com>
19+
*/
20+
final class GoIPTransportFactoryTest extends TransportFactoryTestCase
21+
{
22+
public static function createProvider(): iterable
23+
{
24+
yield [
25+
'goip://host.test:9000?sim_slot=31',
26+
'goip://user:pass@host.test:9000?sim_slot=31',
27+
];
28+
}
29+
30+
public static function supportsProvider(): iterable
31+
{
32+
yield [true, 'goip://root:root@host.test:9000?sim_slot=31'];
33+
yield [false, 'somethingElse://root:root@host.test:9000?sim_slot=31'];
34+
}
35+
36+
public static function unsupportedSchemeProvider(): iterable
37+
{
38+
yield ['somethingElse://user:pass@host.test?sim_slot=2'];
39+
}
40+
41+
public static function incompleteDsnProvider(): iterable
42+
{
43+
yield 'missing username or password' => ['goip://host.test?sim_slot=4'];
44+
}
45+
46+
public static function missingRequiredOptionProvider(): iterable
47+
{
48+
yield 'missing required option: sim_slot' => ['goip://user:pass@host.test'];
49+
}
50+
51+
public function createFactory(): GoIPTransportFactory
52+
{
53+
return new GoIPTransportFactory();
54+
}
55+
}

0 commit comments

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