-
-
Notifications
You must be signed in to change notification settings - Fork 9.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d6d233b
commit 066d0f8
Showing
17 changed files
with
507 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
/Tests export-ignore | ||
/phpunit.xml.dist export-ignore | ||
/.gitattributes export-ignore | ||
/.gitignore export-ignore |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
vendor/ | ||
composer.lock | ||
phpunit.xml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
CHANGELOG | ||
========= | ||
|
||
6.4 | ||
--- | ||
|
||
* Add the bridge |
47 changes: 47 additions & 0 deletions
47
src/Symfony/Component/Notifier/Bridge/GoIP/GoIPOptions.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Notifier\Bridge\GoIP; | ||
|
||
use Symfony\Component\Notifier\Message\MessageOptionsInterface; | ||
|
||
/** | ||
* @author Ahmed Ghanem <ahmedghanem7361@gmail.com> | ||
*/ | ||
final class GoIPOptions implements MessageOptionsInterface | ||
{ | ||
private array $options = []; | ||
|
||
public function toArray(): array | ||
{ | ||
return $this->options; | ||
} | ||
|
||
public function getRecipientId(): ?string | ||
{ | ||
return null; | ||
} | ||
|
||
/** | ||
* @return $this | ||
*/ | ||
public function setSimSlot(int $simSlot): static | ||
{ | ||
$this->options['simSlot'] = $simSlot; | ||
|
||
return $this; | ||
} | ||
|
||
public function getSimSlot(): ?int | ||
{ | ||
return $this->options['simSlot'] ?? null; | ||
} | ||
} |
114 changes: 114 additions & 0 deletions
114
src/Symfony/Component/Notifier/Bridge/GoIP/GoIPTransport.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Notifier\Bridge\GoIP; | ||
|
||
use Symfony\Component\Notifier\Exception\LogicException; | ||
use Symfony\Component\Notifier\Exception\TransportException; | ||
use Symfony\Component\Notifier\Exception\UnsupportedMessageTypeException; | ||
use Symfony\Component\Notifier\Message\MessageInterface; | ||
use Symfony\Component\Notifier\Message\SentMessage; | ||
use Symfony\Component\Notifier\Message\SmsMessage; | ||
use Symfony\Component\Notifier\Transport\AbstractTransport; | ||
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface; | ||
use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface; | ||
use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface; | ||
use Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface; | ||
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface; | ||
use Symfony\Contracts\HttpClient\HttpClientInterface; | ||
|
||
/** | ||
* @author Ahmed Ghanem <ahmedghanem7361@gmail.com> | ||
*/ | ||
final class GoIPTransport extends AbstractTransport | ||
{ | ||
public function __construct( | ||
private readonly string $username, | ||
#[\SensitiveParameter] | ||
private readonly string $password, | ||
private readonly int $simSlot, | ||
HttpClientInterface $client = null, | ||
EventDispatcherInterface $dispatcher = null | ||
) { | ||
parent::__construct($client, $dispatcher); | ||
} | ||
|
||
public function __toString(): string | ||
{ | ||
return sprintf('goip://%s?sim_slot=%s', $this->getEndpoint(), $this->simSlot); | ||
} | ||
|
||
public function supports(MessageInterface $message): bool | ||
{ | ||
return $message instanceof SmsMessage && (null === $message->getOptions() || $message->getOptions() instanceof GoIPOptions); | ||
} | ||
|
||
/** | ||
* @throws TransportExceptionInterface | ||
* @throws ServerExceptionInterface | ||
* @throws RedirectionExceptionInterface | ||
* @throws ClientExceptionInterface | ||
*/ | ||
protected function doSend(MessageInterface $message): SentMessage | ||
{ | ||
if (!$message instanceof SmsMessage) { | ||
throw new UnsupportedMessageTypeException(__CLASS__, SmsMessage::class, $message); | ||
} | ||
|
||
if (($options = $message->getOptions()) && !$options instanceof GoIPOptions) { | ||
throw new LogicException(sprintf('The "%s" transport only supports an instance of the "%s" as an option class.', __CLASS__, GoIPOptions::class)); | ||
} | ||
|
||
if ('' !== $message->getFrom()) { | ||
throw new LogicException(sprintf('The "%s" transport does not support the "From" option.', __CLASS__)); | ||
} | ||
|
||
$response = $this->client->request('GET', $this->getEndpoint(), [ | ||
'query' => [ | ||
'u' => $this->username, | ||
'p' => $this->password, | ||
'l' => $options?->getSimSlot() ?? $this->simSlot, | ||
'n' => $message->getPhone(), | ||
'm' => $message->getSubject(), | ||
], | ||
]); | ||
|
||
try { | ||
$statusCode = $response->getStatusCode(); | ||
} catch (TransportExceptionInterface $e) { | ||
throw new TransportException('Could not reach the GoIP gateway.', $response, 0, $e); | ||
} | ||
|
||
if (200 !== $statusCode) { | ||
throw new TransportException(sprintf('The GoIP gateway has responded with a wrong http_code: "%s" on the address: "%s".', $statusCode, $this->getEndpoint()), $response); | ||
} | ||
|
||
if (str_contains(strtolower($response->getContent()), 'error') || !str_contains(strtolower($response->getContent()), 'sending')) { | ||
throw new TransportException(sprintf('Could not send the message through GoIP. Response: "%s".', $response->getContent()), $response); | ||
} | ||
|
||
if (!$messageId = $this->extractMessageIdFromContent($response->getContent())) { | ||
throw new TransportException(sprintf('Could not extract the message id from the GoIP response: "%s".', $response->getContent()), $response); | ||
} | ||
|
||
$sentMessage = new SentMessage($message, (string) $this); | ||
$sentMessage->setMessageId($messageId); | ||
|
||
return $sentMessage; | ||
} | ||
|
||
private function extractMessageIdFromContent(string $content): string|bool | ||
{ | ||
preg_match('/; ID:(.*?)$/i', trim($content), $result); | ||
|
||
return $result[1] ?? false; | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
src/Symfony/Component/Notifier/Bridge/GoIP/GoIPTransportFactory.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Notifier\Bridge\GoIP; | ||
|
||
use Symfony\Component\Notifier\Exception\InvalidArgumentException; | ||
use Symfony\Component\Notifier\Exception\UnsupportedSchemeException; | ||
use Symfony\Component\Notifier\Transport\AbstractTransportFactory; | ||
use Symfony\Component\Notifier\Transport\Dsn; | ||
|
||
/** | ||
* @author Ahmed Ghanem <ahmedghanem7361@gmail.com> | ||
*/ | ||
final class GoIPTransportFactory extends AbstractTransportFactory | ||
{ | ||
private const SCHEME_NAME = 'goip'; | ||
|
||
public function create(Dsn $dsn): GoIPTransport | ||
{ | ||
if (self::SCHEME_NAME !== $dsn->getScheme()) { | ||
throw new UnsupportedSchemeException($dsn, self::SCHEME_NAME, $this->getSupportedSchemes()); | ||
} | ||
|
||
$username = $this->getUser($dsn); | ||
$password = $this->getPassword($dsn); | ||
|
||
if (0 === ($simSlot = (int) $dsn->getRequiredOption('sim_slot'))) { | ||
throw new InvalidArgumentException(sprintf('The provided SIM-Slot: "%s" is not valid.', $simSlot)); | ||
} | ||
|
||
return (new GoIPTransport($username, $password, $simSlot, $this->client, $this->dispatcher)) | ||
->setHost($dsn->getHost()) | ||
->setPort($dsn->getPort()); | ||
} | ||
|
||
protected function getSupportedSchemes(): array | ||
{ | ||
return [ | ||
self::SCHEME_NAME, | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
Copyright (c) 2023-present Fabien Potencier | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is furnished | ||
to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
THE SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
GoIP Notifier | ||
============= | ||
|
||
Provides a [GoIP](https://en.wikipedia.org/wiki/GoIP) integration for the | ||
Symfony | ||
Notifier Component. | ||
|
||
DSN example | ||
----------- | ||
|
||
``` | ||
GOIP_DSN=goip://USERNAME:PASSWORD@HOST:80?sim_slot=SIM_SLOT | ||
``` | ||
|
||
where: | ||
|
||
- `USERNAME` GoIP Username | ||
- `PASSWORD` GoIP Password | ||
- `HOST` GoIP Hostname/IP-Address | ||
- `SIM_SLOT` SIM slot which will be used to send the messages (e.g. 1, | ||
9, 15, 2) | ||
|
||
Resources | ||
--------- | ||
|
||
* [Contributing](https://symfony.com/doc/current/contributing/index.html) | ||
* [Report issues](https://github.com/symfony/symfony/issues) and | ||
[send Pull Requests](https://github.com/symfony/symfony/pulls) | ||
in the [main Symfony repository](https://github.com/symfony/symfony) |
55 changes: 55 additions & 0 deletions
55
src/Symfony/Component/Notifier/Bridge/GoIP/Tests/GoIPTransportFactoryTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Notifier\Bridge\GoIP\Tests; | ||
|
||
use Symfony\Component\Notifier\Bridge\GoIP\GoIPTransportFactory; | ||
use Symfony\Component\Notifier\Test\TransportFactoryTestCase; | ||
|
||
/** | ||
* @author Ahmed Ghanem <ahmedghanem7361@gmail.com> | ||
*/ | ||
final class GoIPTransportFactoryTest extends TransportFactoryTestCase | ||
{ | ||
public static function createProvider(): iterable | ||
{ | ||
yield [ | ||
'goip://host.test:9000?sim_slot=31', | ||
'goip://user:pass@host.test:9000?sim_slot=31', | ||
]; | ||
} | ||
|
||
public static function supportsProvider(): iterable | ||
{ | ||
yield [true, 'goip://root:root@host.test:9000?sim_slot=31']; | ||
yield [false, 'somethingElse://root:root@host.test:9000?sim_slot=31']; | ||
} | ||
|
||
public static function unsupportedSchemeProvider(): iterable | ||
{ | ||
yield ['somethingElse://user:pass@host.test?sim_slot=2']; | ||
} | ||
|
||
public static function incompleteDsnProvider(): iterable | ||
{ | ||
yield 'missing username or password' => ['goip://host.test?sim_slot=4']; | ||
} | ||
|
||
public static function missingRequiredOptionProvider(): iterable | ||
{ | ||
yield 'missing required option: sim_slot' => ['goip://user:pass@host.test']; | ||
} | ||
|
||
public function createFactory(): GoIPTransportFactory | ||
{ | ||
return new GoIPTransportFactory(); | ||
} | ||
} |
Oops, something went wrong.