custom/plugins/FroshPlatformThumbnailProcessor/src/Service/UrlGeneratorDecorator.php line 119

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Frosh\ThumbnailProcessor\Service;
  3. use Shopware\Core\Content\Media\Aggregate\MediaThumbnail\MediaThumbnailEntity;
  4. use Shopware\Core\Content\Media\MediaEntity;
  5. use Shopware\Core\Content\Media\MediaType\ImageType;
  6. use Shopware\Core\Content\Media\Pathname\UrlGeneratorInterface;
  7. use Shopware\Core\System\SystemConfig\SystemConfigService;
  8. use Symfony\Component\HttpFoundation\RequestStack;
  9. class UrlGeneratorDecorator implements UrlGeneratorInterface
  10. {
  11.     /**
  12.      * @var RequestStack
  13.      */
  14.     private $requestStack;
  15.     /**
  16.      * @var string|null
  17.      */
  18.     private $baseUrl;
  19.     /**
  20.      * @var UrlGeneratorInterface
  21.      */
  22.     private $decoratedService;
  23.     /**
  24.      * @var ThumbnailUrlTemplateInterface
  25.      */
  26.     private $thumbnailUrlTemplate;
  27.     /**
  28.      * @var bool
  29.      */
  30.     private $processSVG;
  31.     /**
  32.      * @var bool
  33.      */
  34.     private $processOriginalImages;
  35.     public function __construct(
  36.         UrlGeneratorInterface $decoratedService,
  37.         ThumbnailUrlTemplateInterface $thumbnailUrlTemplate,
  38.         RequestStack $requestStack,
  39.         SystemConfigService $systemConfigService,
  40.         ?string $baseUrl null
  41.     )
  42.     {
  43.         $this->decoratedService $decoratedService;
  44.         $this->requestStack $requestStack;
  45.         $this->baseUrl $this->normalizeBaseUrl($baseUrl);
  46.         $this->thumbnailUrlTemplate $thumbnailUrlTemplate;
  47.         $this->processSVG = (bool)$systemConfigService->get('FroshPlatformThumbnailProcessor.config.ProcessSVG');
  48.         $this->processOriginalImages = (bool)$systemConfigService->get('FroshPlatformThumbnailProcessor.config.ProcessOriginalImages');
  49.     }
  50.     public function getAbsoluteMediaUrl(MediaEntity $media): string
  51.     {
  52.         if (!($media->getMediaType() instanceof ImageType)) {
  53.             return $this->decoratedService->getAbsoluteMediaUrl($media);
  54.         }
  55.         if (!$this->processOriginalImages) {
  56.             return $this->decoratedService->getAbsoluteMediaUrl($media);
  57.         }
  58.         if (!$this->processSVG && $media->getFileExtension() === 'svg') {
  59.             return $this->decoratedService->getAbsoluteMediaUrl($media);
  60.         }
  61.         return $this->thumbnailUrlTemplate->getUrl(
  62.             $this->getBaseUrl(),
  63.             $this->getRelativeMediaUrl($media),
  64.             "3000",
  65.             "3000"
  66.         );
  67.     }
  68.     public function getRelativeMediaUrl(MediaEntity $media): string
  69.     {
  70.         return $this->decoratedService->getRelativeMediaUrl($media);
  71.     }
  72.     public function getAbsoluteThumbnailUrl(MediaEntity $mediaMediaThumbnailEntity $thumbnail): string
  73.     {
  74.         if (!$this->processSVG && $media->getFileExtension() === 'svg') {
  75.             return $this->decoratedService->getAbsoluteMediaUrl($media);
  76.         }
  77.         return $this->thumbnailUrlTemplate->getUrl(
  78.             $this->getBaseUrl(),
  79.             $this->decoratedService->getRelativeMediaUrl($media),
  80.             (string) $thumbnail->getWidth(),
  81.             (string) $thumbnail->getHeight());
  82.     }
  83.     public function getRelativeThumbnailUrl(MediaEntity $mediaMediaThumbnailEntity $thumbnail): string
  84.     {
  85.         return $this->getAbsoluteThumbnailUrl($media$thumbnail);
  86.     }
  87.     private function normalizeBaseUrl(?string $baseUrl): ?string
  88.     {
  89.         if (!$baseUrl) {
  90.             return null;
  91.         }
  92.         return rtrim($baseUrl'/');
  93.     }
  94.     private function getBaseUrl(): string
  95.     {
  96.         if (!$this->baseUrl) {
  97.             $this->baseUrl $this->createFallbackUrl();
  98.         }
  99.         return $this->baseUrl;
  100.     }
  101.     private function createFallbackUrl(): string
  102.     {
  103.         $request $this->requestStack->getMasterRequest();
  104.         if ($request) {
  105.             $basePath $request->getSchemeAndHttpHost() . $request->getBasePath();
  106.             return rtrim($basePath'/');
  107.         }
  108.         return '';
  109.     }
  110. }