|
| 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 | +} |
0 commit comments