8000 [Notifier] [FakeChat] Added the bridge · symfony/symfony@11e7a96 · GitHub
[go: up one dir, main page]

Skip to content

Commit 11e7a96

Browse files
committed
[Notifier] [FakeChat] Added the bridge
1 parent d7b6805 commit 11e7a96

File tree

11 files changed

+353
-2
lines changed

11 files changed

+353
-2
lines changed

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

Lines changed: 2 additions & 2 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\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;
@@ -188,8 +189,6 @@
188189
use Symfony\Contracts\Translation\LocaleAwareInterface;
189190

190191
/**
191-
* FrameworkExtension.
192-
*
193192
* @author Fabien Potencier <fabien@symfony.com>
194193
* @author Jeremy Mikola <jmikola@gmail.com>
195194
* @author Kévin Dunglas <dunglas@gmail.com>
@@ -2337,6 +2336,7 @@ private function registerNotifierConfiguration(array $config, ContainerBuilder $
23372336
FirebaseTransportFactory::class => 'notifier.transport_factory.firebase',
23382337
FreeMobileTransportFactory::class => 'notifier.transport_factory.freemobile',
23392338
SpotHitTransportFactory::class => 'notifier.transport_factory.spothit',
2339+
FakeChatTransportFactory::class => 'notifier.transport_factory.fakechat',
23402340
FakeSmsTransportFactory::class => 'notifier.transport_factory.fakesms',
23412341
OvhCloudTransportFactory::class => 'notifier.transport_factory.ovhcloud',
23422342
SinchTransportFactory::class => 'notifier.transport_factory.sinch',

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\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;
@@ -96,6 +97,10 @@
9697
->parent('notifier.transport_factory.abstract')
9798
->tag('texter.transport_factory')
9899

100+
->set('notifier.transport_factory.fakechat', FakeChatTransportFactory::class)
101+
->parent('notifier.transport_factory.abstract')
102+
->tag('chatter.transport_factory')
103+
99104
->set('notifier.transport_factory.fakesms', FakeSmsTransportFactory::class)
100105
->parent('notifier.transport_factory.abstract')
101106
->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: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
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\Message\SmsMessage;
< 10000 /code>
23+
use Symfony\Component\Notifier\Transport\AbstractTransport;
24+
use Symfony\Contracts\HttpClient\HttpClientInterface;
25+
26+
/**
27+
* @author Oskar Stark <oskarstark@googlemail.com>
28+
*/
29+
final class FakeChatEmailTransport extends AbstractTransport
30+
{
31+
private $mailer;
32+
private $to;
33+
private $from;
34+
35+
public function __construct(MailerInterface $mailer, string $to, string $from, HttpClientInterface $client = null, EventDispatcherInterface $dispatcher = null)
36+
{
37+
$this->mailer = $mailer;
38+
$this->to = $to;
39+
$this->from = $from;
40+
41+
parent::__construct($client, $dispatcher);
42+
}
43+
44+
public function __toString(): string
45+
{
46+
return sprintf('fakechat+email://%s?to=%s&from=%s', $this->getEndpoint(), $this->to, $this->from);
47+
}
48+
49+
public function supports(MessageInterface $message): bool
50+
{
51+
return $message instanceof ChatMessage;
52+
}
53+
54+
/**
55+
* @param MessageInterface|ChatMessage $message
56+
*
57+
* @throws TransportExceptionInterface
58+
*/
59+
protected function doSend(MessageInterface $message): SentMessage
60+
{
61+
if (!$this->supports($message)) {
62+
throw new UnsupportedMessageTypeException(__CLASS__, ChatMessage::class, $message);
63+
}
64+
65+
$email = (new Email())
66+
->from($this->from)
67+
->to($this->to)
68+
->subject(sprintf('New Chat message for recipient: %s', $message->getRecipientId()))
69+
->html($message->getSubject())
70+
->text($message->getSubject());
71+
72+
$this->mailer->send($email);
73+
74+
return new SentMessage($message, (string) $this);
75+
}
76+
}
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 <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+
}
Lines changed: 23 additions & 0 deletions
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)
Lines changed: 48 additions & 0 deletions
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+
}
Lines changed: 67 additions & 0 deletions
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+
}
Lines changed: 32 additions & 0 deletions
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+
}
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 FakeChat 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>

0 commit comments

Comments
 (0)
0