Code Duplication    Length = 154-154 lines in 2 locations

Imagine/Cache/Resolver/FlysystemResolver.php 1 location

@@ 20-173 (lines=154) @@
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
    public function resolve($path, $filter)
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
    protected function getFileUrl($path, $filter)
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

Imagine/Cache/Resolver/FlysystemV2Resolver.php 1 location

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