diff --git a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php index 8dd0577a34..90a1f02e3a 100644 --- a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php +++ b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php @@ -113,6 +113,7 @@ use Symfony\Component\Notifier\Bridge\AllMySms\AllMySmsTransportFactory; use Symfony\Component\Notifier\Bridge\Clickatell\ClickatellTransportFactory; use Symfony\Component\Notifier\Bridge\Discord\DiscordTransportFactory; use Symfony\Component\Notifier\Bridge\Esendex\EsendexTransportFactory; +use Symfony\Component\Notifier\Bridge\FakeChat\FakeChatTransportFactory; use Symfony\Component\Notifier\Bridge\FakeSms\FakeSmsTransportFactory; use Symfony\Component\Notifier\Bridge\Firebase\FirebaseTransportFactory; use Symfony\Component\Notifier\Bridge\FreeMobile\FreeMobileTransportFactory; @@ -2373,6 +2374,7 @@ class FrameworkExtension extends Extension FirebaseTransportFactory::class => 'notifier.transport_factory.firebase', FreeMobileTransportFactory::class => 'notifier.transport_factory.freemobile', SpotHitTransportFactory::class => 'notifier.transport_factory.spothit', + FakeChatTransportFactory::class => 'notifier.transport_factory.fakechat', FakeSmsTransportFactory::class => 'notifier.transport_factory.fakesms', OvhCloudTransportFactory::class => 'notifier.transport_factory.ovhcloud', SinchTransportFactory::class => 'notifier.transport_factory.sinch', diff --git a/src/Symfony/Bundle/FrameworkBundle/Resources/config/notifier_transports.php b/src/Symfony/Bundle/FrameworkBundle/Resources/config/notifier_transports.php index ded1d18bfd..e18e0a1753 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Resources/config/notifier_transports.php +++ b/src/Symfony/Bundle/FrameworkBundle/Resources/config/notifier_transports.php @@ -15,6 +15,7 @@ use Symfony\Component\Notifier\Bridge\AllMySms\AllMySmsTransportFactory; use Symfony\Component\Notifier\Bridge\Clickatell\ClickatellTransportFactory; use Symfony\Component\Notifier\Bridge\Discord\DiscordTransportFactory; use Symfony\Component\Notifier\Bridge\Esendex\EsendexTransportFactory; +use Symfony\Component\Notifier\Bridge\FakeChat\FakeChatTransportFactory; use Symfony\Component\Notifier\Bridge\FakeSms\FakeSmsTransportFactory; use Symfony\Component\Notifier\Bridge\Firebase\FirebaseTransportFactory; use Symfony\Component\Notifier\Bridge\FreeMobile\FreeMobileTransportFactory; @@ -97,6 +98,10 @@ return static function (ContainerConfigurator $container) { ->parent('notifier.transport_factory.abstract') ->tag('texter.transport_factory') + ->set('notifier.transport_factory.fakechat', FakeChatTransportFactory::class) + ->parent('notifier.transport_factory.abstract') + ->tag('chatter.transport_factory') + ->set('notifier.transport_factory.fakesms', FakeSmsTransportFactory::class) ->parent('notifier.transport_factory.abstract') ->tag('texter.transport_factory') diff --git a/src/Symfony/Component/Notifier/Bridge/FakeChat/.gitattributes b/src/Symfony/Component/Notifier/Bridge/FakeChat/.gitattributes new file mode 100644 index 0000000000..84c7add058 --- /dev/null +++ b/src/Symfony/Component/Notifier/Bridge/FakeChat/.gitattributes @@ -0,0 +1,4 @@ +/Tests export-ignore +/phpunit.xml.dist export-ignore +/.gitattributes export-ignore +/.gitignore export-ignore diff --git a/src/Symfony/Component/Notifier/Bridge/FakeChat/CHANGELOG.md b/src/Symfony/Component/Notifier/Bridge/FakeChat/CHANGELOG.md new file mode 100644 index 0000000000..1f2b652ac2 --- /dev/null +++ b/src/Symfony/Component/Notifier/Bridge/FakeChat/CHANGELOG.md @@ -0,0 +1,7 @@ +CHANGELOG +========= + +5.3 +--- + + * Add the bridge diff --git a/src/Symfony/Component/Notifier/Bridge/FakeChat/FakeChatEmailTransport.php b/src/Symfony/Component/Notifier/Bridge/FakeChat/FakeChatEmailTransport.php new file mode 100644 index 0000000000..a44abb023c --- /dev/null +++ b/src/Symfony/Component/Notifier/Bridge/FakeChat/FakeChatEmailTransport.php @@ -0,0 +1,75 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Notifier\Bridge\FakeChat; + +use Symfony\Component\EventDispatcher\EventDispatcherInterface; +use Symfony\Component\Mailer\Exception\TransportExceptionInterface; +use Symfony\Component\Mailer\MailerInterface; +use Symfony\Component\Mime\Email; +use Symfony\Component\Notifier\Exception\UnsupportedMessageTypeException; +use Symfony\Component\Notifier\Message\ChatMessage; +use Symfony\Component\Notifier\Message\MessageInterface; +use Symfony\Component\Notifier\Message\SentMessage; +use Symfony\Component\Notifier\Transport\AbstractTransport; +use Symfony\Contracts\HttpClient\HttpClientInterface; + +/** + * @author Oskar Stark + */ +final class FakeChatEmailTransport extends AbstractTransport +{ + private $mailer; + private $to; + private $from; + + public function __construct(MailerInterface $mailer, string $to, string $from, HttpClientInterface $client = null, EventDispatcherInterface $dispatcher = null) + { + $this->mailer = $mailer; + $this->to = $to; + $this->from = $from; + + parent::__construct($client, $dispatcher); + } + + public function __toString(): string + { + return sprintf('fakechat+email://%s?to=%s&from=%s', $this->getEndpoint(), $this->to, $this->from); + } + + public function supports(MessageInterface $message): bool + { + return $message instanceof ChatMessage; + } + + /** + * @param MessageInterface|ChatMessage $message + * + * @throws TransportExceptionInterface + */ + protected function doSend(MessageInterface $message): SentMessage + { + if (!$this->supports($message)) { + throw new UnsupportedMessageTypeException(__CLASS__, ChatMessage::class, $message); + } + + $email = (new Email()) + ->from($this->from) + ->to($this->to) + ->subject(sprintf('New Chat message for recipient: %s', $message->getRecipientId())) + ->html($message->getSubject()) + ->text($message->getSubject()); + + $this->mailer->send($email); + + return new SentMessage($message, (string) $this); + } +} diff --git a/src/Symfony/Component/Notifier/Bridge/FakeChat/FakeChatTransportFactory.php b/src/Symfony/Component/Notifier/Bridge/FakeChat/FakeChatTransportFactory.php new file mode 100644 index 0000000000..9d40cca74a --- /dev/null +++ b/src/Symfony/Component/Notifier/Bridge/FakeChat/FakeChatTransportFactory.php @@ -0,0 +1,58 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Notifier\Bridge\FakeChat; + +use Symfony\Component\Notifier\Exception\UnsupportedSchemeException; +use Symfony\Component\Notifier\Transport\AbstractTransportFactory; +use Symfony\Component\Notifier\Transport\Dsn; +use Symfony\Component\Notifier\Transport\TransportInterface; +use Symfony\Contracts\Service\ServiceProviderInterface; + +/** + * @author Oskar Stark + */ +final class FakeChatTransportFactory extends AbstractTransportFactory +{ + protected $serviceProvider; + + public function __construct(ServiceProviderInterface $serviceProvider) + { + parent::__construct(); + + $this->serviceProvider = $serviceProvider; + } + + /** + * @return FakeChatEmailTransport + */ + public function create(Dsn $dsn): TransportInterface + { + $scheme = $dsn->getScheme(); + + if (!\in_array($scheme, $this->getSupportedSchemes())) { + throw new UnsupportedSchemeException($dsn, 'fakechat', $this->getSupportedSchemes()); + } + + if ('fakechat+email' === $scheme) { + $serviceId = $dsn->getHost(); + $to = $dsn->getRequiredOption('to'); + $from = $dsn->getRequiredOption('from'); + + return (new FakeChatEmailTransport($this->serviceProvider->get($serviceId), $to, $from))->setHost($serviceId); + } + } + + protected function getSupportedSchemes(): array + { + return ['fakechat+email']; + } +} diff --git a/src/Symfony/Component/Notifier/Bridge/FakeChat/LICENSE b/src/Symfony/Component/Notifier/Bridge/FakeChat/LICENSE new file mode 100644 index 0000000000..efb17f98e7 --- /dev/null +++ b/src/Symfony/Component/Notifier/Bridge/FakeChat/LICENSE @@ -0,0 +1,19 @@ +Copyright (c) 2021 Fabien Potencier + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is furnished +to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. diff --git a/src/Symfony/Component/Notifier/Bridge/FakeChat/README.md b/src/Symfony/Component/Notifier/Bridge/FakeChat/README.md new file mode 100644 index 0000000000..45cb71cb94 --- /dev/null +++ b/src/Symfony/Component/Notifier/Bridge/FakeChat/README.md @@ -0,0 +1,23 @@ +Fake Chat Notifier +================== + +Provides Fake Chat (as email during development) integration for Symfony Notifier. + +#### DSN example + +``` +FAKE_CHAT_DSN=fakechat+email://MAILER_SERVICE_ID?to=TO&from=FROM +``` + +where: + - `MAILER_SERVICE_ID` is mailer service id (use `mailer` by default) + - `TO` is email who receive Chat message during development + - `FROM` is email who send Chat message during development + +Resources +--------- + + * [Contributing](https://symfony.com/doc/current/contributing/index.html) + * [Report issues](https://github.com/symfony/symfony/issues) and + [send Pull Requests](https://github.com/symfony/symfony/pulls) + in the [main Symfony repository](https://github.com/symfony/symfony) diff --git a/src/Symfony/Component/Notifier/Bridge/FakeChat/Tests/FakeChatEmailTransportTest.php b/src/Symfony/Component/Notifier/Bridge/FakeChat/Tests/FakeChatEmailTransportTest.php new file mode 100644 index 0000000000..34836d0599 --- /dev/null +++ b/src/Symfony/Component/Notifier/Bridge/FakeChat/Tests/FakeChatEmailTransportTest.php @@ -0,0 +1,48 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Notifier\Bridge\FakeChat\Tests; + +use Symfony\Component\Mailer\MailerInterface; +use Symfony\Component\Notifier\Bridge\FakeChat\FakeChatEmailTransport; +use Symfony\Component\Notifier\Message\ChatMessage; +use Symfony\Component\Notifier\Message\MessageInterface; +use Symfony\Component\Notifier\Message\SmsMessage; +use Symfony\Component\Notifier\Test\TransportTestCase; +use Symfony\Component\Notifier\Transport\TransportInterface; +use Symfony\Contracts\HttpClient\HttpClientInterface; + +/** + * @author Oskar Stark + */ +final class FakeChatEmailTransportTest extends TransportTestCase +{ + public function createTransport(?HttpClientInterface $client = null): TransportInterface + { + return (new FakeChatEmailTransport($this->createMock(MailerInterface::class), 'recipient@email.net', 'from@email.net'))->setHost('mailer'); + } + + public function toStringProvider(): iterable + { + yield ['fakechat+email://mailer?to=recipient@email.net&from=from@email.net', $this->createTransport()]; + } + + public function supportedMessagesProvider(): iterable + { + yield [new ChatMessage('Hello!')]; + } + + public function unsupportedMessagesProvider(): iterable + { + yield [new SmsMessage('0611223344', 'Hello!')]; + yield [$this->createMock(MessageInterface::class)]; + } +} diff --git a/src/Symfony/Component/Notifier/Bridge/FakeChat/Tests/FakeChatTransportFactoryTest.php b/src/Symfony/Component/Notifier/Bridge/FakeChat/Tests/FakeChatTransportFactoryTest.php new file mode 100644 index 0000000000..f3e928a279 --- /dev/null +++ b/src/Symfony/Component/Notifier/Bridge/FakeChat/Tests/FakeChatTransportFactoryTest.php @@ -0,0 +1,67 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Notifier\Bridge\FakeChat\Tests; + +use Symfony\Component\Mailer\MailerInterface; +use Symfony\Component\Notifier\Bridge\FakeChat\FakeChatTransportFactory; +use Symfony\Component\Notifier\Test\TransportFactoryTestCase; +use Symfony\Component\Notifier\Transport\TransportFactoryInterface; +use Symfony\Contracts\Service\ServiceProviderInterface; + +/** + * @author Oskar Stark + */ +final class FakeChatTransportFactoryTest extends TransportFactoryTestCase +{ + /** + * @return FakeChatTransportFactory + */ + public function createFactory(): TransportFactoryInterface + { + $serviceProvider = $this->createMock(ServiceProviderInterface::class); + $serviceProvider->method('has')->willReturn(true); + $serviceProvider->method('get')->willReturn($this->createMock(MailerInterface::class)); + + return new FakeChatTransportFactory($serviceProvider); + } + + public function createProvider(): iterable + { + yield [ + 'fakechat+email://mailer?to=recipient@email.net&from=sender@email.net', + 'fakechat+email://mailer?to=recipient@email.net&from=sender@email.net', + ]; + } + + public function missingRequiredOptionProvider(): iterable + { + yield 'missing option: from' => ['fakechat+email://mailer?to=recipient@email.net']; + yield 'missing option: to' => ['fakechat+email://mailer?from=sender@email.net']; + } + + public function supportsProvider(): iterable + { + yield [true, 'fakechat+email://mailer?to=recipient@email.net&from=sender@email.net']; + yield [false, 'somethingElse://mailer?to=recipient@email.net&from=sender@email.net']; + } + + public function incompleteDsnProvider(): iterable + { + yield 'missing from' => ['fakechat+email://mailer?to=recipient@email.net']; + yield 'missing to' => ['fakechat+email://mailer?from=recipient@email.net']; + } + + public function unsupportedSchemeProvider(): iterable + { + yield ['somethingElse://mailer?to=recipient@email.net&from=sender@email.net']; + } +} diff --git a/src/Symfony/Component/Notifier/Bridge/FakeChat/composer.json b/src/Symfony/Component/Notifier/Bridge/FakeChat/composer.json new file mode 100644 index 0000000000..6ce4f4949a --- /dev/null +++ b/src/Symfony/Component/Notifier/Bridge/FakeChat/composer.json @@ -0,0 +1,32 @@ +{ + "name": "symfony/fake-chat-notifier", + "type": "symfony-bridge", + "description": "Fake Chat (as email during development) Notifier Bridge.", + "keywords": ["chat", "development", "email", "notifier", "symfony"], + "homepage": "https://symfony.com", + "license": "MIT", + "authors": [ + { + "name": "Oskar Stark", + "homepage": "https://github.com/OskarStark" + }, + { + "name": "Symfony Community", + "homepage": "https://symfony.com/contributors" + } + ], + "require": { + "php": ">=7.2.5", + "symfony/http-client": "^4.4|^5.2", + "symfony/notifier": "^5.3", + "symfony/event-dispatcher-contracts": "^2", + "symfony/mailer": "^5.2" + }, + "autoload": { + "psr-4": { "Symfony\\Component\\Notifier\\Bridge\\FakeChat\\": "" }, + "exclude-from-classmap": [ + "/Tests/" + ] + }, + "minimum-stability": "dev" +} diff --git a/src/Symfony/Component/Notifier/Bridge/FakeChat/phpunit.xml.dist b/src/Symfony/Component/Notifier/Bridge/FakeChat/phpunit.xml.dist new file mode 100644 index 0000000000..307034c8df --- /dev/null +++ b/src/Symfony/Component/Notifier/Bridge/FakeChat/phpunit.xml.dist @@ -0,0 +1,31 @@ + + + + + + + + + + ./Tests/ + + + + + + ./ + + ./Resources + ./Tests + ./vendor + + + + diff --git a/src/Symfony/Component/Notifier/Exception/UnsupportedSchemeException.php b/src/Symfony/Component/Notifier/Exception/UnsupportedSchemeException.php index b948d194ac..da3657263d 100644 --- a/src/Symfony/Component/Notifier/Exception/UnsupportedSchemeException.php +++ b/src/Symfony/Component/Notifier/Exception/UnsupportedSchemeException.php @@ -72,6 +72,10 @@ class UnsupportedSchemeException extends LogicException 'class' => Bridge\SpotHit\SpotHitTransportFactory::class, 'package' => 'symfony/spot-hit-notifier', ], + 'fakechat' => [ + 'class' => Bridge\FakeChat\FakeChatTransportFactory::class, + 'package' => 'symfony/fake-chat-notifier', + ], 'fakesms' => [ 'class' => Bridge\FakeSms\FakeSmsTransportFactory::class, 'package' => 'symfony/fake-sms-notifier',