[Notifier] [FakeChat] Added the bridge
This commit is contained in:
parent
71a407da29
commit
0ce156c484
@ -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',
|
||||
|
@ -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')
|
||||
|
4
src/Symfony/Component/Notifier/Bridge/FakeChat/.gitattributes
vendored
Normal file
4
src/Symfony/Component/Notifier/Bridge/FakeChat/.gitattributes
vendored
Normal file
@ -0,0 +1,4 @@
|
||||
/Tests export-ignore
|
||||
/phpunit.xml.dist export-ignore
|
||||
/.gitattributes export-ignore
|
||||
/.gitignore export-ignore
|
@ -0,0 +1,7 @@
|
||||
CHANGELOG
|
||||
=========
|
||||
|
||||
5.3
|
||||
---
|
||||
|
||||
* Add the bridge
|
@ -0,0 +1,75 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Symfony package.
|
||||
*
|
||||
* (c) Fabien Potencier <fabien@symfony.com>
|
||||
*
|
||||
* 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 <oskarstark@googlemail.com>
|
||||
*/
|
||||
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);
|
||||
}
|
||||
}
|
@ -0,0 +1,58 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Symfony package.
|
||||
*
|
||||
* (c) Fabien Potencier <fabien@symfony.com>
|
||||
*
|
||||
* 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 <oskarstark@googlemail.com>
|
||||
*/
|
||||
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'];
|
||||
}
|
||||
}
|
19
src/Symfony/Component/Notifier/Bridge/FakeChat/LICENSE
Normal file
19
src/Symfony/Component/Notifier/Bridge/FakeChat/LICENSE
Normal file
@ -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.
|
23
src/Symfony/Component/Notifier/Bridge/FakeChat/README.md
Normal file
23
src/Symfony/Component/Notifier/Bridge/FakeChat/README.md
Normal file
@ -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)
|
@ -0,0 +1,48 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Symfony package.
|
||||
*
|
||||
* (c) Fabien Potencier <fabien@symfony.com>
|
||||
*
|
||||
* 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 <oskarstark@googlemail.com>
|
||||
*/
|
||||
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)];
|
||||
}
|
||||
}
|
@ -0,0 +1,67 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Symfony package.
|
||||
*
|
||||
* (c) Fabien Potencier <fabien@symfony.com>
|
||||
*
|
||||
* 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 <oskarstark@googlemail.com>
|
||||
*/
|
||||
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'];
|
||||
}
|
||||
}
|
32
src/Symfony/Component/Notifier/Bridge/FakeChat/composer.json
Normal file
32
src/Symfony/Component/Notifier/Bridge/FakeChat/composer.json
Normal file
@ -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"
|
||||
}
|
@ -0,0 +1,31 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
|
||||
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/5.2/phpunit.xsd"
|
||||
backupGlobals="false"
|
||||
colors="true"
|
||||
bootstrap="vendor/autoload.php"
|
||||
failOnRisky="true"
|
||||
failOnWarning="true"
|
||||
>
|
||||
<php>
|
||||
<ini name="error_reporting" value="-1" />
|
||||
</php>
|
||||
|
||||
<testsuites>
|
||||
<testsuite name="Symfony FakeChat Notifier Bridge Test Suite">
|
||||
<directory>./Tests/</directory>
|
||||
</testsuite>
|
||||
</testsuites>
|
||||
|
||||
<filter>
|
||||
<whitelist>
|
||||
<directory>./</directory>
|
||||
<exclude>
|
||||
<directory>./Resources</directory>
|
||||
<directory>./Tests</directory>
|
||||
<directory>./vendor</directory>
|
||||
</exclude>
|
||||
</whitelist>
|
||||
</filter>
|
||||
</phpunit>
|
@ -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',
|
||||
|
Reference in New Issue
Block a user