Skip to content

Commit

Permalink
Add search of mail templates in symfony-bundles (#66)
Browse files Browse the repository at this point in the history
If you used Symfony bundles to add multiple themes, they weren't found when the template finder searched for the matching theme of the salesChannel resulting in it taking the first theme in alphabetical order.

When no Shopware app or plugin was found, it now searches for matching bundles.
  • Loading branch information
Vhulcan authored Apr 13, 2024
1 parent 7255fe1 commit 330ff48
Showing 1 changed file with 39 additions and 11 deletions.
50 changes: 39 additions & 11 deletions src/Services/MailFinderService.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Symfony\Component\DependencyInjection\Attribute\AsAlias;
use Symfony\Component\DependencyInjection\Attribute\Autowire;
use Symfony\Component\DependencyInjection\Attribute\TaggedIterator;
use Symfony\Component\HttpKernel\Bundle\BundleInterface;
use Twig\Loader\FilesystemLoader;

#[AsAlias]
Expand All @@ -21,14 +22,17 @@ class MailFinderService implements MailFinderServiceInterface

/**
* @param LoaderInterface[] $availableLoaders
* @param iterable<BundleInterface> $bundles
*/
public function __construct(
#[Autowire(service: 'twig.loader.native_filesystem')]
private readonly FilesystemLoader $filesystemLoader,
#[TaggedIterator('frosh_template_mail.loader')]
private readonly iterable $availableLoaders,
private readonly SearchPathProvider $searchPathProvider,
private readonly Connection $connection
private readonly Connection $connection,
#[Autowire(service: 'kernel.bundles')]
private readonly iterable $bundles,
) {}

public function findTemplateByTechnicalName(
Expand All @@ -41,16 +45,8 @@ public function findTemplateByTechnicalName(

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

$stmt = $this->connection->prepare(
'SELECT IFNULL(a.path, p.path) AS `path` FROM `theme` AS t '
. 'LEFT JOIN `theme_sales_channel` AS tsc ON tsc.`theme_id` = t.`id` '
. 'LEFT JOIN `plugin` AS p ON p.`name` = t.`technical_name` '
. 'LEFT JOIN `app` AS a ON a.`name` = t.`technical_name` '
. 'WHERE tsc.`sales_channel_id` = ?;'
);

$stmt->bindValue(1, Uuid::fromHexToBytes($businessEvent->getSalesChannelId()));
$themePath = $stmt->executeQuery()->fetchOne();
$themePath = $this->findPathOfThemeFromPluginOrApp($businessEvent->getSalesChannelId())
?? $this->findPathOfThemeFromSymfonyBundle($businessEvent->getSalesChannelId());

if (\is_string($themePath)) {
usort($paths, static function ($a, $b) use ($themePath) {
Expand Down Expand Up @@ -83,4 +79,36 @@ public function findTemplateByTechnicalName(

return null;
}

public function findPathOfThemeFromPluginOrApp(string $salesChannelId): ?string {
$stmt = $this->connection->prepare(
'SELECT IFNULL(a.path, p.path) AS `path` FROM `theme` AS t '
. 'LEFT JOIN `theme_sales_channel` AS tsc ON tsc.`theme_id` = t.`id` '
. 'LEFT JOIN `plugin` AS p ON p.`name` = t.`technical_name` '
. 'LEFT JOIN `app` AS a ON a.`name` = t.`technical_name` '
. 'WHERE tsc.`sales_channel_id` = ?;'
);

$stmt->bindValue(1, Uuid::fromHexToBytes($salesChannelId));

return $stmt->executeQuery()->fetchOne();

Check failure on line 94 in src/Services/MailFinderService.php

View workflow job for this annotation

GitHub Actions / phpstan / Static Analyse

Method Frosh\TemplateMail\Services\MailFinderService::findPathOfThemeFromPluginOrApp() should return string|null but returns mixed.
}

public function findPathOfThemeFromSymfonyBundle(string $salesChannelId): ?string {
$stmt = $this->connection->prepare(
'SELECT t.`technical_name` FROM `theme` AS t '
. 'LEFT JOIN `theme_sales_channel` AS tsc ON tsc.`theme_id` = t.`id` '
. 'WHERE tsc.`sales_channel_id` = ?;'
);

$stmt->bindValue(1, Uuid::fromHexToBytes($salesChannelId));

$technicalName = $stmt->executeQuery()->fetchOne();

if (isset($this->bundles[$technicalName])) {
return $this->bundles[$technicalName]->getPath();
}

return null;
}
}

0 comments on commit 330ff48

Please sign in to comment.