Skip to content

Commit 2a989c6

Browse files
committed
fix directory casing
0 parents  commit 2a989c6

12 files changed

+496
-0
lines changed

.gitattributes

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

.gitignore

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

CHANGELOG.md

Lines 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

GoIpOptions.php

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 <[email protected]>
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 <[email protected]>
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+
}

GoIpTransport.php

Lines 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 <[email protected]>
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 <[email protected]>
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+
}

GoIpTransportFactory.php

Lines 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 <[email protected]>
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 <[email protected]>
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+
}

LICENSE

Lines 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.

README.md

Lines 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)

Tests/GoIpTransportFactoryTest.php

Lines 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 <[email protected]>
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 <[email protected]>
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:[email protected]:9000?sim_slot=31',
27+
];
28+
}
29+
30+
public static function supportsProvider(): iterable
31+
{
32+
yield [true, 'goip://root:[email protected]:9000?sim_slot=31'];
33+
yield [false, 'somethingElse://root:[email protected]:9000?sim_slot=31'];
34+
}
35+
36+
public static function unsupportedSchemeProvider(): iterable
37+
{
38+
yield ['somethingElse://user:[email protected]?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:[email protected]'];
49+
}
50+
51+
public function createFactory(): GoIpTransportFactory
52+
{
53+
return new GoIpTransportFactory();
54+
}
55+
}

0 commit comments

Comments
 (0)