FlysystemResolver::remove()   B
last analyzed

Complexity

Conditions 8
Paths 7

Size

Total Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 23
rs 8.4444
cc 8
nc 7
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\Cache\Resolver;
13
14
use League\Flysystem\AdapterInterface;
15
use League\Flysystem\FilesystemInterface;
16
use Liip\ImagineBundle\Binary\BinaryInterface;
17
use Liip\ImagineBundle\Exception\Imagine\Cache\Resolver\NotResolvableException;
18
use Symfony\Component\Routing\RequestContext;
19
20
class FlysystemResolver implements ResolverInterface
21
{
22
    /**
23
     * @var FilesystemInterface
24
     */
25
    protected $flysystem;
26
27
    /**
28
     * @var RequestContext
29
     */
30
    protected $requestContext;
31
32
    /**
33
     * @var string
34
     */
35
    protected $webRoot;
36
37
    /**
38
     * @var string
39
     */
40
    protected $cachePrefix;
41
42
    /**
43
     * @var string
44
     */
45
    protected $cacheRoot;
46
47
    /**
48
     * Flysystem specific visibility.
49
     *
50
     * @see AdapterInterface
51
     *
52
     * @var string
53
     */
54
    protected $visibility;
55
56
    /**
57
     * FlysystemResolver constructor.
58
     *
59
     * @param string $rootUrl
60
     * @param string $cachePrefix
61
     * @param string $visibility
62
     */
63
    public function __construct(
64
        FilesystemInterface $flysystem,
65
        RequestContext $requestContext,
66
        $rootUrl,
67
        $cachePrefix = 'media/cache',
68
        $visibility = AdapterInterface::VISIBILITY_PUBLIC
69
    ) {
70
        $this->flysystem = $flysystem;
71
        $this->requestContext = $requestContext;
72
73
        $this->webRoot = rtrim($rootUrl, '/');
74
        $this->cachePrefix = ltrim(str_replace('//', '/', $cachePrefix), '/');
75
        $this->cacheRoot = $this->cachePrefix;
76
        $this->visibility = $visibility;
77
    }
78
79
    /**
80
     * Checks whether the given path is stored within this Resolver.
81
     *
82
     * @param string $path
83
     * @param string $filter
84
     *
85
     * @return bool
86
     */
87
    public function isStored($path, $filter)
88
    {
89
        return $this->flysystem->has($this->getFilePath($path, $filter));
90
    }
91
92
    /**
93
     * Resolves filtered path for rendering in the browser.
94
     *
95
     * @param string $path   The path where the original file is expected to be
96
     * @param string $filter The name of the imagine filter in effect
97
     *
98
     * @throws NotResolvableException
99
     *
100
     * @return string The absolute URL of the cached image
101
     */
102 View Code Duplication
    public function resolve($path, $filter)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
103
    {
104
        return sprintf(
105
            '%s/%s',
106
            rtrim($this->webRoot, '/'),
107
            ltrim($this->getFileUrl($path, $filter), '/')
108
        );
109
    }
110
111
    /**
112
     * Stores the content of the given binary.
113
     *
114
     * @param BinaryInterface $binary The image binary to store
115
     * @param string          $path   The path where the original file is expected to be
116
     * @param string          $filter The name of the imagine filter in effect
117
     */
118
    public function store(BinaryInterface $binary, $path, $filter)
119
    {
120
        $this->flysystem->put(
121
            $this->getFilePath($path, $filter),
122
            $binary->getContent(),
123
            ['visibility' => $this->visibility, 'mimetype' => $binary->getMimeType()]
124
        );
125
    }
126
127
    /**
128
     * @param string[] $paths   The paths where the original files are expected to be
129
     * @param string[] $filters The imagine filters in effect
130
     */
131
    public function remove(array $paths, array $filters)
132
    {
133
        if (empty($paths) && empty($filters)) {
134
            return;
135
        }
136
137
        if (empty($paths)) {
138
            foreach ($filters as $filter) {
139
                $filterCacheDir = $this->cacheRoot.'/'.$filter;
140
                $this->flysystem->deleteDir($filterCacheDir);
141
            }
142
143
            return;
144
        }
145
146
        foreach ($paths as $path) {
147
            foreach ($filters as $filter) {
148
                if ($this->flysystem->has($this->getFilePath($path, $filter))) {
149
                    $this->flysystem->delete($this->getFilePath($path, $filter));
150
                }
151
            }
152
        }
153
    }
154
155
    /**
156
     * {@inheritdoc}
157
     */
158
    protected function getFilePath($path, $filter)
159
    {
160
        return $this->getFileUrl($path, $filter);
161
    }
162
163
    /**
164
     * {@inheritdoc}
165
     */
166 View Code Duplication
    protected function getFileUrl($path, $filter)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
167
    {
168
        // crude way of sanitizing URL scheme ("protocol") part
169
        $path = str_replace('://', '---', $path);
170
171
        return $this->cachePrefix.'/'.$filter.'/'.ltrim($path, '/');
172
    }
173
}
174