Last active
November 21, 2019 07:23
-
-
Save HoelShare/b854064fec8ec23c59a3a1e18675435a to your computer and use it in GitHub Desktop.
Extend MailService and add custom data to every event
This file contains 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
<?php declare(strict_types=1); | |
namespace ExampleMailExtender\Core; | |
use Psr\Log\LoggerInterface; | |
use Shopware\Core\Content\MailTemplate\Service\MailSender; | |
use Shopware\Core\Content\MailTemplate\Service\MailService as DecoratedService; | |
use Shopware\Core\Content\MailTemplate\Service\MessageFactory; | |
use Shopware\Core\Framework\Context; | |
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface; | |
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria; | |
use Shopware\Core\Framework\Twig\StringTemplateRenderer; | |
use Shopware\Core\Framework\Validation\DataValidator; | |
use Shopware\Core\System\SalesChannel\SalesChannelDefinition; | |
use Shopware\Core\System\SystemConfig\SystemConfigService; | |
use Symfony\Component\EventDispatcher\EventDispatcherInterface; | |
class MailService extends DecoratedService | |
{ | |
/** | |
* @var DecoratedService | |
*/ | |
private $decoratedService; | |
/** | |
* @var EntityRepositoryInterface | |
*/ | |
private $customRepository; | |
public function __construct( | |
DataValidator $dataValidator, | |
StringTemplateRenderer $templateRenderer, | |
MessageFactory $messageFactory, | |
MailSender $mailSender, | |
EntityRepositoryInterface $mediaRepository, | |
SalesChannelDefinition $salesChannelDefinition, | |
EntityRepositoryInterface $salesChannelRepository, | |
SystemConfigService $systemConfigService, | |
EventDispatcherInterface $eventDispatcher, | |
LoggerInterface $logger, | |
DecoratedService $decoratedService, | |
EntityRepositoryInterface $customRepository | |
) { | |
parent::__construct( | |
$dataValidator, | |
$templateRenderer, | |
$messageFactory, | |
$mailSender, | |
$mediaRepository, | |
$salesChannelDefinition, | |
$salesChannelRepository, | |
$systemConfigService, | |
$eventDispatcher, | |
$logger | |
); | |
$this->decoratedService = $decoratedService; | |
$this->customRepository = $customRepository; | |
} | |
public function send(array $data, Context $context, array $templateData = []): ?\Swift_Message | |
{ | |
// fetch my data | |
$myEntity = $this->customRepository->search((new Criteria())->setLimit(1), $context)->first(); | |
$templateData['myEntity'] = $myEntity; | |
return $this->decoratedService->send($data, $context, $templateData); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment