Completed
Push — feature-20rc1 ( 008ae2 )
by Rob
16:55
created

ImagineService::bustCache()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 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\Imagine;
13
14
use Liip\ImagineBundle\Exception\Imagine\Filter\NonExistingFilterException;
15
use Liip\ImagineBundle\File\FileInterface;
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\Log\Logger;
20
use Liip\ImagineBundle\Log\LoggerAwareTrait;
21
22
class ImagineService
23
{
24
    use LoggerAwareTrait;
25
26
    /**
27
     * @var DataManager
28
     */
29
    private $dataManager;
30
31
    /**
32
     * @var FilterManager
33
     */
34
    private $filterManager;
35
36
    /**
37
     * @var CacheManager
38
     */
39
    private $cacheManager;
40
41
    /**
42
     * @param DataManager   $dataManager
43
     * @param FilterManager $filterManager
44
     * @param CacheManager  $cacheManager
45
     */
46
    public function __construct(DataManager $dataManager, FilterManager $filterManager, CacheManager $cacheManager, Logger $logger = null)
47
    {
48
        $this->dataManager = $dataManager;
49
        $this->filterManager = $filterManager;
50
        $this->cacheManager = $cacheManager;
51
        $this->requireSetLogger($logger);
52
    }
53
54
    /**
55
     * @param string $path
56
     * @param string $filter
57
     */
58
    public function bustCache($path, $filter)
59
    {
60
        if (!$this->cacheManager->isStored($path, $filter)) {
61
            return;
62
        }
63
64
        $this->cacheManager->remove($path, $filter);
65
    }
66
67
    /**
68
     * @param string $path
69
     * @param string $filter
70
     * @param string $resolver
71
     *
72
     * @return string
73
     */
74
    public function getUrlOfFilteredImage($path, $filter, $resolver = null)
75
    {
76
        if ($this->cacheManager->isStored($path, $filter, $resolver)) {
77
            return $this->cacheManager->resolve($path, $filter, $resolver);
78
        }
79
80
        $filteredBinary = $this->createFilteredBinary(
81
            $path,
82
            $filter
83
        );
84
85
        $this->cacheManager->store(
86
            $filteredBinary,
87
            $path,
88
            $filter,
89
            $resolver
90
        );
91
92
        return $this->cacheManager->resolve($path, $filter, $resolver);
93
    }
94
95
    /**
96
     * @param string      $path
97
     * @param string      $filter
98
     * @param array       $runtimeFilters
99
     * @param string|null $resolver
100
     *
101
     * @return string
102
     */
103
    public function getUrlOfFilteredImageWithRuntimeFilters($path, $filter, array $runtimeFilters = [], $resolver = null)
104
    {
105
        $runtimePath = $this->cacheManager->getRuntimePath($path, $runtimeFilters);
106
        if ($this->cacheManager->isStored($runtimePath, $filter, $resolver)) {
107
            return $this->cacheManager->resolve($runtimePath, $filter, $resolver);
108
        }
109
110
        $filteredBinary = $this->createFilteredBinary(
111
            $path,
112
            $filter,
113
            $runtimeFilters
114
        );
115
116
        $this->cacheManager->store(
117
            $filteredBinary,
118
            $runtimePath,
119
            $filter,
120
            $resolver
121
        );
122
123
        return $this->cacheManager->resolve($runtimePath, $filter, $resolver);
124
    }
125
126
    /**
127
     * @param string $path
128
     * @param string $filter
129
     * @param array  $runtimeFilters
130
     *
131
     * @throws NonExistingFilterException
132
     *
133
     * @return FileInterface
134
     */
135
    private function createFilteredBinary($path, $filter, array $runtimeFilters = [])
136
    {
137
        $binary = $this->dataManager->find($filter, $path);
138
139
        try {
140
            return $this->filterManager->applyFilter($binary, $filter, [
141
                'filters' => $runtimeFilters,
142
            ]);
143
        } catch (NonExistingFilterException $e) {
144
            $message = sprintf('Could not locate filter "%s" for path "%s". Message was "%s"', $filter, $path, $e->getMessage());
145
146
            $this->logger->debug($message);
147
148
            throw $e;
149
        }
150
    }
151
}
152