vendor/liip/imagine-bundle/Service/FilterService.php line 82

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the `liip/LiipImagineBundle` project.
  4.  *
  5.  * (c) https://github.com/liip/LiipImagineBundle/graphs/contributors
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE.md
  8.  * file that was distributed with this source code.
  9.  */
  10. namespace Liip\ImagineBundle\Service;
  11. use Liip\ImagineBundle\Binary\BinaryInterface;
  12. use Liip\ImagineBundle\Exception\Imagine\Filter\NonExistingFilterException;
  13. use Liip\ImagineBundle\Imagine\Cache\CacheManager;
  14. use Liip\ImagineBundle\Imagine\Data\DataManager;
  15. use Liip\ImagineBundle\Imagine\Filter\FilterManager;
  16. use Psr\Log\LoggerInterface;
  17. use Psr\Log\NullLogger;
  18. class FilterService
  19. {
  20.     /**
  21.      * @var DataManager
  22.      */
  23.     private $dataManager;
  24.     /**
  25.      * @var FilterManager
  26.      */
  27.     private $filterManager;
  28.     /**
  29.      * @var CacheManager
  30.      */
  31.     private $cacheManager;
  32.     /**
  33.      * @var LoggerInterface
  34.      */
  35.     private $logger;
  36.     /**
  37.      * @param DataManager     $dataManager
  38.      * @param FilterManager   $filterManager
  39.      * @param CacheManager    $cacheManager
  40.      * @param LoggerInterface $logger
  41.      */
  42.     public function __construct(
  43.         DataManager $dataManager,
  44.         FilterManager $filterManager,
  45.         CacheManager $cacheManager,
  46.         LoggerInterface $logger null
  47.     ) {
  48.         $this->dataManager $dataManager;
  49.         $this->filterManager $filterManager;
  50.         $this->cacheManager $cacheManager;
  51.         $this->logger $logger ?: new NullLogger();
  52.     }
  53.     /**
  54.      * @param string $path
  55.      * @param string $filter
  56.      */
  57.     public function bustCache($path$filter)
  58.     {
  59.         if (!$this->cacheManager->isStored($path$filter)) {
  60.             return;
  61.         }
  62.         $this->cacheManager->remove($path$filter);
  63.     }
  64.     /**
  65.      * @param string $path
  66.      * @param string $filter
  67.      * @param string $resolver
  68.      *
  69.      * @return string
  70.      */
  71.     public function getUrlOfFilteredImage($path$filter$resolver null)
  72.     {
  73.         if ($this->cacheManager->isStored($path$filter$resolver)) {
  74.             return $this->cacheManager->resolve($path$filter$resolver);
  75.         }
  76.         $filteredBinary $this->createFilteredBinary(
  77.             $path,
  78.             $filter
  79.         );
  80.         $this->cacheManager->store(
  81.             $filteredBinary,
  82.             $path,
  83.             $filter,
  84.             $resolver
  85.         );
  86.         return $this->cacheManager->resolve($path$filter$resolver);
  87.     }
  88.     /**
  89.      * @param string      $path
  90.      * @param string      $filter
  91.      * @param array       $runtimeFilters
  92.      * @param string|null $resolver
  93.      *
  94.      * @return string
  95.      */
  96.     public function getUrlOfFilteredImageWithRuntimeFilters($path$filter, array $runtimeFilters = [], $resolver null)
  97.     {
  98.         $runtimePath $this->cacheManager->getRuntimePath($path$runtimeFilters);
  99.         if ($this->cacheManager->isStored($runtimePath$filter$resolver)) {
  100.             return $this->cacheManager->resolve($runtimePath$filter$resolver);
  101.         }
  102.         $filteredBinary $this->createFilteredBinary(
  103.             $path,
  104.             $filter,
  105.             $runtimeFilters
  106.         );
  107.         $this->cacheManager->store(
  108.             $filteredBinary,
  109.             $runtimePath,
  110.             $filter,
  111.             $resolver
  112.         );
  113.         return $this->cacheManager->resolve($runtimePath$filter$resolver);
  114.     }
  115.     /**
  116.      * @param string $path
  117.      * @param string $filter
  118.      * @param array  $runtimeFilters
  119.      *
  120.      * @throws NonExistingFilterException
  121.      *
  122.      * @return BinaryInterface
  123.      */
  124.     private function createFilteredBinary($path$filter, array $runtimeFilters = [])
  125.     {
  126.         $binary $this->dataManager->find($filter$path);
  127.         try {
  128.             return $this->filterManager->applyFilter($binary$filter, [
  129.                 'filters' => $runtimeFilters,
  130.             ]);
  131.         } catch (NonExistingFilterException $e) {
  132.             $message sprintf('Could not locate filter "%s" for path "%s". Message was "%s"'$filter$path$e->getMessage());
  133.             $this->logger->debug($message);
  134.             throw $e;
  135.         }
  136.     }
  137. }