Skip to content

Commit 1b8709e

Browse files
Add Free Mobile notifier
1 parent e464954 commit 1b8709e

File tree

14 files changed

+370
-0
lines changed

14 files changed

+370
-0
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@
9292
use Symfony\Component\Mime\MimeTypeGuesserInterface;
9393
use Symfony\Component\Mime\MimeTypes;
9494
use Symfony\Component\Notifier\Bridge\Firebase\FirebaseTransportFactory;
95+
use Symfony\Component\Notifier\Bridge\FreeMobile\FreeMobileTransportFactory;
9596
use Symfony\Component\Notifier\Bridge\Mattermost\MattermostTransportFactory;
9697
use Symfony\Component\Notifier\Bridge\Nexmo\NexmoTransportFactory;
9798
use Symfony\Component\Notifier\Bridge\OvhCloud\OvhCloudTransportFactory;
@@ -2044,6 +2045,7 @@ private function registerNotifierConfiguration(array $config, ContainerBuilder $
20442045
RocketChatTransportFactory::class => 'notifier.transport_factory.rocketchat',
20452046
TwilioTransportFactory::class => 'notifier.transport_factory.twilio',
20462047
FirebaseTransportFactory::class => 'notifier.transport_factory.firebase',
2048+
FreeMobileTransportFactory::class => 'notifier.transport_factory.freemobile',
20472049
OvhCloudTransportFactory::class => 'notifier.transport_factory.ovhcloud',
20482050
SinchTransportFactory::class => 'notifier.transport_factory.sinch',
20492051
];

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@
3838
<tag name="texter.transport_factory" />
3939
</service>
4040

