Completed
Push — 2.0 ( 057aa6...b272ae )
by Maksim
02:01
created

FilterService::getUrlOfFilteredImage()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 20
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

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