Skip to content

Commit

Permalink
Add MailerInterface::class. (#26)
Browse files Browse the repository at this point in the history
  • Loading branch information
terabytesoftw committed Jun 18, 2023
1 parent d9aa65e commit a2c0ea3
Show file tree
Hide file tree
Showing 5 changed files with 136 additions and 99 deletions.
3 changes: 2 additions & 1 deletion composer.json
Expand Up @@ -61,7 +61,8 @@
},
"config-plugin": {
"application-params": "?application-params.php",
"di": ["common/*.php"]
"di": ["common/*.php"],
"params": "common/params/*.php"
},
"config-plugin-environments": {
"tests": {
Expand Down
31 changes: 10 additions & 21 deletions config/common/mailer.php
Expand Up @@ -2,28 +2,17 @@

declare(strict_types=1);

use Psr\Log\LoggerInterface;
use Yii\Service\Mailer;
use Yii\Service\ParameterInterface;
use Yiisoft\Aliases\Aliases;
use Yiisoft\Mailer\MailerInterface;
use Yiisoft\Translator\TranslatorInterface;
use Yii\Service\MailerInterface;

/** @var array $params */
return [
Mailer::class => static function (
Aliases $aliases,
LoggerInterface $logger,
MailerInterface $mailer,
TranslatorInterface $translator,
ParameterInterface $parameter
) {
$mailer = new Mailer($aliases, $logger, $mailer, $translator);

return $mailer
->from($parameter->get('yii-tools.service.mailer.from', ''))
->signatureImage($parameter->get('yii-tools.service.mailer.signature-image', ''))
->signatureText($parameter->get('yii-tools.service.mailer.signature-text', ''))
->translatorCategory($parameter->get('yii-tools.service.mailer.translator-category', ''))
->viewPath($parameter->get('yii-tools.service.mailer.view-path', ''));
},
MailerInterface::class => [
'class' => Mailer::class,
'from()' => [$params['yii-tools.service.mailer.from']],
'signatureImage()' => [$params['yii-tools.service.mailer.signature-image']],
'signatureText()' => [$params['yii-tools.service.mailer.signature-text']],
'translatorCategory()' => [$params['yii-tools.service.mailer.translator-category']],
'viewPath()' => [$params['yii-tools.service.mailer.view-path']],
],
];
11 changes: 11 additions & 0 deletions config/common/params/yii-tools-service-mailer.php
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

return [
'yii-tools.service.mailer.from' => '',
'yii-tools.service.mailer.signature-image' => '',
'yii-tools.service.mailer.signature-text' => '',
'yii-tools.service.mailer.translator-category' => 'app',
'yii-tools.service.mailer.view-path' => '',
];
111 changes: 34 additions & 77 deletions src/Mailer.php
Expand Up @@ -31,7 +31,7 @@
* ->send('test@example.com', ['message' => 'Test body', 'username' => 'Test username']);
* ```
*/
final class Mailer
final class Mailer implements \Yii\Service\MailerInterface
{
/** @psalm-var string[] */
private array $attachments = [];
Expand All @@ -52,13 +52,6 @@ public function __construct(
) {
}

/**
* Returns a new instance with the specified attachments.
*
* @param array $value Attachments.
*
* @psalm-param string[] $value
*/
public function attachments(array $value): self
{
$new = clone $this;
Expand All @@ -67,11 +60,6 @@ public function attachments(array $value): self
return $new;
}

/**
* Returns a new instance with the specified from.
*
* @param string $value From.
*/
public function from(string $value): self
{
$new = clone $this;
Expand All @@ -80,13 +68,6 @@ public function from(string $value): self
return $new;
}

/**
* Returns a new instance with the specified layout.
*
* @param array $value Layout.
*
* @psalm-param array<string, string>|string|null $value
*/
public function layout(array|string|null $value): self
{
$new = clone $this;
Expand All @@ -95,11 +76,35 @@ public function layout(array|string|null $value): self
return $new;
}

/**
* Returns a new instance with the specified signature image.
*
* @param string $value Signature image.
*/
public function send(string $email, array $params = []): bool
{
$message = $this->mailer
->compose(
$this->layout,
[
'translator' => $this->translator,
'params' => $params,
],
[
'file' => $this->signatureImage,
'signatureTextEmail' => $this->signatureText,
'translator' => $this->translator,
],
)
->withFrom($this->from)
->withSubject($this->subject)
->withTo($email);

foreach ($this->attachments as $attachment) {
$filename = $this->aliases->get($attachment);
$message = $message->withAttached(
File::fromPath($filename, basename($filename), mime_content_type($filename))
);
}

return $this->sendInternal($message);
}

public function signatureImage(string $value): self
{
$new = clone $this;
Expand All @@ -112,11 +117,6 @@ public function signatureImage(string $value): self
return $new;
}

/**
* Returns a new instance with the specified signature text.
*
* @param string $value Signature text.
*/
public function signatureText(string $value): self
{
$new = clone $this;
Expand All @@ -125,11 +125,6 @@ public function signatureText(string $value): self
return $new;
}

/**
* Returns a new instance with the specified subject.
*
* @param string $value Subject.
*/
public function subject(string $value): self
{
$new = clone $this;
Expand All @@ -138,11 +133,6 @@ public function subject(string $value): self
return $new;
}

/**
* Returns a new instance with the specified translator category.
*
* @param string $value Translator category.
*/
public function translatorCategory(string $value): self
{
$new = clone $this;
Expand All @@ -154,11 +144,6 @@ public function translatorCategory(string $value): self
return $new;
}

/**
* Returns a new instance with the specified view path.
*
* @param string $value View path.
*/
public function viewPath(string $value): self
{
$new = clone $this;
Expand All @@ -171,40 +156,12 @@ public function viewPath(string $value): self
}

/**
* Sends an email.
* Sends the given email message.
*
* @param MessageInterface $message Email message instance to be sent.
*
* @param string $email Email.
* @param array $params Params.
* @throws Exception If sending failed.
*/
public function send(string $email, array $params = []): bool
{
$message = $this->mailer
->compose(
$this->layout,
[
'translator' => $this->translator,
'params' => $params,
],
[
'file' => $this->signatureImage,
'signatureTextEmail' => $this->signatureText,
'translator' => $this->translator,
],
)
->withFrom($this->from)
->withSubject($this->subject)
->withTo($email);

foreach ($this->attachments as $attachment) {
$filename = $this->aliases->get($attachment);
$message = $message->withAttached(
File::fromPath($filename, basename($filename), mime_content_type($filename))
);
}

return $this->sendInternal($message);
}

private function sendInternal(MessageInterface $message): bool
{
$result = false;
Expand Down
79 changes: 79 additions & 0 deletions src/MailerInterface.php
@@ -0,0 +1,79 @@
<?php

declare(strict_types=1);

namespace Yii\Service;

/**
* Provides a way to send email messages.
*/
interface MailerInterface
{
/**
* Returns a new instance with the specified attachments.
*
* @param array $value Attachments.
*
* @psalm-param string[] $value
*/
public function attachments(array $value): self;

/**
* Returns a new instance with the specified from.
*
* @param string $value From.
*/
public function from(string $value): self;

/**
* Returns a new instance with the specified layout.
*
* @param array $value Layout.
*
* @psalm-param array<string, string>|string|null $value
*/
public function layout(array|string|null $value): self;

/**
* Sends an email.
*
* @param string $email Email.
* @param array $params Params.
*/
public function send(string $email, array $params = []): bool;

/**
* Returns a new instance with the specified signature image.
*
* @param string $value Signature image.
*/
public function signatureImage(string $value): self;

/**
* Returns a new instance with the specified signature text.
*
* @param string $value Signature text.
*/
public function signatureText(string $value): self;

/**
* Returns a new instance with the specified subject.
*
* @param string $value Subject.
*/
public function subject(string $value): self;

/**
* Returns a new instance with the specified translator category.
*
* @param string $value Translator category.
*/
public function translatorCategory(string $value): self;

/**
* Returns a new instance with the specified view path.
*
* @param string $value View path.
*/
public function viewPath(string $value): self;
}

0 comments on commit a2c0ea3

Please sign in to comment.