Skip to content
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

Extract ViewMailer #101

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions src/FileMailer.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,21 +39,19 @@ 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.
* @param MessageSettings|null $messageSettings The default and extra message settings.
* @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;
}

Expand Down
63 changes: 0 additions & 63 deletions src/Mailer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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, string>|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.
Expand Down
44 changes: 0 additions & 44 deletions src/MailerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,26 +27,6 @@
*/
interface MailerInterface
{
/**
* Creates a new message instance and optionally composes its body content via view rendering.
*
* @param array<string, string>|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.
*
Expand All @@ -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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Yiisoft\Mailer;
namespace Yiisoft\Mailer\ViewMailer;

use LogicException;

Expand Down
86 changes: 86 additions & 0 deletions src/ViewMailer/ViewMailer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Mailer\ViewMailer;

use Yiisoft\Mailer\MailerInterface;
use Yiisoft\Mailer\Message;
use Yiisoft\Mailer\MessageInterface;
use Yiisoft\Mailer\SendResults;

final class ViewMailer implements MailerInterface
{
public function __construct(
private readonly MailerInterface $mailer,
private MessageBodyRenderer $messageBodyRenderer,
) {
}

/**
* 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, string>|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);
}
}
2 changes: 1 addition & 1 deletion tests/FileMailerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions tests/MailerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
4 changes: 2 additions & 2 deletions tests/MessageBodyRendererTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion tests/MessageBodyTemplateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
4 changes: 2 additions & 2 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Loading