custom/plugins/IntediaImgbolt/src/Core/Content/Media/Pathname/CdnUrlGenerator.php line 53

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Intedia\Imgbolt\Core\Content\Media\Pathname;
  3. use Shopware\Core\Content\Media\Aggregate\MediaThumbnail\MediaThumbnailEntity;
  4. use Shopware\Core\Content\Media\MediaEntity;
  5. use Shopware\Core\Content\Media\Pathname\PathnameStrategy\PathnameStrategyInterface;
  6. use Shopware\Core\Content\Media\Pathname\UrlGeneratorInterface;
  7. use Shopware\Core\PlatformRequest;
  8. use Shopware\Core\System\SystemConfig\SystemConfigService;
  9. use Symfony\Component\HttpFoundation\RequestStack;
  10. use Symfony\Contracts\Service\ResetInterface;
  11. class CdnUrlGenerator implements UrlGeneratorInterfaceResetInterface
  12. {
  13.     const CONFIG_KEY 'IntediaImgbolt.config.';
  14.     /**
  15.      * @var UrlGeneratorInterface
  16.      */
  17.     private $originalService;
  18.     /**
  19.      * @var RequestStack
  20.      */
  21.     private $requestStack;
  22.     /**
  23.      * @var string
  24.      */
  25.     private $baseUrl;
  26.     /**
  27.      * @var array
  28.      */
  29.     protected $config;
  30.     /**
  31.      * @var PathnameStrategyInterface
  32.      */
  33.     private $pathnameStrategy;
  34.     /**
  35.      * @var SystemConfigService
  36.      */
  37.     protected $systemConfigService;
  38.     /**
  39.      * @var array
  40.      */
  41.     protected $cdnUrls;
  42.     public function __construct(
  43.         UrlGeneratorInterface $originalService,
  44.         PathnameStrategyInterface $pathnameStrategy,
  45.         RequestStack $requestStack,
  46.         string $baseUrl,
  47.         SystemConfigService $systemConfigService
  48.     ) {
  49.         $this->originalService     $originalService;
  50.         $this->pathnameStrategy    $pathnameStrategy;
  51.         $this->requestStack        $requestStack;
  52.         $this->systemConfigService $systemConfigService;
  53.         $this->config  $this->getConfig();
  54.         $this->cdnUrls $this->getCdnUrls();
  55.         $this->baseUrl $this->normalizeBaseUrl($baseUrl);
  56.     }
  57.     /**
  58.      * {@inheritDoc}
  59.      */
  60.     public function getRelativeMediaUrl(MediaEntity $media): string
  61.     {
  62.         return $this->originalService->getRelativeMediaUrl($media);
  63.     }
  64.     /**
  65.      * {@inheritDoc}
  66.      */
  67.     public function getAbsoluteMediaUrl(MediaEntity $media): string
  68.     {
  69.         return $this->getBaseUrl() . '/' $this->getRelativeMediaUrl($media);
  70.     }
  71.     /**
  72.      * {@inheritDoc}
  73.      */
  74.     public function getRelativeThumbnailUrl(MediaEntity $mediaMediaThumbnailEntity $thumbnail): string
  75.     {
  76.         return $this->originalService->getRelativeThumbnailUrl($media$thumbnail);
  77.     }
  78.     /**
  79.      * @param MediaEntity $media
  80.      * @param MediaThumbnailEntity $thumbnail
  81.      * @return string
  82.      */
  83.     public function getAbsoluteThumbnailUrl(MediaEntity $mediaMediaThumbnailEntity $thumbnail): string
  84.     {
  85.         return $this->getBaseUrl() . '/' $this->getRelativeThumbnailUrl($media$thumbnail);
  86.     }
  87.     /**
  88.      * @return string
  89.      */
  90.     private function getBaseUrl(): string
  91.     {
  92.         if (!$this->baseUrl) {
  93.             $this->baseUrl $this->createFallbackUrl();
  94.         }
  95.         return $this->baseUrl;
  96.     }
  97.     /**
  98.      * @return string
  99.      */
  100.     private function createFallbackUrl(): string
  101.     {
  102.         $request $this->requestStack->getMasterRequest();
  103.         if ($request) {
  104.             $schemeAndHost = empty($this->cdnUrls) ? $request->getSchemeAndHttpHost() : rtrim($this->cdnUrls[array_rand($this->cdnUrls)], '/');
  105.             $basePath      $schemeAndHost $request->getBasePath();
  106.             return rtrim($basePath'/');
  107.         }
  108.         return $_SERVER['APP_URL'];
  109.     }
  110.     /**
  111.      * @param string|null $baseUrl
  112.      * @return string|null
  113.      */
  114.     private function normalizeBaseUrl(?string $baseUrl): ?string
  115.     {
  116.         if ($baseUrl === null) { // The case most of the time
  117.             return null;
  118.         }
  119.         return rtrim($baseUrl'/');
  120.     }
  121.     protected function getCdnUrls(): array
  122.     {
  123.         if ($this->getConfigValue('imCdnEnabled')) {
  124.             if ($domainConfig trim($this->getConfigValue('imCdnDomain'))) {
  125.                 return array_map('trim'explode(','$domainConfig));
  126.             }
  127.         }
  128.         return [];
  129.     }
  130.     /**
  131.      * @param $key
  132.      * @param null $default
  133.      * @return mixed|null
  134.      */
  135.     protected function getConfigValue($key$default null)
  136.     {
  137.         if (array_key_exists($key$this->config)) {
  138.             return $this->config[$key];
  139.         }
  140.         else if (array_key_exists(self::CONFIG_KEY $key$this->config)) {
  141.             return $this->config[self::CONFIG_KEY $key];
  142.         }
  143.         return $default;
  144.     }
  145.     protected function getConfig(): array
  146.     {
  147.         $request $this->requestStack->getMasterRequest();
  148.         $salesChannelId $request $request->attributes->get(PlatformRequest::ATTRIBUTE_SALES_CHANNEL_ID) : null;
  149.         return $this->systemConfigService->getDomain(self::CONFIG_KEY$salesChannelIdtrue);
  150.     }
  151.     public function reset(): void
  152.     {
  153.         if (method_exists($this->originalService'reset')) {
  154.             $this->originalService->reset();
  155.         }
  156.         // TODO: Implement reset() method.
  157.     }
  158. }