Completed
Pull Request — master (#1162)
by Dmitry
05:06
created

WebPathResolver::store()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 7
rs 10
cc 1
nc 1
nop 3
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 Liip\ImagineBundle\Imagine\Cache\Helper\PathHelper;
16
use Symfony\Component\Filesystem\Filesystem;
17
use Symfony\Component\Routing\RequestContext;
18
19
class WebPathResolver implements ResolverInterface
20
{
21
    /**
22
     * @var Filesystem
23
     */
24
    protected $filesystem;
25
26
    /**
27
     * @var RequestContext
28
     */
29
    protected $requestContext;
30
31
    /**
32
     * @var string
33
     */
34
    protected $webRoot;
35
36
    /**
37
     * @var string
38
     */
39
    protected $cachePrefix;
40
41
    /**
42
     * @var string
43
     */
44
    protected $cacheRoot;
45
46
    /**
47
     * @param Filesystem     $filesystem
48
     * @param RequestContext $requestContext
49
     * @param string         $webRootDir
50
     * @param string         $cachePrefix
51
     */
52
    public function __construct(
53
        Filesystem $filesystem,
54
        RequestContext $requestContext,
55
        $webRootDir,
56
        $cachePrefix = 'media/cache'
57
    ) {
58
        $this->filesystem = $filesystem;
59
        $this->requestContext = $requestContext;
60
61
        $this->webRoot = rtrim(str_replace('//', '/', $webRootDir), '/');
62
        $this->cachePrefix = ltrim(str_replace('//', '/', $cachePrefix), '/');
63
        $this->cacheRoot = $this->webRoot.'/'.$this->cachePrefix;
64
    }
65
66
    /**
67
     * {@inheritdoc}
68
     */
69
    public function resolve($path, $filter)
70
    {
71
        return sprintf('%s/%s',
72
            $this->getBaseUrl(),
73
            $this->getFileUrl($path, $filter)
74
        );
75
    }
76
77
    /**
78
     * {@inheritdoc}
79
     */
80
    public function isStored($path, $filter)
81
    {
82
        return is_file($this->getFilePath($path, $filter));
83
    }
84
85
    /**
86
     * {@inheritdoc}
87
     */
88
    public function store(BinaryInterface $binary, $path, $filter)
89
    {
90
        $this->filesystem->dumpFile(
91
            $this->getFilePath($path, $filter),
92
            $binary->getContent()
93
        );
94
    }
95
96
    /**
97
     * {@inheritdoc}
98
     */
99
    public function remove(array $paths, array $filters)
100
    {
101
        if (empty($paths) && empty($filters)) {
102
            return;
103
        }
104
105
        if (empty($paths)) {
106
            $filtersCacheDir = [];
107
            foreach ($filters as $filter) {
108
                $filtersCacheDir[] = $this->cacheRoot.'/'.$filter;
109
            }
110
111
            $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...
112
113
            return;
114
        }
115
116
        foreach ($paths as $path) {
117
            foreach ($filters as $filter) {
118
                $this->filesystem->remove($this->getFilePath($path, $filter));
119
            }
120
        }
121
    }
122
123
    /**
124
     * {@inheritdoc}
125
     */
126
    protected function getFilePath($path, $filter)
127
    {
128
        return $this->webRoot.'/'.$this->getFullPath($path, $filter);
129
    }
130
131
    /**
132
     * {@inheritdoc}
133
     */
134
    protected function getFileUrl($path, $filter)
135
    {
136
        return PathHelper::filePathToUrlPath($this->getFullPath($path, $filter));
137
    }
138
139 View Code Duplication
    private function getFullPath($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...
140
    {
141
        // crude way of sanitizing URL scheme ("protocol") part
142
        $path = str_replace('://', '---', $path);
143
144
        return $this->cachePrefix.'/'.$filter.'/'.ltrim($path, '/');
145
    }
146
147
    /**
148
     * @return string
149
     */
150
    protected function getBaseUrl()
151
    {
152
        $port = '';
153
        if ('https' === $this->requestContext->getScheme() && 443 !== $this->requestContext->getHttpsPort()) {
154
            $port = ":{$this->requestContext->getHttpsPort()}";
155
        }
156
157
        if ('http' === $this->requestContext->getScheme() && 80 !== $this->requestContext->getHttpPort()) {
158
            $port = ":{$this->requestContext->getHttpPort()}";
159
        }
160
161
        $baseUrl = $this->requestContext->getBaseUrl();
162
        if ('.php' === mb_substr($this->requestContext->getBaseUrl(), -4)) {
163
            $baseUrl = pathinfo($this->requestContext->getBaseurl(), PATHINFO_DIRNAME);
164
        }
165
        $baseUrl = rtrim($baseUrl, '/\\');
166
167
        return sprintf('%s://%s%s%s',
168
            $this->requestContext->getScheme(),
169
            $this->requestContext->getHost(),
170
            $port,
171
            $baseUrl
172
        );
173
    }
174
}
175