Skip to content

Commit 262a95a

Browse files
feat: add debug info to subject (#69)
* feat: add debug info to subject feat: add debug info to subject * fix: fix tests
1 parent f2c09a4 commit 262a95a

File tree

3 files changed

+61
-6
lines changed

3 files changed

+61
-6
lines changed

src/Resources/config/config.xml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:noNamespaceSchemaLocation="https://raw.githubusercontent.com/shopware/platform/master/src/Core/System/SystemConfig/Schema/config.xsd">
4+
<card>
5+
<title>Debug mode</title>
6+
<title lang="de-DE">Debug-Modus</title>
7+
8+
<input-field type="bool">
9+
<name>debugMode</name>
10+
<label>Enable debug mode</label>
11+
<label lang="de-DE">Debug-Modus aktivieren</label>
12+
<helpText>Adds the technical name of the mail-template to the subject to easily identify which mail has been send</helpText>
13+
<helpText lang="de-DE">Fügt den technischen Namen der Mail-Vorlage zum Betreff hinzu, um leicht zu erkennen, welche Mail gesendet wurde</helpText>
14+
</input-field>
15+
</card>
16+
</config>

src/Subscriber/FlowSubscriber.php

Lines changed: 42 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919
use Shopware\Core\Framework\Validation\DataBag\DataBag;
2020
use Shopware\Core\System\Language\LanguageCollection;
2121
use Shopware\Core\System\Language\LanguageEntity;
22+
use Shopware\Core\System\SalesChannel\SalesChannelCollection;
23+
use Shopware\Core\System\SalesChannel\SalesChannelEntity;
24+
use Shopware\Core\System\SystemConfig\SystemConfigService;
2225
use Symfony\Component\DependencyInjection\Attribute\Autowire;
2326
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
2427
use Symfony\Component\HttpFoundation\ParameterBag;
@@ -28,13 +31,16 @@ class FlowSubscriber implements EventSubscriberInterface
2831
/**
2932
* @param EntityRepository<MailTemplateTypeCollection> $mailTemplateTypeRepository
3033
* @param EntityRepository<LanguageCollection> $languageRepository
34+
* @param EntityRepository<SalesChannelCollection> $salesChannelRepository
3135
*/
3236
public function __construct(
3337
private readonly EntityRepository $mailTemplateTypeRepository,
3438
private readonly MailFinderServiceInterface $mailFinderService,
3539
#[Autowire(service: Translator::class)]
3640
private readonly AbstractTranslator $translator,
3741
private readonly EntityRepository $languageRepository,
42+
private readonly SystemConfigService $systemConfigService,
43+
private readonly EntityRepository $salesChannelRepository,
3844
) {}
3945

4046
public static function getSubscribedEvents(): array
@@ -77,6 +83,17 @@ private function sendMail(DataBag $dataBag, string $mailTemplateTypeId, Context
7783
}
7884

7985
if ($subject) {
86+
$salesChannelId = $dataBag->getString('salesChannelId');
87+
$debugMode = $this->systemConfigService->getBool('FroshPlatformTemplateMail.config.debugMode', $salesChannelId);
88+
if ($debugMode) {
89+
$subject = sprintf(
90+
'DEBUG: %s (%s - %s - %s)',
91+
$subject,
92+
$this->getSalesChannelName($salesChannelId, $context),
93+
$this->getLocaleCode($context->getLanguageId(), $context),
94+
$technicalName,
95+
);
96+
}
8097
$dataBag->set('subject', $subject);
8198
}
8299
}
@@ -93,13 +110,11 @@ private function createTemplateMailContext(ParameterBag $dataBag, Context $conte
93110

94111
private function fixTranslator(TemplateMailContext $businessEvent): void
95112
{
96-
$criteria = new Criteria([$businessEvent->getContext()->getLanguageId()]);
97-
$criteria->addAssociation('locale');
98-
99-
/** @var LanguageEntity $language */
100-
$language = $this->languageRepository->search($criteria, $businessEvent->getContext())->first();
113+
$localCode = $this->getLocaleCode(
114+
$businessEvent->getContext()->getLanguageId(),
115+
$businessEvent->getContext(),
116+
);
101117

102-
$localCode = $language->getLocale()?->getCode();
103118
if ($localCode === null) {
104119
return;
105120
}
@@ -111,4 +126,25 @@ private function fixTranslator(TemplateMailContext $businessEvent): void
111126
$businessEvent->getContext(),
112127
);
113128
}
129+
130+
private function getLocaleCode(string $languageId, Context $context): ?string
131+
{
132+
$criteria = new Criteria([$languageId]);
133+
$criteria->addAssociation('locale');
134+
135+
/** @var LanguageEntity $language */
136+
$language = $this->languageRepository->search($criteria, $context)->first();
137+
138+
return $language->getLocale()?->getCode();
139+
}
140+
141+
private function getSalesChannelName(string $salesChannelId, Context $context): ?string
142+
{
143+
$criteria = new Criteria([$salesChannelId]);
144+
145+
/** @var SalesChannelEntity $salesChannel */
146+
$salesChannel = $this->salesChannelRepository->search($criteria, $context)->first();
147+
148+
return $salesChannel->getName();
149+
}
114150
}

tests/Subscriber/FlowSubscriberTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Shopware\Core\Framework\Context;
1515
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
1616
use Shopware\Core\Framework\Validation\DataBag\DataBag;
17+
use Shopware\Core\System\SystemConfig\SystemConfigService;
1718

1819
class FlowSubscriberTest extends TestCase
1920
{
@@ -34,6 +35,8 @@ public function testEmptyMailDoesNothing(): void
3435
$this->createMock(MailFinderServiceInterface::class),
3536
$this->createMock(AbstractTranslator::class),
3637
$this->createMock(EntityRepository::class),
38+
$this->createMock(SystemConfigService::class),
39+
$this->createMock(EntityRepository::class),
3740
);
3841

3942
$subscriber->onFlowSendMailActionEvent(new FlowSendMailActionEvent(new DataBag([]), new MailTemplateEntity(), new StorableFlow('test', Context::createDefaultContext())));

0 commit comments

Comments
 (0)