41+
<service id="notifier.transport_factory.freemobile" class="Symfony\Component\Notifier\Bridge\FreeMobile\FreeMobileTransportFactory" parent="notifier.transport_factory.abstract">
42+
<tag name="texter.transport_factory" />
43+
</service>
44+
4145
<service id="notifier.transport_factory.ovhcloud" class="Symfony\Component\Notifier\Bridge\OvhCloud\OvhCloudTransportFactory" parent="notifier.transport_factory.abstract">
4246
<tag name="texter.transport_factory" />
4347
</service>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
/Tests export-ignore
2+
/phpunit.xml.dist export-ignore
3+
/.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.1.0
5+
-----
6+
7+
* Added the bridge as `@experimental`
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
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\FreeMobile;
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+
* @author Antoine Makdessi <amakdessi@me.com>
24+
*
25+
* @experimental in 5.1
26+
*/
27+
final class FreeMobileTransport extends AbstractTransport
28+
{
29+
protected const HOST = 'https://smsapi.free-mobile.fr/sendmsg';
30+
31+
private $login;
32+
private $password;
33+
private $phone;
34+
35+
public function __construct(string $login, string $password, string $phone, HttpClientInterface $client = null, EventDispatcherInterface $dispatcher = null)
36+
{
37+
$this->login = $login;
38+
$this->password = $password;
39+
$this->phone = $phone;
40+
41+
parent::__construct($client, $dispatcher);
42+
}
43+
44+
public function __toString(): string
45+
{
46+
return sprintf('freemobile://%s?phone=%s', $this->getEndpoint(), $this->phone);
47+
}
48+
49+
public function supports(MessageInterface $message): bool
50+
{
51+
return $message instanceof SmsMessage && $this->phone === $message->getPhone();
52+
}
53+
54+
protected function doSend(MessageInterface $message): void
55+
{
56+
if (!$this->supports($message)) {
57+
throw new LogicException(sprintf('The "%s" transport only supports instances of "%s" (instance of "%s" given) and configured with your phone number.', __CLASS__, SmsMessage::class, \get_class($message)));
58+
}
59+
60+
$response = $this->client->request('POST', $this->getEndpoint(), [
61+
'json' => [
62+
'user' => $this->login,
63+
'pass' => $this->password,
64+
'msg' => $message->getSubject(),
65+
],
66+
]);
67+
68+
if (200 !== $response->getStatusCode()) {
69+
$error = $response->toArray(false);
70+
71+
throw new TransportException(sprintf('Unable to send the SMS: "%s" (see "%s").', $error['message'], $error['more_info']), $response);
72+
}
73+
}
74+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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\FreeMobile;
13+
14+
use Symfony\Component\Notifier\Exception\IncompleteDsnException;
15+
use Symfony\Component\Notifier\Exception\UnsupportedSchemeException;
16+
use Symfony\Component\Notifier\Transport\AbstractTransportFactory;
17+
use Symfony\Component\Notifier\Transport\Dsn;
18+
use Symfony\Component\Notifier\Transport\TransportInterface;
19+
20+
/**
21+
* @author Antoine Makdessi <amakdessi@me.com>
22+
*
23+
* @experimental in 5.1
24+
*/
25+
final class FreeMobileTransportFactory extends AbstractTransportFactory
26+
{
27+
/**
28+
* @return FreeMobileTransport
29+
*/
30+
public function create(Dsn $dsn): TransportInterface
31+
{
32+
$scheme = $dsn->getScheme();
33+
$login = $this->getUser($dsn);
34+
$password = $this->getPassword($dsn);
35+
$phone = $dsn->getOption('phone');
36+
37+
if (null === $phone || '' === $phone) {
38+
throw new IncompleteDsnException('Missing phone.');
39+
}
40+
41+
if ('freemobile' === $scheme) {
42+
return new FreeMobileTransport($login, $password, $phone, $this->client, $this->dispatcher);
43+
}
44+
45+
throw new UnsupportedSchemeException($dsn, 'freemobile', $this->getSupportedSchemes());
46+
}
47+
48+
protected function getSupportedSchemes(): array
49+
{
50+
return ['freemobile'];
51+
}
52+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
Copyright (c) 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: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
Free Mobile Notifier
2+
====================
3+
4+
Provides Free Mobile integration for Symfony Notifier.
5+
This provider allows you to receive an SMS notification
6+
on your personal mobile number.
7+
8+
Resources
9+
---------
10+
11+
* [Contributing](https://symfony.com/doc/current/contributing/index.html)
12+
* [Report issues](https://github.com/symfony/symfony/issues) and
13+
[send Pull Requests](https://github.com/symfony/symfony/pulls)
14+
in the [main Symfony repository](https://github.com/symfony/symfony)
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
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\FreeMobile\Tests;
13+
14+
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\Notifier\Bridge\FreeMobile\FreeMobileTransportFactory;
16+
use Symfony\Component\Notifier\Exception\IncompleteDsnException;
17+
use Symfony\Component\Notifier\Exception\UnsupportedSchemeException;
18+
use Symfony\Component\Notifier\Transport\Dsn;
19+
20+
final class FreeMobileTransportFactoryTest extends TestCase
21+
{
22+
public function testCreateWithDsn(): void
23+
{
24+
$factory = $this->initFactory();
25+
26+
$dsn = 'freemobile://login:pass@default?phone=0611223344';
27+
$transport = $factory->create(Dsn::fromString($dsn));
28+
$transport->setHost('host.test');
29+
30+
$this->assertSame('freemobile://host.test?phone=0611223344', (string) $transport);
31+
}
32+
33+
public function testCreateWithNoPhoneThrowsMalformed(): void
34+
{
35+
$factory = $this->initFactory();
36+
37+
$this->expectException(IncompleteDsnException::class);
38+
39+
$dsnIncomplete = 'freemobile://login:pass@default';
40+
$factory->create(Dsn::fromString($dsnIncomplete));
41+
}
42+
43+
public function testSupportsFreeMobileScheme(): void
44+
{
45+
$factory = $this->initFactory();
46+
47+
$dsn = 'freemobile://login:pass@default?phone=0611223344';
48+
$dsnUnsupported = 'foobarmobile://login:pass@default?phone=0611223344';
49+
50+
$this->assertTrue($factory->supports(Dsn::fromString($dsn)));
51+
$this->assertFalse($factory->supports(Dsn::fromString($dsnUnsupported)));
52+
}
53+
54+
public function testNonFreeMobileSchemeThrows(): void
55+
{
56+
$factory = $this->initFactory();
57+
58+
$this->expectException(UnsupportedSchemeException::class);
59+
60+
$dsnUnsupported = 'foobarmobile://login:pass@default?phone=0611223344';
61+
$factory->create(Dsn::fromString($dsnUnsupported));
62+
}
63+
64+
private function initFactory(): FreeMobileTransportFactory
65+
{
66+
return new FreeMobileTransportFactory();
67+
}
68+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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\FreeMobile\Tests;
13+
14+
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\Notifier\Bridge\FreeMobile\FreeMobileTransport;
16+
use Symfony\Component\Notifier\Exception\LogicException;
17+
use Symfony\Component\Notifier\Message\MessageInterface;
18+
use Symfony\Component\Notifier\Message\SmsMessage;
19+
use Symfony\Contracts\HttpClient\HttpClientInterface;
20+
21+
final class FreeMobileTransportTest extends TestCase
22+
{
23+
public function testToStringContainsProperties(): void
24+
{
25+
$transport = $this->initTransport();
26+
27+
$this->assertSame('freemobile://host.test?phone=0611223344', (string) $transport);
28+
}
29+
30+
public function testSupportsMessageInterface(): void
31+
{
32+
$transport = $this->initTransport();
33+
34+
$this->assertTrue($transport->supports(new SmsMessage('0611223344', 'Hello!')));
35+
$this->assertFalse($transport->supports(new SmsMessage('0699887766', 'Hello!')));
36+
$this->assertFalse($transport->supports($this->createMock(MessageInterface::class), 'Hello!'));
37+
}
38+
39+
public function testSendNonSmsMessageThrowsException(): void
40+
{
41+
$transport = $this->initTransport();
42+
43+
$this->expectException(LogicException::class);
44+
45+
$transport->send(new SmsMessage('0699887766', 'Hello!'));
46+
}
47+
48+
private function initTransport(): FreeMobileTransport
49+
{
50+
return (new FreeMobileTransport(
51+
'login', 'pass', '0611223344', $this->createMock(HttpClientInterface::class)
52+
))->setHost('host.test');
53+
}
54+
}

0 commit comments

Comments
 (0)