Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/code-style.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ jobs:
strategy:
fail-fast: false
matrix:
version-selection: [ 'highest' ]
version-selection: [ 'lowest', 'highest' ]
steps:
- name: Checkout
uses: actions/checkout@v4
Expand Down
9 changes: 6 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "frosh/frosh-platform-template-mail",
"version": "3.0.7",
"version": "4.0.0",
"type": "shopware-platform-plugin",
"description": "Load mail templates from theme",
"keywords": [
Expand Down Expand Up @@ -45,10 +45,13 @@
}
},
"require": {
"shopware/core": "~6.5.0 || ~6.6.0 || ~6.7.0"
"shopware/core": "~6.5.8 || ~6.6.0 || ~6.7.0"
},
"scripts": {
"format": "docker run --rm -v $(pwd):/ext shopware/shopware-cli:latest extension format /ext",
"check": "docker run --rm -v $(pwd):/ext shopware/shopware-cli:latest extension validate --full /ext"
"check": [
"docker run --rm -v $(pwd):/ext shopware/shopware-cli:latest extension validate --check-against lowest --full /ext",
"docker run --rm -v $(pwd):/ext shopware/shopware-cli:latest extension validate --check-against highest --full /ext"
]
}
}
20 changes: 20 additions & 0 deletions src/DTO/TemplateData.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php declare(strict_types=1);

namespace Frosh\TemplateMail\DTO;

class TemplateData
{
public function __construct(
public ?TypeData $subject = null,
public ?TypeData $html = null,
public ?TypeData $plain = null,
) {
}

public function isAllSet(): bool
{
return $this->subject !== null
&& $this->html !== null
&& $this->plain !== null;
}
}
19 changes: 19 additions & 0 deletions src/DTO/TemplateType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php declare(strict_types=1);

namespace Frosh\TemplateMail\DTO;

enum TemplateType
{
case SUBJECT;
case HTML;
case PLAIN;

public function filePart(): string
{
return match ($this) {
self::SUBJECT => 'subject.',
self::HTML => 'html.',
self::PLAIN => 'plain.',
};
}
}
12 changes: 12 additions & 0 deletions src/DTO/TypeData.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php declare(strict_types=1);

namespace Frosh\TemplateMail\DTO;

