-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Notifier] [FakeChat] Added the bridge #40647
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 4 additions & 0 deletions
4
src/Symfony/Component/Notifier/Bridge/FakeChat/.gitattributes
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
/Tests export-ignore | ||
/phpunit.xml.dist export-ignore | ||
/.gitattributes export-ignore | ||
/.gitignore export-ignore |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
CHANGELOG | ||
========= | ||
|
||
5.3 | ||
--- | ||
|
||
* Add the bridge |
75 changes: 75 additions & 0 deletions
75
src/Symfony/Component/Notifier/Bridge/FakeChat/FakeChatEmailTransport.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} |
58 changes: 58 additions & 0 deletions
58
src/Symfony/Component/Notifier/Bridge/FakeChat/FakeChatTransportFactory.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} | ||
OskarStark marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
protected function getSupportedSchemes(): array | ||
{ | ||
return ['fakechat+email']; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) |
48 changes: 48 additions & 0 deletions
48
src/Symfony/Component/Notifier/Bridge/FakeChat/Tests/FakeChatEmailTransportTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)]; | ||
} | ||
} |
67 changes: 67 additions & 0 deletions
67
src/Symfony/Component/Notifier/Bridge/FakeChat/Tests/FakeChatTransportFactoryTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 changes: 32 additions & 0 deletions
32
src/Symfony/Component/Notifier/Bridge/FakeChat/composer.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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" | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same as #40731, where is this locator configured? The framework integration seems broken