WebPathResolver::getFileUrl()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
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 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 string $webRootDir
48
     * @param string $cachePrefix
49
     */
50
    public function __construct(
51
        Filesystem $filesystem,
52
        RequestContext $requestContext,
53
        $webRootDir,
54
        $cachePrefix = 'media/cache'
55
    ) {
56
        $this->filesystem = $filesystem;
57
        $this->requestContext = $requestContext;
58
59
        $this->webRoot = rtrim(str_replace('//', '/', $webRootDir), '/');
60
        $this->cachePrefix = ltrim(str_replace('//', '/', $cachePrefix), '/');
61
        $this->cacheRoot = $this->webRoot.'/'.$this->cachePrefix;
62
    }
63
64
    /**
65
     * {@inheritdoc}
66
     */
67 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...
68
    {
69
        return sprintf('%s/%s',
70
            rtrim($this->getBaseUrl(), '/'),
71
            ltrim($this->getFileUrl($path, $filter), '/')
72
        );
73
    }
74
75
    /**
76
     * {@inheritdoc}
77
     */
78
    public function isStored($path, $filter)
79
    {
80
        return is_file($this->getFilePath($path, $filter));
81
    }
82
83
    /**
84
     * {@inheritdoc}
85
     */
86
    public function store(BinaryInterface $binary, $path, $filter)
87
    {
88
        $this->filesystem->dumpFile(
89
            $this->getFilePath($path, $filter),
90
            $binary->getContent()
91
        );
92
    }
93
94
    /**
95
     * {@inheritdoc}
96
     */
97
    public function remove(array $paths, array $filters)
98
    {
99
        if (empty($paths) && empty($filters)) {
100
            return;
101
        }
102
103
        if (empty($paths)) {
104
            $filtersCacheDir = [];
105
            foreach ($filters as $filter) {
106
                $filtersCacheDir[] = $this->cacheRoot.'/'.$filter;
107
            }
108
109
            $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...
110
111
            return;
112
        }
113
114
        foreach ($paths as $path) {
115
            foreach ($filters as $filter) {
116
                $this->filesystem->remove($this->getFilePath($path, $filter));
117
            }
118
        }
119
    }
120
121
    /**
122
     * {@inheritdoc}
123
     */
124
    protected function getFilePath($path, $filter)
125
    {
126
        return $this->webRoot.'/'.$this->getFullPath($path, $filter);
127
    }
128
129
    /**
130
     * {@inheritdoc}
131
     */
132
    protected function getFileUrl($path, $filter)
133
    {
134
        return PathHelper::filePathToUrlPath($this->getFullPath($path, $filter));
135
    }
136
137
    /**
138
     * @return string
139
     */
140
    protected function getBaseUrl()
141
    {
142
        $port = '';
143 View Code Duplication
        if ('https' === $this->requestContext->getScheme() && 443 !== $this->requestContext->getHttpsPort()) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
144
            $port = ":{$this->requestContext->getHttpsPort()}";
145
        }
146
147 View Code Duplication
        if ('http' === $this->requestContext->getScheme() && 80 !== $this->requestContext->getHttpPort()) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
148
            $port = ":{$this->requestContext->getHttpPort()}";
149
        }
150
151
        $baseUrl = $this->requestContext->getBaseUrl();
152
        if ('.php' === mb_substr($this->requestContext->getBaseUrl(), -4)) {
153
            $baseUrl = pathinfo($this->requestContext->getBaseurl(), PATHINFO_DIRNAME);
154
        }
155
        $baseUrl = rtrim($baseUrl, '/\\');
156
157
        return sprintf('%s://%s%s%s',
158
            $this->requestContext->getScheme(),
159
            $this->requestContext->getHost(),
160
            $port,
161
            $baseUrl
162
        );
163
    }
164
165 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...
166
    {
167
        // crude way of sanitizing URL scheme ("protocol") part
168
        $path = str_replace('://', '---', $path);
169
170
        return $this->cachePrefix.'/'.$filter.'/'.ltrim($path, '/');
171
    }
172
}
173