Completed
Pull Request — master (#1307)
by Peter
02:04
created

FilterService::getUrlOfFilteredImage()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 4
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\Imagine\Filter\NonExistingFilterException;
16
use Liip\ImagineBundle\Imagine\Cache\CacheManager;
17
use Liip\ImagineBundle\Imagine\Data\DataManager;
18
use Liip\ImagineBundle\Imagine\Filter\FilterManager;
19
use Psr\Log\LoggerInterface;
20
use Psr\Log\NullLogger;
21
22
class FilterService
23
{
24
    /**
25
     * @var DataManager
26
     */
27
    private $dataManager;
28
29
    /**
30
     * @var FilterManager
31
     */
32
    private $filterManager;
33
34
    /**
35
     * @var CacheManager
36
     */
37
    private $cacheManager;
38
39
    /**
40
     * @var LoggerInterface
41
     */
42
    private $logger;
43
44
    /**
45
     * @var bool
46
     */
47
    private $webpGenerate;
48
49
    /**
50
     * @var array
51
     */
52
    private $webpOptions;
53
54
    /**
55
     * @param DataManager          $dataManager
56
     * @param FilterManager        $filterManager
57
     * @param CacheManager         $cacheManager
58
     * @param bool                 $webpGenerate
59
     * @param array                $webpOptions
60
     * @param LoggerInterface|null $logger
61
     */
62
    public function __construct(
63
        DataManager $dataManager,
64
        FilterManager $filterManager,
65
        CacheManager $cacheManager,
66
        bool $webpGenerate,
67
        array $webpOptions,
68
        ?LoggerInterface $logger = null
69
    ) {
70
        $this->dataManager = $dataManager;
71
        $this->filterManager = $filterManager;
72
        $this->cacheManager = $cacheManager;
73
        $this->webpGenerate = $webpGenerate;
74
        $this->webpOptions = $webpOptions;
75
        $this->logger = $logger ?: new NullLogger();
76
    }
77
78
    /**
79
     * @param string $path
80
     * @param string $filter
81
     */
82
    public function bustCache($path, $filter)
83
    {
84
        $basePathContainer = new FilterPathContainer($path);
85
        $filterPathContainers = [$basePathContainer];
86
87
        if ($this->webpGenerate) {
88
            $filterPathContainers[] = $basePathContainer->createWebp($this->webpOptions);
89
        }
90
91
        foreach ($filterPathContainers as $filterPathContainer) {
92
            if ($this->cacheManager->isStored($filterPathContainer->getTarget(), $filter)) {
93
                $this->cacheManager->remove($filterPathContainer->getTarget(), $filter);
94
            }
95
        }
96
    }
97
98
    /**
99
     * @param string      $path
100
     * @param string      $filter
101
     * @param string|null $resolver
102
     * @param bool        $webpSupported
103
     *
104
     * @return string
105
     */
106
    public function getUrlOfFilteredImage($path, $filter, $resolver = null, bool $webpSupported = false)
107
    {
108
        $basePathContainer = new FilterPathContainer($path);
109
110
        return $this->getUrlOfFilteredImageByContainer($basePathContainer, $filter, $resolver, $webpSupported);
111
    }
112
113
    /**
114
     * @param string      $path
115
     * @param string      $filter
116
     * @param array       $runtimeFilters
117
     * @param string|null $resolver
118
     * @param bool        $webpSupported
119
     *
120
     * @return string
121
     */
122
    public function getUrlOfFilteredImageWithRuntimeFilters(
123
        $path,
124
        $filter,
125
        array $runtimeFilters = [],
126
        $resolver = null,
127
        bool $webpSupported = false
128
    ) {
129
        $runtimePath = $this->cacheManager->getRuntimePath($path, $runtimeFilters);
130
        $basePathContainer = new FilterPathContainer($path, $runtimePath, [
131
            'filters' => $runtimeFilters,
132
        ]);
133
134
        return $this->getUrlOfFilteredImageByContainer($basePathContainer, $filter, $resolver, $webpSupported);
135
    }
136
137
    /**
138
     * @param FilterPathContainer $basePathContainer
139
     * @param string              $filter
140
     * @param string|null         $resolver
141
     * @param bool                $webpSupported
142
     *
143
     * @return string
144
     */
145
    private function getUrlOfFilteredImageByContainer(
146
        FilterPathContainer $basePathContainer,
147
        string $filter,
148
        ?string $resolver = null,
149
        bool $webpSupported = false
150
    ): string {
151
        $filterPathContainers = [$basePathContainer];
152
153
        if ($this->webpGenerate) {
154
            $webpPathContainer = $basePathContainer->createWebp($this->webpOptions);
155
            $filterPathContainers[] = $webpPathContainer;
156
        }
157
158
        foreach ($filterPathContainers as $filterPathContainer) {
159
            if (!$this->cacheManager->isStored($filterPathContainer->getTarget(), $filter, $resolver)) {
160
                $this->cacheManager->store(
161
                    $this->createFilteredBinary($filterPathContainer, $filter),
162
                    $filterPathContainer->getTarget(),
163
                    $filter,
164
                    $resolver
165
                );
166
            }
167
        }
168
169
        if ($webpSupported && isset($webpPathContainer)) {
170
            return $this->cacheManager->resolve($webpPathContainer->getTarget(), $filter, $resolver);
171
        }
172
173
        return $this->cacheManager->resolve($basePathContainer->getTarget(), $filter, $resolver);
174
    }
175
176
    /**
177
     * @param FilterPathContainer $filterPathContainer
178
     * @param string              $filter
179
     *
180
     * @throws NonExistingFilterException
181
     *
182
     * @return BinaryInterface
183
     */
184
    private function createFilteredBinary(FilterPathContainer $filterPathContainer, string $filter): BinaryInterface
185
    {
186
        $binary = $this->dataManager->find($filter, $filterPathContainer->getSource());
187
188
        try {
189
            return $this->filterManager->applyFilter($binary, $filter, $filterPathContainer->getOptions());
190
        } catch (NonExistingFilterException $e) {
191
            $this->logger->debug(sprintf(
192
                'Could not locate filter "%s" for path "%s". Message was "%s"',
193
                $filter,
194
                $filterPathContainer->getSource(),
195
                $e->getMessage()
196
            ));
197
198
            throw $e;
199
        }
200
    }
201
}
202