Skip to content

Commit d09e468

Browse files
committed
Add an image resolver to provide an image to the email
1 parent 5acf0f0 commit d09e468

File tree

6 files changed

+102
-2
lines changed

6 files changed

+102
-2
lines changed

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
"doctrine/orm": "^2.0",
99
"doctrine/persistence": "^2.0 || ^3.0",
1010
"knplabs/knp-menu": "^3.7",
11+
"liip/imagine-bundle": "^2.0",
1112
"psr/log": "^1.0 || ^2.0 || ^3.0",
1213
"setono/doctrine-orm-trait": "^1.2",
1314
"sylius/channel": "^1.0",

src/EmailManager/RestockNotificationEmailManager.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,16 @@
66

77
use Setono\SyliusRestockNotificationPlugin\Mailer\Emails;
88
use Setono\SyliusRestockNotificationPlugin\Model\RestockNotificationRequestInterface;
9+
use Setono\SyliusRestockNotificationPlugin\Resolver\ImageUrlResolverInterface;
910
use Sylius\Component\Mailer\Sender\SenderInterface;
1011
use Webmozart\Assert\Assert;
1112

1213
final class RestockNotificationEmailManager implements RestockNotificationEmailManagerInterface
1314
{
14-
public function __construct(private readonly SenderInterface $sender)
15-
{
15+
public function __construct(
16+
private readonly SenderInterface $sender,
17+
private readonly ImageUrlResolverInterface $imageUrlResolver,
18+
) {
1619
}
1720

1821
public function sendRestockNotificationEmail(RestockNotificationRequestInterface $restockNotificationRequest): void
@@ -34,6 +37,7 @@ public function sendRestockNotificationEmail(RestockNotificationRequestInterface
3437
'restockNotificationRequest' => $restockNotificationRequest,
3538
'channel' => $channel,
3639
'localeCode' => $localeCode,
40+
'imageUrl' => $this->imageUrlResolver->resolve($restockNotificationRequest),
3741
],
3842
)
3943
;

src/Resolver/ImageUrlResolver.php

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Setono\SyliusRestockNotificationPlugin\Resolver;
6+
7+
use Liip\ImagineBundle\Imagine\Cache\CacheManager;
8+
use Setono\SyliusRestockNotificationPlugin\Model\RestockNotificationRequestInterface;
9+
use Sylius\Component\Core\Model\ProductInterface;
10+
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
11+
12+
final class ImageUrlResolver implements ImageUrlResolverInterface
13+
{
14+
public function __construct(
15+
private readonly CacheManager $cacheManager,
16+
private readonly string $filter = 'sylius_shop_product_large_thumbnail',
17+
) {
18+
}
19+
20+
public function resolve(RestockNotificationRequestInterface $restockNotificationRequest): ?string
21+
{
22+
$product = $restockNotificationRequest->getProductVariant()?->getProduct();
23+
if (!$product instanceof ProductInterface) {
24+
return null;
25+
}
26+
27+
$image = $product->getImages()->first();
28+
if (false === $image) {
29+
return null;
30+
}
31+
32+
$path = $image->getPath();
33+
if (null === $path) {
34+
return null;
35+
}
36+
37+
$hostname = self::getChannelHostname($restockNotificationRequest);
38+
if (null === $hostname) {
39+
return $this->cacheManager->getBrowserPath($path, $this->filter);
40+
}
41+
42+
return sprintf(
43+
'%s%s',
44+
$hostname,
45+
$this->cacheManager->getBrowserPath(path: $path, filter: $this->filter, referenceType: UrlGeneratorInterface::ABSOLUTE_PATH),
46+
);
47+
}
48+
49+
/**
50+
* @return non-empty-string|null
51+
*/
52+
private static function getChannelHostname(RestockNotificationRequestInterface $restockNotificationRequest): ?string
53+
{
54+
$channel = $restockNotificationRequest->getChannel();
55+
if (null === $channel) {
56+
return null;
57+
}
58+
59+
$hostname = $channel->getHostname();
60+
if (null === $hostname || '' === $hostname) {
61+
return null;
62+
}
63+
64+
return sprintf('https://%s', $hostname);
65+
}
66+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Setono\SyliusRestockNotificationPlugin\Resolver;
6+
7+
use Setono\SyliusRestockNotificationPlugin\Model\RestockNotificationRequestInterface;
8+
9+
interface ImageUrlResolverInterface
10+
{
11+
/**
12+
* Will resolve an image URL based on a restock notification request
13+
*/
14+
public function resolve(RestockNotificationRequestInterface $restockNotificationRequest): ?string;
15+
}

src/Resources/config/services.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
<import resource="services/menu.xml"/>
1313
<import resource="services/message.xml"/>
1414
<import resource="services/notifier.xml"/>
15+
<import resource="services/resolver.xml"/>
1516
<import resource="services/twig.xml"/>
1617
</imports>
1718
</container>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?xml version="1.0" encoding="UTF-8" ?>
2+
3+
<container xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://symfony.com/schema/dic/services"
4+
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
5+
<services>
6+
<service id="Setono\SyliusRestockNotificationPlugin\Resolver\ImageUrlResolverInterface"
7+
alias="Setono\SyliusRestockNotificationPlugin\Resolver\ImageUrlResolver"/>
8+
9+
<service id="Setono\SyliusRestockNotificationPlugin\Resolver\ImageUrlResolver">
10+
<argument type="service" id="liip_imagine.cache.manager"/>
11+
</service>
12+
</services>
13+
</container>

0 commit comments

Comments
 (0)