Skip to content

Instantly share code, notes, and snippets.

@HoelShare
Last active November 21, 2019 07:23
Show Gist options
  • Save HoelShare/b854064fec8ec23c59a3a1e18675435a to your computer and use it in GitHub Desktop.
Save HoelShare/b854064fec8ec23c59a3a1e18675435a to your computer and use it in GitHub Desktop.
Extend MailService and add custom data to every event
<?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