Completed
Pull Request — master (#1128)
by
unknown
02:32
created

WebPathResolver::remove()   B

Complexity

Conditions 7
Paths 6

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.6186
cc 7
nc 6
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 Liip\ImagineBundle\Binary\BinaryInterface;
15
use Symfony\Component\Filesystem\Filesystem;
16
use Symfony\Component\Routing\RequestContext;
17
18
class WebPathResolver implements ResolverInterface
19
{
20
    /**
21
     * @var Filesystem
22
     */
23
    protected $filesystem;
24
25
    /**
26
     * @var RequestContext
27
     */
28
    protected $requestContext;
29
30
    /**
31
     * @var string
32
     */
33
    protected $webRoot;
34
35
    /**
36
     * @var string
37
     */
38
    protected $cachePrefix;
39
40
    /**
41
     * @var string
42
     */
43
    protected $cacheRoot;
44
45
    /**
46
     * @param Filesystem     $filesystem
47
     * @param RequestContext $requestContext
48
     * @param string         $webRootDir
49
     * @param string         $cachePrefix
50
     */
51
    public function __construct(
52
        Filesystem $filesystem,
53
        RequestContext $requestContext,
54
        $webRootDir,
55
        $cachePrefix = 'media/cache'
56
    ) {
57
        $this->filesystem = $filesystem;
58
        $this->requestContext = $requestContext;
59
60
        $this->webRoot = rtrim(str_replace('//', '/', $webRootDir), '/');
61
        $this->cachePrefix = ltrim(str_replace('//', '/', $cachePrefix), '/');
62
        $this->cacheRoot = $this->webRoot.'/'.$this->cachePrefix;
63
    }
64
65
    /**
66
     * {@inheritdoc}
67
     */
68
    public function resolve($path, $filter)
69
    {
70
        return sprintf('%s/%s',
71
            $this->getBaseUrl(),
72
            $this->getFileUrl($path, $filter)
73
        );
74
    }
75
76
    /**
77
     * {@inheritdoc}
78
     */
79
    public function isStored($path, $filter)
80
    {
81
        return is_file($this->getFilePath($path, $filter));
82
    }
83
84
    /**
85
     * {@inheritdoc}
86
     */
87
    public function store(BinaryInterface $binary, $path, $filter)
88
    {
89
        $this->filesystem->dumpFile(
90
            $this->getFilePath($path, $filter),
91
            $binary->getContent()
92
        );
93
    }
94
95
    /**
96
     * {@inheritdoc}
97
     */
98
    public function remove(array $paths, array $filters)
99
    {
100
        if (empty($paths) && empty($filters)) {
101
            return;
102
        }
103
104
        if (empty($paths)) {
105
            $filtersCacheDir = [];
106
            foreach ($filters as $filter) {
107
                $filtersCacheDir[] = $this->cacheRoot.'/'.$filter;
108
            }
109
110
            $this->filesystem->remove($filtersCacheDir);
0 ignored issues
show
Documentation introduced by
$filtersCacheDir is of type array, but the function expects a string|object<Symfony\Co...nt\Filesystem\iterable>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
111
112
            return;
113
        }
114
115
        foreach ($paths as $path) {
116
            foreach ($filters as $filter) {
117
                $this->filesystem->remove($this->getFilePath($path, $filter));
118
            }
119
        }
120
    }
121
122
    /**
123
     * {@inheritdoc}
124
     */
125
    protected function getFilePath($path, $filter)
126
    {
127
        return $this->webRoot.'/'.$this->getFileUrl($path, $filter);
128
    }
129
130
    /**
131
     * {@inheritdoc}
132
     */
133 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...
134
    {
135
        // crude way of sanitizing URL scheme ("protocol") part
136
        $path = str_replace('://', '---', $path);
137
138
        return $this->cachePrefix.'/'.$filter.'/'.ltrim($path, '/');
139
    }
140
141
    /**
142
     * @return string
143
     */
144
    protected function getBaseUrl()
145
    {
146
        $baseUrl = $this->requestContext->getBaseUrl();
147
        if ('.php' === mb_substr($this->requestContext->getBaseUrl(), -4)) {
148
            $baseUrl = pathinfo($this->requestContext->getBaseurl(), PATHINFO_DIRNAME);
149
        }
150
        $baseUrl = rtrim($baseUrl, '/\\');
151
152
        return sprintf('//%s%s',
153
            $this->requestContext->getHost(),
154
            $baseUrl
155
        );
156
    }
157
}
158