class TypeData
{
public function __construct(
public string $filePath,
public string $content
) {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
<template #column-froshTemplateMail="{ item }">
<sw-label
appearance="circle"
:variant="item.extensions?.froshTemplateMail.subject ? 'success' : 'warning'"
:variant="item.extensions?.froshTemplateMail?.subject ? 'success' : 'warning'"
v-tooltip="getFroshTooltip('subject', item.extensions.froshTemplateMail.subject)"
>
<sw-icon
Expand All @@ -56,7 +56,7 @@
</sw-label>
<sw-label
appearance="circle"
:variant="item.extensions?.froshTemplateMail.plain ? 'success' : 'warning'"
:variant="item.extensions?.froshTemplateMail?.plain ? 'success' : 'warning'"
v-tooltip="getFroshTooltip('plain', item.extensions.froshTemplateMail.plain)"
>
<sw-icon
Expand All @@ -66,7 +66,7 @@
</sw-label>
<sw-label
appearance="circle"
:variant="item.extensions?.froshTemplateMail.html ? 'success' : 'warning'"
:variant="item.extensions?.froshTemplateMail?.html ? 'success' : 'warning'"
v-tooltip="getFroshTooltip('html', item.extensions.froshTemplateMail.html)"
>
<sw-icon
Expand Down
19 changes: 7 additions & 12 deletions src/Services/CachedMailFinderService.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Frosh\TemplateMail\Services;

use Frosh\TemplateMail\DTO\TemplateData;
use Symfony\Component\DependencyInjection\Attribute\AsDecorator;
use Symfony\Component\DependencyInjection\Attribute\When;
use Symfony\Contracts\Cache\CacheInterface;
Expand All @@ -19,29 +20,23 @@ public function __construct(
) {
}

public function findTemplateByTechnicalName(
string $type,
public function getTemplateDataByTechnicalName(
string $technicalName,
TemplateMailContext $businessEvent,
bool $returnFolder = false,
?string $mailTemplateId = null,
): ?string {
$salesChannelId = $businessEvent->getSalesChannelId();

): TemplateData {
$cacheKey = hash(
'xxh128',
$type
. $technicalName
$technicalName
. $mailTemplateId
. $salesChannelId
. $businessEvent->getSalesChannelId()
. $businessEvent->getContext()->getLanguageId()
. $returnFolder,
);

return $this->cache->get($cacheKey, function (ItemInterface $cacheItem) use ($type, $technicalName, $businessEvent, $returnFolder, $mailTemplateId) {
return $this->cache->get($cacheKey, function (ItemInterface $cacheItem) use ($technicalName, $businessEvent, $mailTemplateId) {
$cacheItem->expiresAfter(3600);

return $this->mailFinderService->findTemplateByTechnicalName($type, $technicalName, $businessEvent, $returnFolder, $mailTemplateId);
return $this->mailFinderService->getTemplateDataByTechnicalName($technicalName, $businessEvent, $mailTemplateId);
});
}
}
86 changes: 56 additions & 30 deletions src/Services/MailFinderService.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
namespace Frosh\TemplateMail\Services;

use Doctrine\DBAL\Connection;
use Frosh\TemplateMail\DTO\TemplateData;
use Frosh\TemplateMail\DTO\TemplateType;
use Frosh\TemplateMail\DTO\TypeData;
use Frosh\TemplateMail\Services\MailLoader\LoaderInterface;
use Shopware\Core\Framework\Uuid\Uuid;
use Symfony\Component\DependencyInjection\Attribute\AsAlias;
Expand All @@ -16,10 +19,6 @@
#[AsAlias]
class MailFinderService implements MailFinderServiceInterface
{
final public const TYPE_HTML = 'html.';
final public const TYPE_PLAIN = 'plain.';
final public const TYPE_SUBJECT = 'subject.';

/**
* @param LoaderInterface[] $availableLoaders
* @param array<BundleInterface> $bundles
Expand All @@ -36,55 +35,56 @@ public function __construct(
) {
}

public function findTemplateByTechnicalName(
string $type,
public function getTemplateDataByTechnicalName(
string $technicalName,
TemplateMailContext $businessEvent,
bool $returnFolder = false,
?string $mailTemplateId = null,
): ?string {
): TemplateData {
$templateData = new TemplateData();

$paths = $this->filesystemLoader->getPaths();

$searchFolder = $this->searchPathProvider->buildPaths($businessEvent);
$searchFolders = $this->searchPathProvider->buildPaths($businessEvent);

$themePath = $this->findPathOfThemeFromPluginOrApp($businessEvent->getSalesChannelId())
?? $this->findPathOfThemeFromSymfonyBundle($businessEvent->getSalesChannelId());

if (\is_string($themePath)) {
usort($paths, static function ($a, $b) use ($themePath) {
if (str_contains($a, $themePath)) {
return -1;
}

if (str_contains($b, $themePath)) {
return 1;
}

return 0;
});
\usort(
$paths,
static fn ($a, $b) => \str_contains($b, $themePath) <=> \str_contains($a, $themePath)
);
}

foreach ($paths as $path) {
foreach ($this->availableLoaders as $availableLoader) {
$supportedExtensions = $availableLoader->supportedExtensions();
$viewMailFolderPath = $path . '/email';
if (!\is_dir($viewMailFolderPath)) {
continue;
}

foreach ($this->availableLoaders as $loader) {
$supportedExtensions = $loader->supportedExtensions();

foreach ($supportedExtensions as $supportedExtension) {
foreach ($searchFolder as $folder) {
$filePath = $path . '/email/' . $folder . '/' . $technicalName . '/' . $mailTemplateId . '/' . $type . $supportedExtension;
if (file_exists($filePath) && $content = $availableLoader->load($filePath)) {
return $returnFolder ? $filePath : $content;
foreach ($searchFolders as $folder) {
$folderPath = $viewMailFolderPath . '/' . $folder . '/' . $technicalName;
if (!\is_dir($folderPath)) {
continue;
}

$filePath = $path . '/email/' . $folder . '/' . $technicalName . '/' . $type . $supportedExtension;
if (file_exists($filePath) && $content = $availableLoader->load($filePath)) {
return $returnFolder ? $filePath : $content;
$templateData->subject ??= $this->loadTypeData(TemplateType::SUBJECT, $loader, $supportedExtension, $folderPath, $mailTemplateId);
$templateData->html ??= $this->loadTypeData(TemplateType::HTML, $loader, $supportedExtension, $folderPath, $mailTemplateId);
$templateData->plain ??= $this->loadTypeData(TemplateType::PLAIN, $loader, $supportedExtension, $folderPath, $mailTemplateId);

if ($templateData->isAllSet()) {
return $templateData;
}
}
}
}
}

return null;
return $templateData;
}

public function findPathOfThemeFromPluginOrApp(string $salesChannelId): ?string
Expand Down Expand Up @@ -128,4 +128,30 @@ public function findPathOfThemeFromSymfonyBundle(string $salesChannelId): ?strin

return null;
}

private function loadTypeData(
TemplateType $type,
LoaderInterface $loader,
string $supportedExtension,
string $folderPath,
?string $mailTemplateId
): ?TypeData {
$filePath = $folderPath . '/' . $mailTemplateId . '/' . $type->filePart() . $supportedExtension;
if (\is_file($filePath) && $content = $loader->load($filePath)) {
return new TypeData(
$filePath,
$content,
);
}

$filePath = $folderPath . '/' . $type->filePart() . $supportedExtension;
if (\is_file($filePath) && $content = $loader->load($filePath)) {
return new TypeData(
$filePath,
$content,
);
}

return null;
}
}
8 changes: 4 additions & 4 deletions src/Services/MailFinderServiceInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@

namespace Frosh\TemplateMail\Services;

use Frosh\TemplateMail\DTO\TemplateData;

interface MailFinderServiceInterface
{
public function findTemplateByTechnicalName(
string $type,
public function getTemplateDataByTechnicalName(
string $technicalName,
TemplateMailContext $businessEvent,
bool $returnFolder = false,
?string $mailTemplateId = null,
): ?string;
): TemplateData;
}
5 changes: 3 additions & 2 deletions src/Services/SearchPathProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,9 @@ public function buildPaths(TemplateMailContext $businessEvent): array
{
$searchFolder = [$businessEvent->getContext()->getLanguageId(), 'global'];

if ($businessEvent->getContext()->getSource() instanceof SalesChannelApiSource) {
array_unshift($searchFolder, $businessEvent->getContext()->getSource()->getSalesChannelId());
$contextSource = $businessEvent->getContext()->getSource();
if ($contextSource instanceof SalesChannelApiSource) {
array_unshift($searchFolder, $contextSource->getSalesChannelId());
}

$criteria = new Criteria($businessEvent->getContext()->getLanguageIdChain());
Expand Down
Loading