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

FilterService   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 178
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 0
Metric Value
wmc 16
lcom 1
cbo 7
dl 0
loc 178
rs 10
c 0
b 0
f 0

6 Methods

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