Skip to content

Commit 345eb75

Browse files
committed
feature #40647 [Notifier] [FakeChat] Added the bridge (OskarStark)
This PR was merged into the 5.3-dev branch. Discussion ---------- [Notifier] [FakeChat] Added the bridge | Q | A | ------------- | --- | Branch? | 5.x | Bug fix? | no | New feature? | yes | Deprecations? | no | Tickets | | License | MIT | Doc PR | symfony/symfony-docs#15171 | Recipe PR | todo This PR is based on #39949 but for chat messages instead of SMS. Commits ------- 0ce156c [Notifier] [FakeChat] Added the bridge
2 parents 71a407d + 0ce156c commit 345eb75

13 files changed

+375
-0
lines changed

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

+2
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\FakeChat\FakeChatTransportFactory;
116117
use Symfony\Component\Notifier\Bridge\FakeSms\FakeSmsTransportFactory;
117118
use Symfony\Component\Notifier\Bridge\Firebase\FirebaseTransportFactory;
118119
use Symfony\Component\Notifier\Bridge\FreeMobile\FreeMobileTransportFactory;
@@ -2373,6 +2374,7 @@ private function registerNotifierConfiguration(array $config, ContainerBuilder $
23732374
FirebaseTransportFactory::class => 'notifier.transport_factory.firebase',
23742375
FreeMobileTransportFactory::class => 'notifier.transport_factory.freemobile',
23752376
SpotHitTransportFactory::class => 'notifier.transport_factory.spothit',
2377+
FakeChatTransportFactory::class => 'notifier.transport_factory.fakechat',
23762378
FakeSmsTransportFactory::class => 'notifier.transport_factory.fakesms',
23772379
OvhCloudTransportFactory::class => 'notifier.transport_factory.ovhcloud',
23782380
SinchTransportFactory::class => 'notifier.transport_factory.sinch',

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

+5
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\FakeChat\FakeChatTransportFactory;
1819
use Symfony\Component\Notifier\Bridge\FakeSms\FakeSmsTransportFactory;
1920
use Symfony\Component\Notifier\Bridge\Firebase\FirebaseTransportFactory;
2021
use Symfony\Component\Notifier\Bridge\FreeMobile\FreeMobileTransportFactory;
@@ -97,6 +98,10 @@
9798
->parent('notifier.transport_factory.abstract')
9899
->tag('texter.transport_factory')
99100

101+
->set('notifier.transport_factory.fakechat', FakeChatTransportFactory::class)
102+
->parent('notifier.transport_factory.abstract')
103+
->tag('chatter.transport_factory')
104+
100105
->set('notifier.transport_factory.fakesms', FakeSmsTransportFactory::class)
101106
->parent('notifier.transport_factory.abstract')
102107
->tag('texter.transport_factory')
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
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
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 <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\FakeChat;
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\ChatMessage;
20+
use Symfony\Component\Notifier\Message\MessageInterface;
21+
use Symfony\Component\Notifier\Message\SentMessage;
22+
use Symfony\Component\Notifier\Transport\AbstractTransport;
23+
use Symfony\Contracts\HttpClient\HttpClientInterface;
24+
25+
/**
26+
* @author Oskar Stark <oskarstark@googlemail.com>
27+
*/
28+
final class FakeChatEmailTransport 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('fakechat+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 ChatMessage;
51+
}
52+
53+
/**
54+
* @param MessageInterface|ChatMessage $message
55+
*
56+
* @throws TransportExceptionInterface
57+
*/
58+
protected function doSend(MessageInterface $message): SentMessage
59+
{
60+
if (!$this->supports($message)) {
61+
throw new UnsupportedMessageTypeException(__CLASS__, ChatMessage::class, $message);
62+
}
63+
64+
$email = (new Email())
65+
->from($this->from)
66+
->to($this->to)
67+
->subject(sprintf('New Chat message for recipient: %s', $message->getRecipientId()))
68+
->html($message->getSubject())
69+
->text($message->getSubject());
70+
71+
$this->mailer->send($email);
72+
73+
return new SentMessage($message, (string) $this);
74+
}
75+
}
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 <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\FakeChat;
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 Oskar Stark <oskarstark@googlemail.com>
22+
*/
23+
final class FakeChatTransportFactory 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 FakeChatEmailTransport
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, 'fakechat', $this->getSupportedSchemes());
43+
}
44+
45+
if ('fakechat+email' === $scheme) {
46+
$serviceId = $dsn->getHost();
47+
$to = $dsn->getRequiredOption('to');
48+
$from = $dsn->getRequiredOption('from');
49+
50+
return (new FakeChatEmailTransport($this->serviceProvider->get($serviceId), $to, $from))->setHost($serviceId);
51+
}
52+
}
53+
54+
protected function getSupportedSchemes(): array
55+
{
56+
return ['fakechat+email'];
57+
}
58+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
Copyright (c) 2021 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.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
Fake Chat Notifier
2+
==================
3+
4+
Provides Fake Chat (as email during development) integration for Symfony Notifier.
5+
6+
#### DSN example
7+
8+
```
9+
FAKE_CHAT_DSN=fakechat+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 Chat message during development
15+
- `FROM` is email who send Chat message 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)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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\FakeChat\Tests;
13+
14+
use Symfony\Component\Mailer\MailerInterface;
15+
use Symfony\Component\Notifier\Bridge\FakeChat\FakeChatEmailTransport;
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\Test\TransportTestCase;
20+
use Symfony\Component\Notifier\Transport\TransportInterface;
21+
use Symfony\Contracts\HttpClient\HttpClientInterface;
22+
23+
/**
24+
* @author Oskar Stark <oskarstark@googlemail.com>
25+
*/
26+
final class FakeChatEmailTransportTest extends TransportTestCase
27+
{
28+
public function createTransport(?HttpClientInterface $client = null): TransportInterface
29+
{
30+
return (new FakeChatEmailTransport($this->createMock(MailerInterface::class), 'recipient@email.net', 'from@email.net'))->setHost('mailer');
31+
}
32+
33+
public function toStringProvider(): iterable
34+
{
35+
yield ['fakechat+email://mailer?to=recipient@email.net&from=from@email.net', $this->createTransport()];
36+
}
37+
38+
public function supportedMessagesProvider(): iterable
39+
{
40+
yield [new ChatMessage('Hello!')];
41+
}
42+
43+
public function unsupportedMessagesProvider(): iterable
44+
{
45+
yield [new SmsMessage('0611223344', 'Hello!')];
46+
yield [$this->createMock(MessageInterface::class)];
47+
}
48+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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\FakeChat\Tests;
13+
14+
use Symfony\Component\Mailer\MailerInterface;
15+
use Symfony\Component\Notifier\Bridge\FakeChat\FakeChatTransportFactory;
16+
use Symfony\Component\Notifier\Test\TransportFactoryTestCase;
17+
use Symfony\Component\Notifier\Transport\TransportFactoryInterface;
18+
use Symfony\Contracts\Service\ServiceProviderInterface;
19+
20+
/**
21+
* @author Oskar Stark <oskarstark@googlemail.com>
22+
*/
23+
final class FakeChatTransportFactoryTest extends TransportFactoryTestCase
24+
{
25+
/**
26+
* @return FakeChatTransportFactory
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 FakeChatTransportFactory($serviceProvider);
35+
}
36+
37+
public function createProvider(): iterable
38+
{
39+
yield [
40+
'fakechat+email://mailer?to=recipient@email.net&from=sender@email.net',
41+
'fakechat+email://mailer?to=recipient@email.net&from=sender@email.net',
42+
];
43+
}
44+
45+
public function missingRequiredOptionProvider(): iterable
46+
{
47+
yield 'missing option: from' => ['fakechat+email://mailer?to=recipient@email.net'];
48+
yield 'missing option: to' => ['fakechat+email://mailer?from=sender@email.net'];
49+
}
50+
51+
public function supportsProvider(): iterable
52+
{
53+
yield [true, 'fakechat+email://mailer?to=recipient@email.net&from=sender@email.net'];
54+
yield [false, 'somethingElse://mailer?to=recipient@email.net&from=sender@email.net'];
55+
}
56+
57+
public function incompleteDsnProvider(): iterable
58+
{
59+
yield 'missing from' => ['fakechat+email://mailer?to=recipient@email.net'];
60+
yield 'missing to' => ['fakechat+email://mailer?from=recipient@email.net'];
61+
}
62+
63+
public function unsupportedSchemeProvider(): iterable
64+
{
65+
yield ['somethingElse://mailer?to=recipient@email.net&from=sender@email.net'];
66+
}
67+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
{
2+
"name": "symfony/fake-chat-notifier",
3+
"type": "symfony-bridge",
4+
"description": "Fake Chat (as email during development) Notifier Bridge.",
5+
"keywords": ["chat", "development", "email", "notifier", "symfony"],
6+
"homepage": "https://symfony.com",
7+
"license": "MIT",
8+
"authors": [
9+
{
10+
"name": "Oskar Stark",
11+
"homepage": "https://github.com/OskarStark"
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\\FakeChat\\": "" },
27+
"exclude-from-classmap": [
28+
"/Tests/"
29+
]
30+
},
31+
"minimum-stability": "dev"
32+
}

0 commit comments

Comments
 (0)