Removed ely/email-renderer package and implemented new emails renderer client [skip ci]

This commit is contained in:
ErickSkrauch 2019-06-07 02:16:13 +03:00
parent 7b14b92a10
commit 1bf249030f
23 changed files with 262 additions and 146 deletions

View File

@ -3,6 +3,7 @@
YII_DEBUG=true
YII_ENV=dev
DOMAIN=https://account.ely.by
EMAILS_RENDERER_HOST=http://emails-renderer:3000
## Параметры, отвечающие за безопасность
JWT_USER_SECRET=

View File

@ -20,7 +20,7 @@ class Yii extends \yii\BaseYii {
* @property \yii\swiftmailer\Mailer $mailer
* @property \yii\redis\Connection $redis
* @property \GuzzleHttp\Client $guzzle
* @property \common\components\EmailRenderer $emailRenderer
* @property \common\components\EmailsRenderer\Component $emailsRenderer
* @property \mito\sentry\Component $sentry
* @property \api\components\OAuth2\Component $oauth
* @property \common\components\StatsD $statsd

View File

@ -1,71 +0,0 @@
<?php
namespace common\components;
use Ely\Email\Renderer;
use Ely\Email\TemplateBuilder;
use Yii;
use yii\base\Component;
use yii\base\InvalidConfigException;
class EmailRenderer extends Component {
/**
* @var string базовый путь после хоста. Должен начинаться слешем и заканчиваться без него.
* Например "/email-images"
*/
public $basePath = '';
/**
* @var Renderer
*/
private $renderer;
/**
* @var string
*/
private $_baseDomain;
public function __construct(array $config = []) {
parent::__construct($config);
if ($this->_baseDomain === null) {
$this->_baseDomain = Yii::$app->urlManager->getHostInfo();
if ($this->_baseDomain === null) {
throw new InvalidConfigException('Cannot automatically obtain base domain');
}
}
$this->renderer = new Renderer($this->buildBasePath());
}
public function setBaseDomain(string $baseDomain) {
$this->_baseDomain = $baseDomain;
$this->renderer->setBaseDomain($this->buildBasePath());
}
public function getBaseDomain(): string {
return $this->_baseDomain;
}
/**
* @param string $templateName
* @return TemplateBuilder
*/
public function getTemplate(string $templateName): TemplateBuilder {
return $this->renderer->getTemplate($templateName);
}
/**
* @param TemplateBuilder $template
* @throws \Ely\Email\RendererException
* @return string
*/
public function render(TemplateBuilder $template): string {
return $this->renderer->render($template);
}
private function buildBasePath(): string {
return $this->_baseDomain . $this->basePath;
}
}

View File

@ -0,0 +1,59 @@
<?php
declare(strict_types=1);
namespace common\components\EmailsRenderer;
use GuzzleHttp\Client as GuzzleClient;
use GuzzleHttp\ClientInterface;
class Api {
private $baseUrl;
/**
* @var ClientInterface
*/
private $client;
public function __construct(string $baseUrl) {
$this->baseUrl = $baseUrl;
}
public function setClient(ClientInterface $client): void {
$this->client = $client;
}
/**
* @param \common\components\EmailsRenderer\Request\TemplateRequest $request
*
* @return string
* @throws \GuzzleHttp\Exception\GuzzleException
*/
public function getTemplate(Request\TemplateRequest $request): string {
return $this->getClient()
->request('GET', "/templates/{$request->getLocale()}/{$request->getName()}", [
'query' => $request->getParams(),
])
->getBody()
->getContents();
}
/**
* @return ClientInterface
*/
protected function getClient(): ClientInterface {
if ($this->client === null) {
$this->client = $this->createDefaultClient();
}
return $this->client;
}
private function createDefaultClient(): ClientInterface {
return new GuzzleClient([
'timeout' => 5,
'base_uri' => $this->baseUrl,
]);
}
}

View File

@ -0,0 +1,89 @@
<?php
declare(strict_types=1);
namespace common\components\EmailsRenderer;
use common\components\EmailsRenderer\Request\TemplateRequest;
use Yii;
use yii\base\InvalidConfigException;
use yii\helpers\ArrayHelper;
use yii\helpers\FileHelper;
/**
* @property string $baseDomain
*/
class Component extends \yii\base\Component implements RendererInterface {
/**
* @var string The address of the templates rendering service.
*/
public $serviceUrl;
/**
* @var string базовый путь после хоста. Должен начинаться слешем и заканчиваться без него.
* Например "/email-images"
*/
public $basePath = '';
/**
* @var Api
*/
private $api;
/**
* @var string
*/
private $_baseDomain;
public function init(): void {
parent::init();
if ($this->serviceUrl === null) {
throw new InvalidConfigException('serviceUrl is required');
}
if ($this->_baseDomain === null) {
$this->_baseDomain = Yii::$app->urlManager->getHostInfo();
if ($this->_baseDomain === null) {
throw new InvalidConfigException('Cannot automatically obtain base domain');
}
}
}
public function setBaseDomain(string $baseDomain): void {
$this->_baseDomain = $baseDomain;
}
public function getBaseDomain(): string {
return $this->_baseDomain;
}
/**
* @param string $templateName
* @param string $locale
* @param array $params
*
* @return string
* @throws \GuzzleHttp\Exception\GuzzleException
*/
public function render(string $templateName, string $locale, array $params = []): string {
$request = new TemplateRequest($templateName, $locale, ArrayHelper::merge($params, [
'assetsHost' => $this->buildBasePath(),
]));
return $this->getApi()->getTemplate($request);
}
private function getApi(): Api {
if ($this->api === null) {
$this->api = new Api($this->serviceUrl);
}
return $this->api;
}
private function buildBasePath(): string {
return FileHelper::normalizePath($this->_baseDomain . '/' . $this->basePath, '/');
}
}

View File

@ -0,0 +1,10 @@
<?php
declare(strict_types=1);
namespace common\components\EmailsRenderer;
interface RendererInterface {
public function render(string $templateName, string $locale, array $params = []): string;
}

View File

@ -0,0 +1,41 @@
<?php
declare(strict_types=1);
namespace common\components\EmailsRenderer\Request;
class TemplateRequest {
/**
* @var string
*/
private $name;
/**
* @var string
*/
private $locale;
/**
* @var array
*/
private $params;
public function __construct(string $name, string $locale, array $params) {
$this->name = $name;
$this->locale = $locale;
$this->params = $params;
}
public function getName(): string {
return $this->name;
}
public function getLocale(): string {
return $this->locale;
}
public function getParams(): array {
return $this->params;
}
}

View File

@ -89,8 +89,9 @@ return [
'guzzle' => [
'class' => GuzzleHttp\Client::class,
],
'emailRenderer' => [
'class' => common\components\EmailRenderer::class,
'emailsRenderer' => [
'class' => common\components\EmailsRenderer\Component::class,
'serviceUrl' => getenv('EMAILS_RENDERER_HOST'),
'basePath' => '/images/emails',
],
'oauth' => [

View File

@ -1,4 +1,6 @@
<?php
declare(strict_types=1);
namespace common\emails;
class EmailHelper {

View File

@ -1,4 +1,6 @@
<?php
declare(strict_types=1);
namespace common\emails;
use common\emails\exceptions\CannotSendEmailException;
@ -20,7 +22,8 @@ abstract class Template {
private $to;
/**
* @param string|array $to получатель письма. Задаётся как Email или как массив [email => name]
* @param string|array $to message receiver. Can be passed as string (pure email)
* or as an array [email => user's name]
*/
public function __construct($to) {
$this->mailer = Yii::$app->mailer;

View File

@ -1,16 +1,19 @@
<?php
declare(strict_types=1);
namespace common\emails;
use common\components\EmailRenderer;
use Yii;
use common\components\EmailsRenderer\RendererInterface;
use ErrorException;
use Exception;
use yii\mail\MessageInterface;
abstract class TemplateWithRenderer extends Template {
/**
* @var EmailRenderer
* @var RendererInterface
*/
private $emailRenderer;
private $renderer;
/**
* @var string
@ -20,18 +23,18 @@ abstract class TemplateWithRenderer extends Template {
/**
* @inheritdoc
*/
public function __construct($to, string $locale) {
public function __construct($to, string $locale, RendererInterface $renderer) {
parent::__construct($to);
$this->emailRenderer = Yii::$app->emailRenderer;
$this->locale = $locale;
$this->renderer = $renderer;
}
public function getLocale(): string {
return $this->locale;
}
public function getEmailRenderer(): EmailRenderer {
return $this->emailRenderer;
public function getRenderer(): RendererInterface {
return $this->renderer;
}
/**
@ -46,6 +49,10 @@ abstract class TemplateWithRenderer extends Template {
return $this->getTemplateName();
}
/**
* @return MessageInterface
* @throws ErrorException
*/
protected function createMessage(): MessageInterface {
return $this->getMailer()
->compose()
@ -55,12 +62,16 @@ abstract class TemplateWithRenderer extends Template {
->setSubject($this->getSubject());
}
/**
* @return string
* @throws ErrorException
*/
private function render(): string {
return $this->getEmailRenderer()
->getTemplate($this->getTemplateName())
->setLocale($this->getLocale())
->setParams($this->getParams())
->render();
try {
return $this->getRenderer()->render($this->getTemplateName(), $this->getLocale(), $this->getParams());
} catch (Exception $e) {
throw new ErrorException('Unable to render the template', 0, 1, __FILE__, __LINE__, $e);
}
}
}

View File

@ -1,4 +1,6 @@
<?php
declare(strict_types=1);
namespace common\emails\exceptions;
use Exception;

View File

@ -1,4 +1,6 @@
<?php
declare(strict_types=1);
namespace common\emails\templates;
use common\emails\Template;

View File

@ -1,4 +1,6 @@
<?php
declare(strict_types=1);
namespace common\emails\templates;
use common\emails\Template;

View File

@ -1,6 +1,9 @@
<?php
declare(strict_types=1);
namespace common\emails\templates;
use common\components\EmailsRenderer\RendererInterface;
use common\emails\TemplateWithRenderer;
class ForgotPasswordEmail extends TemplateWithRenderer {
@ -10,8 +13,8 @@ class ForgotPasswordEmail extends TemplateWithRenderer {
/**
* @inheritdoc
*/
public function __construct($to, string $locale, ForgotPasswordParams $params) {
TemplateWithRenderer::__construct($to, $locale);
public function __construct($to, string $locale, ForgotPasswordParams $params, RendererInterface $renderer) {
parent::__construct($to, $locale, $renderer);
$this->params = $params;
}

View File

@ -1,4 +1,6 @@
<?php
declare(strict_types=1);
namespace common\emails\templates;
class ForgotPasswordParams {

View File

@ -1,6 +1,9 @@
<?php
declare(strict_types=1);
namespace common\emails\templates;
use common\components\EmailsRenderer\RendererInterface;
use common\emails\TemplateWithRenderer;
class RegistrationEmail extends TemplateWithRenderer {
@ -10,8 +13,8 @@ class RegistrationEmail extends TemplateWithRenderer {
/**
* @inheritdoc
*/
public function __construct($to, string $locale, RegistrationEmailParams $params) {
TemplateWithRenderer::__construct($to, $locale);
public function __construct($to, string $locale, RegistrationEmailParams $params, RendererInterface $renderer) {
parent::__construct($to, $locale, $renderer);
$this->params = $params;
}

View File

@ -1,4 +1,6 @@
<?php
declare(strict_types=1);
namespace common\emails\templates;
class RegistrationEmailParams {

View File

@ -50,7 +50,7 @@ class SendPasswordRecoveryEmail implements RetryableJobInterface {
Yii::$app->statsd->inc('queue.sendPasswordRecovery.attempt');
$params = new ForgotPasswordParams($this->username, $this->code, $this->link);
$to = EmailHelper::buildTo($this->username, $this->email);
$template = new ForgotPasswordEmail($to, $this->locale, $params);
$template = new ForgotPasswordEmail($to, $this->locale, $params, Yii::$app->emailsRenderer);
$template->send();
}

View File

@ -50,7 +50,7 @@ class SendRegistrationEmail implements RetryableJobInterface {
Yii::$app->statsd->inc('queue.sendRegistrationEmail.attempt');
$params = new RegistrationEmailParams($this->username, $this->code, $this->link);
$to = EmailHelper::buildTo($this->username, $this->email);
$template = new RegistrationEmail($to, $this->locale, $params);
$template = new RegistrationEmail($to, $this->locale, $params, Yii::$app->emailsRenderer);
$template->send();
}

View File

@ -1,7 +1,7 @@
<?php
namespace common\tests\unit\emails;
use common\components\EmailRenderer;
use common\components\EmailsRenderer\Component;
use common\emails\TemplateWithRenderer;
use common\tests\_support\ProtectedCaller;
use common\tests\unit\TestCase;
@ -18,7 +18,7 @@ class TemplateWithRendererTest extends TestCase {
$this->assertSame('mock-to', $template->getTo());
$this->assertSame('mock-locale', $template->getLocale());
$this->assertInstanceOf(MailerInterface::class, $template->getMailer());
$this->assertInstanceOf(EmailRenderer::class, $template->getEmailRenderer());
$this->assertInstanceOf(Component::class, $template->getRenderer());
}
public function testCreateMessage() {
@ -26,8 +26,8 @@ class TemplateWithRendererTest extends TestCase {
$templateBuilder = mock(TemplateBuilder::class)->makePartial();
$templateBuilder->shouldReceive('render')->andReturn('mock-html');
/** @var EmailRenderer|\Mockery\MockInterface $renderer */
$renderer = mock(EmailRenderer::class)->makePartial();
/** @var Component|\Mockery\MockInterface $renderer */
$renderer = mock(Component::class)->makePartial();
$renderer->shouldReceive('getTemplate')->with('mock-template')->andReturn($templateBuilder);
/** @var TemplateWithRenderer|\Mockery\MockInterface $template */

View File

@ -13,7 +13,6 @@
"ext-simplexml": "*",
"bacon/bacon-qr-code": "^1.0",
"domnikl/statsd": "^2.6",
"ely/email-renderer": "dev-master",
"ely/mojang-api": "^0.2.0",
"ely/yii2-tempmail-validator": "^2.0",
"emarref/jwt": "~1.0.3",
@ -52,10 +51,6 @@
{
"type": "composer",
"url": "https://asset-packagist.org"
},
{
"type": "git",
"url": "https://gitlab+deploy-token-1:FDGgmcnLdykcsyJJ_8Uv@gitlab.ely.by/elyby/email-renderer.git"
}
],
"config": {

45
composer.lock generated
View File

@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically"
],
"content-hash": "68086c7aa86b976c390b02cc53b182f9",
"content-hash": "752322170e3433ba42cb90c1dd6122a2",
"packages": [
{
"name": "bacon/bacon-qr-code",
@ -166,7 +166,7 @@
"version": "3.27.0",
"source": {
"type": "git",
"url": "git@github.com:getsentry/raven-js-bower.git",
"url": "https://github.com/getsentry/raven-js-bower.git",
"reference": "c8b3a6040be6928e2f57fa5eec4d7afc31750235"
},
"dist": {
@ -606,46 +606,6 @@
],
"time": "2018-12-04T22:38:24+00:00"
},
{
"name": "ely/email-renderer",
"version": "dev-master",
"source": {
"type": "git",
"url": "https://gitlab+deploy-token-1:FDGgmcnLdykcsyJJ_8Uv@gitlab.ely.by/elyby/email-renderer.git",
"reference": "3b06c19b39a298e6cb00f711b5fed40c315cd95e"
},
"require": {
"php": ">=5.6.0"
},
"type": "library",
"autoload": {
"psr-4": {
"Ely\\Email\\": "src-php/"
}
},
"license": [
"MIT"
],
"authors": [
{
"name": "Ely.by team",
"email": "team@ely.by"
},
{
"name": "ErickSkrauch",
"email": "erickskrauch@ely.by"
},
{
"name": "SleepWalker",
"email": "dev@udf.su"
}
],
"homepage": "http://ely.by",
"keywords": [
""
],
"time": "2019-05-13T21:56:45+00:00"
},
{
"name": "ely/mojang-api",
"version": "0.2.0",
@ -6180,7 +6140,6 @@
"aliases": [],
"minimum-stability": "stable",
"stability-flags": {
"ely/email-renderer": 20,
"roave/security-advisories": 20
},
"prefer-stable": false,