From 3c960c5ed3f3754087f1de602ed6c99034f63f47 Mon Sep 17 00:00:00 2001 From: Sergei Predvoditelev Date: Mon, 7 Oct 2024 12:18:47 +0300 Subject: [PATCH 1/2] Extract `ViewMailer` --- src/FileMailer.php | 4 +- src/Mailer.php | 63 -------------- src/MailerInterface.php | 44 ---------- src/{ => ViewMailer}/MessageBodyRenderer.php | 3 +- src/{ => ViewMailer}/MessageBodyTemplate.php | 2 +- src/ViewMailer/ViewMailer.php | 86 ++++++++++++++++++++ tests/FileMailerTest.php | 2 +- tests/MailerTest.php | 4 +- tests/MessageBodyRendererTest.php | 4 +- tests/MessageBodyTemplateTest.php | 2 +- tests/TestCase.php | 4 +- 11 files changed, 98 insertions(+), 120 deletions(-) rename src/{ => ViewMailer}/MessageBodyRenderer.php (98%) rename src/{ => ViewMailer}/MessageBodyTemplate.php (97%) create mode 100644 src/ViewMailer/ViewMailer.php diff --git a/src/FileMailer.php b/src/FileMailer.php index 38fb3b1..e01192f 100644 --- a/src/FileMailer.php +++ b/src/FileMailer.php @@ -39,7 +39,6 @@ final class FileMailer extends Mailer private $filenameCallback; /** - * @param MessageBodyRenderer $messageBodyRenderer The message body renderer instance. * @param string $path The path where message files located. * @param callable|null $filenameCallback A PHP callback that return a file name which will be used to save * the email message. @@ -47,13 +46,12 @@ final class FileMailer extends Mailer * @param EventDispatcherInterface|null $eventDispatcher The event dispatcher instance. */ public function __construct( - MessageBodyRenderer $messageBodyRenderer, private string $path, callable $filenameCallback = null, ?MessageSettings $messageSettings = null, ?EventDispatcherInterface $eventDispatcher = null, ) { - parent::__construct($messageBodyRenderer, $messageSettings, $eventDispatcher); + parent::__construct($messageSettings, $eventDispatcher); $this->filenameCallback = $filenameCallback; } diff --git a/src/Mailer.php b/src/Mailer.php index 239a057..99cc695 100644 --- a/src/Mailer.php +++ b/src/Mailer.php @@ -17,74 +17,11 @@ abstract class Mailer implements MailerInterface { public function __construct( - private MessageBodyRenderer $messageBodyRenderer, private ?MessageSettings $defaultMessageSettings = null, private ?EventDispatcherInterface $eventDispatcher = null, ) { } - /** - * Returns a new instance with the specified message body template. - * - * @param MessageBodyTemplate $template The message body template instance. - * - * @return self The new instance. - */ - public function withTemplate(MessageBodyTemplate $template): self - { - $new = clone $this; - $new->messageBodyRenderer = $new->messageBodyRenderer->withTemplate($template); - return $new; - } - - /** - * Returns a new instance with specified locale code. - * - * @param string $locale The locale code. - * - * @return self - */ - public function withLocale(string $locale): self - { - $new = clone $this; - $new->messageBodyRenderer = $new->messageBodyRenderer->withLocale($locale); - return $new; - } - - /** - * Creates a new message instance and optionally composes its body content via view rendering. - * - * @param array|string|null $view The view to be used for rendering the message body. - * This can be: - * - a string, which represents the view name for rendering the HTML body of the email. - * In this case, the text body will be generated by applying `strip_tags()` to the HTML body. - * - an array with 'html' and/or 'text' elements. The 'html' element refers to the view name - * for rendering the HTML body, while 'text' element is for rendering the text body. For example, - * `['html' => 'contact-html', 'text' => 'contact-text']`. - * - null, meaning the message instance will be returned without body content. - * - * The view to be rendered can be specified in one of the following formats: - * - a relative view name (e.g. "contact") located under {@see MessageBodyRenderer::$viewPath}. - * @param array $viewParameters The parameters (name-value pairs) - * that will be extracted and available in the view file. - * @param array $layoutParameters The parameters (name-value pairs) - * that will be extracted and available in the layout file. - * - * @throws Throwable If an error occurred during rendering. - * - * @return MessageInterface The message instance. - */ - public function compose($view = null, array $viewParameters = [], array $layoutParameters = []): MessageInterface - { - $message = new Message(); - - if ($view === null) { - return $message; - } - - return $this->messageBodyRenderer->addToMessage($message, $view, $viewParameters, $layoutParameters); - } - /** * Sends the given email message. * This method will log a message about the email being sent. diff --git a/src/MailerInterface.php b/src/MailerInterface.php index 9ac6f9b..1a368a6 100644 --- a/src/MailerInterface.php +++ b/src/MailerInterface.php @@ -27,26 +27,6 @@ */ interface MailerInterface { - /** - * Creates a new message instance and optionally composes its body content via view rendering. - * - * @param array|string|null $view the view to be used for rendering the message body. - * This can be: - * - a string, which represents the view name for rendering the HTML body of the email. - * In this case, the text body will be generated by applying `strip_tags()` to the HTML body. - * - an array with 'html' and/or 'text' elements. The 'html' element refers to the view name - * for rendering the HTML body, while 'text' element is for rendering the text body. For example, - * `['html' => 'contact-html', 'text' => 'contact-text']`. - * - null, meaning the message instance will be returned without body content. - * @param array $viewParameters The parameters (name-value pairs) - * that will be extracted and available in the view file. - * @param array $layoutParameters The parameters (name-value pairs) - * that will be extracted and available in the layout file. - * - * @return MessageInterface The message instance. - */ - public function compose($view = null, array $viewParameters = [], array $layoutParameters = []): MessageInterface; - /** * Sends the given email message. * @@ -67,28 +47,4 @@ public function send(MessageInterface $message): void; * @return SendResults The result object that contains all messages and errors for failed sent messages. */ public function sendMultiple(array $messages): SendResults; - - /** - * Returns a new instance with the specified message body template. - * - * @param MessageBodyTemplate $template The message body template instance. - * - * This method MUST be implemented in such a way as to retain the immutability of the mailer, - * and MUST return an instance that has the new message body template instance. - * - * @return self The new instance. - */ - public function withTemplate(MessageBodyTemplate $template): self; - - /** - * Returns a new instance with the specified locale. - * - * @param string $locale The locale code. - * - * This method MUST be implemented in such a way as to retain the immutability of the mailer, - * and MUST return an instance that has the new message body renderer instance. - * - * @return self The new instance. - */ - public function withLocale(string $locale): self; } diff --git a/src/MessageBodyRenderer.php b/src/ViewMailer/MessageBodyRenderer.php similarity index 98% rename from src/MessageBodyRenderer.php rename to src/ViewMailer/MessageBodyRenderer.php index 3b6926f..7542a0f 100644 --- a/src/MessageBodyRenderer.php +++ b/src/ViewMailer/MessageBodyRenderer.php @@ -2,10 +2,11 @@ declare(strict_types=1); -namespace Yiisoft\Mailer; +namespace Yiisoft\Mailer\ViewMailer; use RuntimeException; use Throwable; +use Yiisoft\Mailer\MessageInterface; use Yiisoft\View\View; use function html_entity_decode; diff --git a/src/MessageBodyTemplate.php b/src/ViewMailer/MessageBodyTemplate.php similarity index 97% rename from src/MessageBodyTemplate.php rename to src/ViewMailer/MessageBodyTemplate.php index d4d5714..32117ea 100644 --- a/src/MessageBodyTemplate.php +++ b/src/ViewMailer/MessageBodyTemplate.php @@ -2,7 +2,7 @@ declare(strict_types=1); -namespace Yiisoft\Mailer; +namespace Yiisoft\Mailer\ViewMailer; use LogicException; diff --git a/src/ViewMailer/ViewMailer.php b/src/ViewMailer/ViewMailer.php new file mode 100644 index 0000000..76f6d74 --- /dev/null +++ b/src/ViewMailer/ViewMailer.php @@ -0,0 +1,86 @@ +messageBodyRenderer = $new->messageBodyRenderer->withTemplate($template); + return $new; + } + + /** + * Returns a new instance with specified locale code. + * + * @param string $locale The locale code. + * + * @return self + */ + public function withLocale(string $locale): self + { + $new = clone $this; + $new->messageBodyRenderer = $new->messageBodyRenderer->withLocale($locale); + return $new; + } + + /** + * Creates a new message instance and optionally composes its body content via view rendering. + * + * @param array|string|null $view the view to be used for rendering the message body. + * This can be: + * - a string, which represents the view name for rendering the HTML body of the email. + * In this case, the text body will be generated by applying `strip_tags()` to the HTML body. + * - an array with 'html' and/or 'text' elements. The 'html' element refers to the view name + * for rendering the HTML body, while 'text' element is for rendering the text body. For example, + * `['html' => 'contact-html', 'text' => 'contact-text']`. + * - null, meaning the message instance will be returned without body content. + * @param array $viewParameters The parameters (name-value pairs) + * that will be extracted and available in the view file. + * @param array $layoutParameters The parameters (name-value pairs) + * that will be extracted and available in the layout file. + * + * @return MessageInterface The message instance. + */ + public function compose($view = null, array $viewParameters = [], array $layoutParameters = []): MessageInterface + { + $message = new Message; + + if ($view === null) { + return $message; + } + + return $this->messageBodyRenderer->addToMessage($message, $view, $viewParameters, $layoutParameters); + } + + public function send(MessageInterface $message): void + { + $this->mailer->send($message); + } + + public function sendMultiple(array $messages): SendResults + { + return $this->mailer->sendMultiple($messages); + } +} diff --git a/tests/FileMailerTest.php b/tests/FileMailerTest.php index 5939aa3..34dc046 100644 --- a/tests/FileMailerTest.php +++ b/tests/FileMailerTest.php @@ -12,7 +12,7 @@ use Yiisoft\Mailer\Event\AfterSend; use Yiisoft\Mailer\Event\BeforeSend; use Yiisoft\Mailer\FileMailer; -use Yiisoft\Mailer\MessageBodyRenderer; +use Yiisoft\Mailer\ViewMailer\MessageBodyRenderer; use Yiisoft\Mailer\MessageInterface; use function file_get_contents; diff --git a/tests/MailerTest.php b/tests/MailerTest.php index 7fd1ceb..c1c7096 100644 --- a/tests/MailerTest.php +++ b/tests/MailerTest.php @@ -12,8 +12,8 @@ use Yiisoft\Mailer\File; use Yiisoft\Mailer\MailerInterface; use Yiisoft\Mailer\Message; -use Yiisoft\Mailer\MessageBodyRenderer; -use Yiisoft\Mailer\MessageBodyTemplate; +use Yiisoft\Mailer\ViewMailer\MessageBodyRenderer; +use Yiisoft\Mailer\ViewMailer\MessageBodyTemplate; use Yiisoft\Mailer\MessageInterface; use Yiisoft\Mailer\Tests\TestAsset\DummyMailer; diff --git a/tests/MessageBodyRendererTest.php b/tests/MessageBodyRendererTest.php index 488d42f..96f1079 100644 --- a/tests/MessageBodyRendererTest.php +++ b/tests/MessageBodyRendererTest.php @@ -7,8 +7,8 @@ use PHPUnit\Framework\Attributes\DataProvider; use RuntimeException; use stdClass; -use Yiisoft\Mailer\MessageBodyRenderer; -use Yiisoft\Mailer\MessageBodyTemplate; +use Yiisoft\Mailer\ViewMailer\MessageBodyRenderer; +use Yiisoft\Mailer\ViewMailer\MessageBodyTemplate; use Yiisoft\View\View; final class MessageBodyRendererTest extends TestCase diff --git a/tests/MessageBodyTemplateTest.php b/tests/MessageBodyTemplateTest.php index 5b137bf..12883b7 100644 --- a/tests/MessageBodyTemplateTest.php +++ b/tests/MessageBodyTemplateTest.php @@ -6,7 +6,7 @@ use LogicException; use PHPUnit\Framework\Attributes\DataProvider; -use Yiisoft\Mailer\MessageBodyTemplate; +use Yiisoft\Mailer\ViewMailer\MessageBodyTemplate; final class MessageBodyTemplateTest extends TestCase { diff --git a/tests/TestCase.php b/tests/TestCase.php index 1b93dc6..629f5b2 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -10,8 +10,8 @@ use Yiisoft\Files\FileHelper; use Yiisoft\Mailer\MailerInterface; use Yiisoft\Mailer\Message; -use Yiisoft\Mailer\MessageBodyRenderer; -use Yiisoft\Mailer\MessageBodyTemplate; +use Yiisoft\Mailer\ViewMailer\MessageBodyRenderer; +use Yiisoft\Mailer\ViewMailer\MessageBodyTemplate; use Yiisoft\Mailer\MessageInterface; use Yiisoft\Mailer\Tests\TestAsset\DummyMailer; use Yiisoft\Test\Support\Container\SimpleContainer; From 481646bbc49593d6840860e58440c8d0289011cd Mon Sep 17 00:00:00 2001 From: StyleCI Bot Date: Mon, 7 Oct 2024 09:19:10 +0000 Subject: [PATCH 2/2] Apply fixes from StyleCI --- src/ViewMailer/ViewMailer.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ViewMailer/ViewMailer.php b/src/ViewMailer/ViewMailer.php index 76f6d74..96a3b9e 100644 --- a/src/ViewMailer/ViewMailer.php +++ b/src/ViewMailer/ViewMailer.php @@ -65,7 +65,7 @@ public function withLocale(string $locale): self */ public function compose($view = null, array $viewParameters = [], array $layoutParameters = []): MessageInterface { - $message = new Message; + $message = new Message(); if ($view === null) { return $message;