Completed
Pull Request — master (#1307)
by Peter
01:44
created

FilterService::createFilteredBinary()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 16
rs 9.7333
cc 2
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\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 int
51
     */
52
    private $webpQuality;
53
54
    /**
55
     * @param DataManager     $dataManager
56
     * @param FilterManager   $filterManager
57
     * @param CacheManager    $cacheManager
58
     * @param bool            $webpGenerate
59
     * @param int             $webpQuality
60
     * @param LoggerInterface $logger
61
     */
62
    public function __construct(
63
        DataManager $dataManager,
64
        FilterManager $filterManager,
65
        CacheManager $cacheManager,
66
        $webpGenerate,
67
        $webpQuality,
68
        LoggerInterface $logger = null
69
    ) {
70
        $this->dataManager = $dataManager;
71
        $this->filterManager = $filterManager;
72
        $this->cacheManager = $cacheManager;
73
        $this->webpGenerate = $webpGenerate;
74
        $this->webpQuality = $webpQuality;
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
        if (!$this->cacheManager->isStored($path, $filter)) {
85
            return;
86
        }
87
88
        $this->cacheManager->remove($path, $filter);
89
    }
90
91
    /**
92
     * @param string $path
93
     * @param string $filter
94
     * @param string $resolver
95
     * @param bool   $webp
96
     *
97
     * @return string
98
     */
99
    public function getUrlOfFilteredImage($path, $filter, $resolver = null, $webp = false)
100
    {
101
        if ($this->cacheManager->isStored($path, $filter, $resolver)) {
102
            return $this->cacheManager->resolve($path, $filter, $resolver);
103
        }
104
105
        $filteredBinary = $this->createFilteredBinary(
106
            $path,
107
            $filter
108
        );
109
110
        $this->cacheManager->store(
111
            $filteredBinary,
112
            $path,
113
            $filter,
114
            $resolver
115
        );
116
117 View Code Duplication
        if ($this->webpGenerate) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
118
            // add webp in new location
119
            $filteredWebpBinary = $this->createFilteredWebpBinary($path, $filter);
120
            $this->cacheManager->store($filteredWebpBinary, $path.'.webp', $filter, $resolver);
121
122
            if ($webp) {
123
                return $this->cacheManager->resolve($path.'.webp', $filter, $resolver);
124
            }
125
        }
126
127
        return $this->cacheManager->resolve($path, $filter, $resolver);
128
    }
129
130
    /**
131
     * @param string      $path
132
     * @param string      $filter
133
     * @param array       $runtimeFilters
134
     * @param string|null $resolver
135
     * @param bool        $webp
136
     *
137
     * @return string
138
     */
139
    public function getUrlOfFilteredImageWithRuntimeFilters(
140
        $path,
141
        $filter,
142
        array $runtimeFilters = [],
143
        $resolver = null,
144
        $webp = false
145
    ) {
146
        $runtimePath = $this->cacheManager->getRuntimePath($path, $runtimeFilters);
147
        if ($this->cacheManager->isStored($runtimePath, $filter, $resolver)) {
148
            return $this->cacheManager->resolve($runtimePath, $filter, $resolver);
149
        }
150
151
        $filteredBinary = $this->createFilteredBinary(
152
            $path,
153
            $filter,
154
            $runtimeFilters
155
        );
156
157
        $this->cacheManager->store(
158
            $filteredBinary,
159
            $runtimePath,
160
            $filter,
161
            $resolver
162
        );
163
164 View Code Duplication
        if ($this->webpGenerate) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
165
            // add webp in new location
166
            $filteredWebpBinary = $this->createFilteredWebpBinary($path, $filter);
167
            $this->cacheManager->store($filteredWebpBinary, $runtimePath.'.webp', $filter, $resolver);
168
169
            if ($webp) {
170
                return $this->cacheManager->resolve($runtimePath.'.webp', $filter, $resolver);
171
            }
172
        }
173
174
        return $this->cacheManager->resolve($runtimePath, $filter, $resolver);
175
    }
176
177
    /**
178
     * @param string $path
179
     * @param string $filter
180
     * @param array  $runtimeFilters
181
     *
182
     * @throws NonExistingFilterException
183
     *
184
     * @return BinaryInterface
185
     */
186
    private function createFilteredBinary($path, $filter, array $runtimeFilters = [])
187
    {
188
        $binary = $this->dataManager->find($filter, $path);
189
190
        try {
191
            return $this->filterManager->applyFilter($binary, $filter, [
192
                'filters' => $runtimeFilters,
193
            ]);
194
        } catch (NonExistingFilterException $e) {
195
            $message = sprintf('Could not locate filter "%s" for path "%s". Message was "%s"', $filter, $path, $e->getMessage());
196
197
            $this->logger->debug($message);
198
199
            throw $e;
200
        }
201
    }
202
203
    /**
204
     * @param string $path
205
     * @param string $filter
206
     * @param array  $runtimeFilters
207
     *
208
     * @throws NonExistingFilterException
209
     *
210
     * @return BinaryInterface
211
     */
212
    private function createFilteredWebpBinary($path, $filter, array $runtimeFilters = [])
213
    {
214
        $binary = $this->dataManager->find($filter, $path);
215
216
        try {
217
            return $this->filterManager->applyFilter($binary, $filter, [
218
                'quality' => $this->webpQuality,
219
                'format' => 'webp',
220
                'filters' => $runtimeFilters,
221
            ]);
222
        } catch (NonExistingFilterException $e) {
223
            $message = sprintf('Could not locate filter "%s" for path "%s". Message was "%s"', $filter, $path, $e->getMessage());
224
225
            $this->logger->debug($message);
226
227
            throw $e;
228
        }
229
    }
230
}
231