Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c14d189

Browse files
committed
feature #39949 [Notifier] [FakeSms] Add the bridge (JamesHemery)
This PR was squashed before being merged into the 5.3-dev branch. Discussion ---------- [Notifier] [FakeSms] Add the bridge | Q | A | ------------- | --- | Branch? | 5.x | Bug fix? |no | New feature? | yes | Deprecations? | no | License | MIT | Doc PR | symfony/symfony-docs#14870 | Recipe PR | symfony/recipes#882 @OskarStark Bridge added :) Commits ------- 351065e [Notifier] [FakeSms] Add the bridge
2 parents 40a2c19 + 351065e commit c14d189

File tree

11 files changed

+354
-0
lines changed

11 files changed

+354
-0
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@
113113
use Symfony\Component\Notifier\Bridge\Clickatell\ClickatellTransportFactory;
114114
use Symfony\Component\Notifier\Bridge\Discord\DiscordTransportFactory;
115115
use Symfony\Component\Notifier\Bridge\Esendex\EsendexTransportFactory;
116+
use Symfony\Component\Notifier\Bridge\FakeSms\FakeSmsTransportFactory;
116117
use Symfony\Component\Notifier\Bridge\Firebase\FirebaseTransportFactory;
117118
use Symfony\Component\Notifier\Bridge\FreeMobile\FreeMobileTransportFactory;
118119
use Symfony\Component\Notifier\Bridge\GatewayApi\GatewayApiTransportFactory;
@@ -2336,6 +2337,7 @@ private function registerNotifierConfiguration(array $config, ContainerBuilder $
23362337
FirebaseTransportFactory::class => 'notifier.transport_factory.firebase',
23372338
FreeMobileTransportFactory::class => 'notifier.transport_factory.freemobile',
23382339
SpotHitTransportFactory::class => 'notifier.transport_factory.spothit',
2340+
FakeSmsTransportFactory::class => 'notifier.transport_factory.fakesms',
23392341
OvhCloudTransportFactory::class => 'notifier.transport_factory.ovhcloud',
23402342
SinchTransportFactory::class => 'notifier.transport_factory.sinch',
23412343
ZulipTransportFactory::class => 'notifier.transport_factory.zulip',

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Symfony\Component\Notifier\Bridge\Clickatell\ClickatellTransportFactory;
1616
use Symfony\Component\Notifier\Bridge\Discord\DiscordTransportFactory;
1717
use Symfony\Component\Notifier\Bridge\Esendex\EsendexTransportFactory;
18+
use Symfony\Component\Notifier\Bridge\FakeSms\FakeSmsTransportFactory;
1819
use Symfony\Component\Notifier\Bridge\Firebase\FirebaseTransportFactory;
1920
use Symfony\Component\Notifier\Bridge\FreeMobile\FreeMobileTransportFactory;
2021
use Symfony\Component\Notifier\Bridge\GatewayApi\GatewayApiTransportFactory;
@@ -95,6 +96,10 @@
9596
->parent('notifier.transport_factory.abstract')
9697
->tag('texter.transport_factory')
9798

99+
->set('notifier.transport_factory.fakesms', FakeSmsTransportFactory::class)
100+
->parent('notifier.transport_factory.abstract')
101+
->tag('texter.transport_factory')
102+
98103
->set('notifier.transport_factory.ovhcloud', OvhCloudTransportFactory::class)
99104
->parent('notifier.transport_factory.abstract')
100105
->tag('texter.transport_factory')
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
CHANGELOG
2+
=========
3+
4+
5.3
5+
---
6+
7+
* Add the bridge
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
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\FakeSms;
13+
14+
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
15+
use Symfony\Component\Mailer\Exception\TransportExceptionInterface;
16+
use Symfony\Component\Mailer\MailerInterface;
17+
use Symfony\Component\Mime\Email;
18+
use Symfony\Component\Notifier\Exception\UnsupportedMessageTypeException;
19+
use Symfony\Component\Notifier\Message\MessageInterface;
20+
use Symfony\Component\Notifier\Message\SentMessage;
21+
use Symfony\Component\Notifier\Message\SmsMessage;
22+
use Symfony\Component\Notifier\Transport\AbstractTransport;
23+
use Symfony\Contracts\HttpClient\HttpClientInterface;
24+
25+
/**
26+
* @author James Hemery <[email protected]>
27+
*/
28+
final class FakeSmsEmailTransport extends AbstractTransport
29+
{
30+
private $mailer;
31+
private $to;
32+
private $from;
33+
34+
public function __construct(MailerInterface $mailer, string $to, string $from, HttpClientInterface $client = null, EventDispatcherInterface $dispatcher = null)
35+
{
36+
$this->mailer = $mailer;
37+
$this->to = $to;
38+
$this->from = $from;
39+
40+
parent::__construct($client, $dispatcher);
41+
}
42+
43+
public function __toString(): string
44+
{
45+
return sprintf('fakesms+email://%s?to=%s&from=%s', $this->getEndpoint(), $this->to, $this->from);
46+
}
47+
48+
public function supports(MessageInterface $message): bool
49+
{
50+
return $message instanceof SmsMessage;
51+
}
52+
53+
/**
54+
* @param MessageInterface|SmsMessage $message
55+
*
56+
* @throws TransportExceptionInterface
57+
*/
58+
protected function doSend(MessageInterface $message): SentMessage
59+
{
60+
if (!$this->supports($message)) {
61+
throw new UnsupportedMessageTypeException(__CLASS__, SmsMessage::class, $message);
62+
}
63+
64+
$email = (new Email())
65+
->from($this->from)
66+
->to($this->to)
67+
->subject(sprintf('New SMS on phone number: %s', $message->getPhone()))
68+
->html($message->getSubject())
69+
->text($message->getSubject());
70+
71+
$this->mailer->send($email);
72+
73+
return new SentMessage($message, (string) $this);
74+
}
75+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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\FakeSms;
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+
use Symfony\Contracts\Service\ServiceProviderInterface;
19+
20+
/**
21+
* @author James Hemery <[email protected]>
22+
*/
23+
final class FakeSmsTransportFactory extends AbstractTransportFactory
24+
{
25+
protected $serviceProvider;
26+
27+
public function __construct(ServiceProviderInterface $serviceProvider)
28+
{
29+
parent::__construct();
30+
31+
$this->serviceProvider = $serviceProvider;
32+
}
33+
34+
/**
35+
* @return FakeSmsEmailTransport
36+
*/
37+
public function create(Dsn $dsn): TransportInterface
38+
{
39+
$scheme = $dsn->getScheme();
40+
41+
if (!\in_array($scheme, $this->getSupportedSchemes())) {
42+
throw new UnsupportedSchemeException($dsn, 'fakesms', $this->getSupportedSchemes());
43+
}
44+
45+
if ('fakesms+email' === $scheme) {
46+
$serviceId = $dsn->getHost();
47+
$to = $dsn->getRequiredOption('to');
48+
$from = $dsn->getRequiredOption('from');
49+
50+
return (new FakeSmsEmailTransport($this->serviceProvider->get($serviceId), $to, $from))->setHost($serviceId);
51+
}
52+
}
53+
54+
protected function getSupportedSchemes(): array
55+
{
56+
return ['fakesms+email'];
57+
}
58+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
Fake SMS Notifier
2+
=================
3+
4+
Provides Fake SMS (as email during development) integration for Symfony Notifier.
5+
6+
#### DSN example
7+
8+
```
9+
FAKE_SMS_DSN=fakesms+email://MAILER_SERVICE_ID?to=TO&from=FROM
10+
```
11+
12+
where:
13+
- `MAILER_SERVICE_ID` is mailer service id (use `mailer` by default)
14+
- `TO` is email who receive SMS during development
15+
- `FROM` is email who send SMS during development
16+
17+
Resources
18+
---------
19+
20+
* [Contributing](https://symfony.com/doc/current/contributing/index.html)
21+
* [Report issues](https://github.com/symfony/symfony/issues) and
22+
[send Pull Requests](https://github.com/symfony/symfony/pulls)
23+
in the [main Symfony repository](https://github.com/symfony/symfony)
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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\FakeSms\Tests;
13+
14+
use Symfony\Component\Mailer\MailerInterface;
15+
use Symfony\Component\Notifier\Bridge\FakeSms\FakeSmsEmailTransport;
16+
use Symfony\Component\Notifier\Message\ChatMessage;
17+
use Symfony\Component\Notifier\Message\MessageInterface;
18+
use Symfony\Component\Notifier\Message\SmsMessage;
19+
use Symfony\Component\Notifier\Tests\TransportTestCase;
20+
use Symfony\Component\Notifier\Transport\TransportInterface;
21+
use Symfony\Contracts\HttpClient\HttpClientInterface;
22+
23+
/**
24+
* @author James Hemery <[email protected]>
25+
*/
26+
final class FakeSmsEmailTransportTest extends TransportTestCase
27+
{
28+
public function createTransport(?HttpClientInterface $client = null): TransportInterface
29+
{
30+
return (new FakeSmsEmailTransport($this->createMock(MailerInterface::class), '[email protected]', '[email protected]'))->setHost('mailer');
31+
}
32+
33+
public function toStringProvider(): iterable
34+
{
35+
yield ['fakesms+email://[email protected]&[email protected]', $this->createTransport()];
36+
}
37+
38+
public function supportedMessagesProvider(): iterable
39+
{
40+
yield [new SmsMessage('0611223344', 'Hello!')];
41+
yield [new SmsMessage('+33611223344', 'Hello!')];
42+
}
43+
44+
public function unsupportedMessagesProvider(): iterable
45+
{
46+
yield [new ChatMessage('Hello!')];
47+
yield [$this->createMock(MessageInterface::class)];
48+
}
49+
}
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 <[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\FakeSms\Tests;
13+
14+
use Symfony\Component\Mailer\MailerInterface;
15+
use Symfony\Component\Notifier\Bridge\FakeSms\FakeSmsTransportFactory;
16+
use Symfony\Component\Notifier\Tests\TransportFactoryTestCase;
17+
use Symfony\Component\Notifier\Transport\TransportFactoryInterface;
18+
use Symfony\Contracts\Service\ServiceProviderInterface;
19+
20+
/**
21+
* @author James Hemery <[email protected]>
22+
*/
23+
final class FakeSmsTransportFactoryTest extends TransportFactoryTestCase
24+
{
25+
/**
26+
* @return FakeSmsTransportFactory
27+
*/
28+
public function createFactory(): TransportFactoryInterface
29+
{
30+
$serviceProvider = $this->createMock(ServiceProviderInterface::class);
31+
$serviceProvider->method('has')->willReturn(true);
32+
$serviceProvider->method('get')->willReturn($this->createMock(MailerInterface::class));
33+
34+
return new FakeSmsTransportFactory($serviceProvider);
35+
}
36+
37+
public function createProvider(): iterable
38+
{
39+
yield [
40+
41+
42+
];
43+
}
44+
45+
public function missingRequiredOptionProvider(): iterable
46+
{
47+
yield 'missing option: from' => ['fakesms+email://[email protected]'];
48+
yield 'missing option: to' => ['fakesms+email://[email protected]'];
49+
}
50+
51+
public function supportsProvider(): iterable
52+
{
53+
yield [true, 'fakesms+email://[email protected]&[email protected]'];
54+
yield [false, 'somethingElse://api_token@default?from=MyCompany'];
55+
}
56+
57+
public function incompleteDsnProvider(): iterable
58+
{
59+
yield 'missing from' => ['fakesms+email://[email protected]'];
60+
yield 'missing to' => ['fakesms+email://[email protected]'];
61+
}
62+
63+
public function unsupportedSchemeProvider(): iterable
64+
{
65+
yield ['foobar://api_token@default?from=MyCompany'];
66+
yield ['foobar://api_token@default'];
67+
}
68+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
{
2+
"name": "symfony/fake-sms-notifier",
3+
"type": "symfony-bridge",
4+
"description": "Fake SMS (as email during development) Notifier Bridge.",
5+
"keywords": ["sms", "development", "email", "notifier", "symfony"],
6+
"homepage": "https://symfony.com",
7+
"license": "MIT",
8+
"authors": [
9+
{
10+
"name": "James Hemery",
11+
"homepage": "https://github.com/JamesHemery"
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.4|^5.2",
21+
"symfony/notifier": "^5.3",
22+
"symfony/event-dispatcher-contracts": "^2",
23+
"symfony/mailer": "^5.2"
24+
},
25+
"autoload": {
26+
"psr-4": { "Symfony\\Component\\Notifier\\Bridge\\FakeSms\\": "" },
27+
"exclude-from-classmap": [
28+
"/Tests/"
29+
]
30+
},
31+
"minimum-stability": "dev"
32+
}
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 FakeSms 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>

src/Symfony/Component/Notifier/Exception/UnsupportedSchemeException.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,10 @@ class UnsupportedSchemeException extends LogicException
7272
'class' => Bridge\SpotHit\SpotHitTransportFactory::class,
7373
'package' => 'symfony/spot-hit-notifier',
7474
],
75+
'fakesms' => [
76+
'class' => Bridge\FakeSms\FakeSmsTransportFactory::class,
77+
'package' => 'symfony/fake-sms-notifier',
78+
],
7579
'ovhcloud' => [
7680
'class' => Bridge\OvhCloud\OvhCloudTransportFactory::class,
7781
'package' => 'symfony/ovh-cloud-notifier',

0 commit comments

Comments
 (0)