├── CHANGELOG.md ├── FakeChatEmailTransport.php ├── FakeChatLoggerTransport.php ├── FakeChatTransportFactory.php ├── LICENSE ├── README.md └── composer.json /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | CHANGELOG 2 | ========= 3 | 4 | 5.4 5 | --- 6 | 7 | * Add the ``FakeChatLoggerTransport`` 8 | 9 | 5.3 10 | --- 11 | 12 | * Add the bridge 13 | -------------------------------------------------------------------------------- /FakeChatEmailTransport.php: -------------------------------------------------------------------------------- 1 | 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 27 | */ 28 | final class FakeChatEmailTransport extends AbstractTransport 29 | { 30 | protected const HOST = 'default'; 31 | 32 | public function __construct( 33 | private MailerInterface $mailer, 34 | private string $to, 35 | private string $from, 36 | ?HttpClientInterface $client = null, 37 | ?EventDispatcherInterface $dispatcher = null, 38 | ) { 39 | parent::__construct($client, $dispatcher); 40 | } 41 | 42 | public function __toString(): string 43 | { 44 | return \sprintf('fakechat+email://%s?to=%s&from=%s', $this->getEndpoint(), $this->to, $this->from); 45 | } 46 | 47 | public function supports(MessageInterface $message): bool 48 | { 49 | return $message instanceof ChatMessage; 50 | } 51 | 52 | /** 53 | * @param MessageInterface|ChatMessage $message 54 | * 55 | * @throws TransportExceptionInterface 56 | */ 57 | protected function doSend(MessageInterface $message): SentMessage 58 | { 59 | if (!$this->supports($message)) { 60 | throw new UnsupportedMessageTypeException(__CLASS__, ChatMessage::class, $message); 61 | } 62 | 63 | $subject = 'New Chat message without specified recipient!'; 64 | if (null !== $message->getRecipientId()) { 65 | $subject = \sprintf('New Chat message for recipient: %s', $message->getRecipientId()); 66 | } 67 | 68 | $email = (new Email()) 69 | ->from($this->from) 70 | ->to($this->to) 71 | ->subject($subject) 72 | ->html($message->getSubject()) 73 | ->text($message->getSubject()); 74 | 75 | if ('default' !== $transportName = $this->getEndpoint()) { 76 | $email->getHeaders()->addTextHeader('X-Transport', $transportName); 77 | } 78 | 79 | $this->mailer->send($email); 80 | 81 | return new SentMessage($message, (string) $this); 82 | } 83 | } 84 | -------------------------------------------------------------------------------- /FakeChatLoggerTransport.php: -------------------------------------------------------------------------------- 1 | 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 Psr\Log\LoggerInterface; 15 | use Symfony\Component\EventDispatcher\EventDispatcherInterface; 16 | use Symfony\Component\Notifier\Exception\UnsupportedMessageTypeException; 17 | use Symfony\Component\Notifier\Message\ChatMessage; 18 | use Symfony\Component\Notifier\Message\MessageInterface; 19 | use Symfony\Component\Notifier\Message\SentMessage; 20 | use Symfony\Component\Notifier\Transport\AbstractTransport; 21 | use Symfony\Contracts\HttpClient\HttpClientInterface; 22 | 23 | /** 24 | * @author Antoine Makdessi 25 | */ 26 | final class FakeChatLoggerTransport extends AbstractTransport 27 | { 28 | protected const HOST = 'default'; 29 | 30 | public function __construct( 31 | private LoggerInterface $logger, 32 | ?HttpClientInterface $client = null, 33 | ?EventDispatcherInterface $dispatcher = null, 34 | ) { 35 | parent::__construct($client, $dispatcher); 36 | } 37 | 38 | public function __toString(): string 39 | { 40 | return \sprintf('fakechat+logger://%s', $this->getEndpoint()); 41 | } 42 | 43 | public function supports(MessageInterface $message): bool 44 | { 45 | return $message instanceof ChatMessage; 46 | } 47 | 48 | /** 49 | * @param MessageInterface|ChatMessage $message 50 | */ 51 | protected function doSend(MessageInterface $message): SentMessage 52 | { 53 | if (!$this->supports($message)) { 54 | throw new UnsupportedMessageTypeException(__CLASS__, ChatMessage::class, $message); 55 | } 56 | 57 | $subject = 'New Chat message without specified recipient!'; 58 | if (null !== $message->getRecipientId()) { 59 | $subject = \sprintf('New Chat message for recipient: %s', $message->getRecipientId()); 60 | } 61 | 62 | $this->logger->info(\sprintf('%s: %s', $subject, $message->getSubject())); 63 | 64 | return new SentMessage($message, (string) $this); 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /FakeChatTransportFactory.php: -------------------------------------------------------------------------------- 1 | 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 Psr\Log\LoggerInterface; 15 | use Symfony\Component\Mailer\MailerInterface; 16 | use Symfony\Component\Notifier\Exception\LogicException; 17 | use Symfony\Component\Notifier\Exception\UnsupportedSchemeException; 18 | use Symfony\Component\Notifier\Transport\AbstractTransportFactory; 19 | use Symfony\Component\Notifier\Transport\Dsn; 20 | use Symfony\Contracts\EventDispatcher\EventDispatcherInterface; 21 | use Symfony\Contracts\HttpClient\HttpClientInterface; 22 | 23 | /** 24 | * @author Oskar Stark 25 | * @author Antoine Makdessi 26 | */ 27 | final class FakeChatTransportFactory extends AbstractTransportFactory 28 | { 29 | public function __construct( 30 | private ?MailerInterface $mailer = null, 31 | private ?LoggerInterface $logger = null, 32 | ?EventDispatcherInterface $dispatcher = null, 33 | ?HttpClientInterface $client = null, 34 | ) { 35 | parent::__construct($dispatcher, $client); 36 | } 37 | 38 | public function create(Dsn $dsn): FakeChatEmailTransport|FakeChatLoggerTransport 39 | { 40 | $scheme = $dsn->getScheme(); 41 | 42 | if ('fakechat+email' === $scheme) { 43 | if (null === $this->mailer) { 44 | $this->throwMissingDependencyException($scheme, MailerInterface::class, 'symfony/mailer'); 45 | } 46 | 47 | $mailerTransport = $dsn->getHost(); 48 | $to = $dsn->getRequiredOption('to'); 49 | $from = $dsn->getRequiredOption('from'); 50 | 51 | return (new FakeChatEmailTransport($this->mailer, $to, $from, $this->client, $this->dispatcher))->setHost($mailerTransport); 52 | } 53 | 54 | if ('fakechat+logger' === $scheme) { 55 | if (null === $this->logger) { 56 | $this->throwMissingDependencyException($scheme, LoggerInterface::class, 'psr/log'); 57 | } 58 | 59 | return new FakeChatLoggerTransport($this->logger, $this->client, $this->dispatcher); 60 | } 61 | 62 | throw new UnsupportedSchemeException($dsn, 'fakechat', $this->getSupportedSchemes()); 63 | } 64 | 65 | protected function getSupportedSchemes(): array 66 | { 67 | return ['fakechat+email', 'fakechat+logger']; 68 | } 69 | 70 | private function throwMissingDependencyException(string $scheme, string $missingDependency, string $suggestedPackage): void 71 | { 72 | throw new LogicException(\sprintf('Cannot create a transport for scheme "%s" without providing an implementation of "%s". Try running "composer require "%s"".', $scheme, $missingDependency, $suggestedPackage)); 73 | } 74 | } 75 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2021-present 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. 20 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Fake Chat Notifier 2 | ================== 3 | 4 | Provides Fake Chat (as email or log during development) integration for Symfony Notifier. 5 | 6 | #### DSN example for email 7 | 8 | ``` 9 | FAKE_CHAT_DSN=fakechat+email://default?to=TO&from=FROM 10 | ``` 11 | 12 | where: 13 | - `TO` is email who receive Chat message during development 14 | - `FROM` is email who send Chat message during development 15 | 16 | To use a custom mailer transport: 17 | ``` 18 | FAKE_CHAT_DSN=fakechat+email://mailchimp?to=TO&from=FROM 19 | ``` 20 | 21 | #### DSN example for logger 22 | 23 | ``` 24 | FAKE_CHAT_DSN=fakechat+logger://default 25 | ``` 26 | 27 | Resources 28 | --------- 29 | 30 | * [Contributing](https://symfony.com/doc/current/contributing/index.html) 31 | * [Report issues](https://github.com/symfony/symfony/issues) and 32 | [send Pull Requests](https://github.com/symfony/symfony/pulls) 33 | in the [main Symfony repository](https://github.com/symfony/symfony) 34 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "symfony/fake-chat-notifier", 3 | "type": "symfony-notifier-bridge", 4 | "description": "Fake Chat (as email or log 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": "Antoine Makdessi", 15 | "email": "amakdessi@me.com", 16 | "homepage": "http://antoine.makdessi.free.fr" 17 | }, 18 | { 19 | "name": "Symfony Community", 20 | "homepage": "https://symfony.com/contributors" 21 | } 22 | ], 23 | "require": { 24 | "php": ">=8.2", 25 | "symfony/http-client": "^6.4|^7.0", 26 | "symfony/notifier": "^7.2" 27 | }, 28 | "require-dev": { 29 | "psr/log": "^1|^2|^3", 30 | "symfony/mailer": "^6.4|^7.0" 31 | }, 32 | "autoload": { 33 | "psr-4": { "Symfony\\Component\\Notifier\\Bridge\\FakeChat\\": "" }, 34 | "exclude-from-classmap": [ 35 | "/Tests/" 36 | ] 37 | }, 38 | "minimum-stability": "dev" 39 | } 40 | --------------------------------------------------------------------------------