Completed
Pull Request — master (#1233)
by
unknown
01:32
created

WebPathResolver::getFileUrl()   A

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\Utility\Path\PathResolverInterface;
15
use Symfony\Component\Filesystem\Filesystem;
16
use Symfony\Component\Routing\RequestContext;
17
18
class WebPathResolver extends AbstractWebPathResolver
19
{
20
    private $requestContext;
21
22
    /**
23
     * @param Filesystem            $filesystem
24
     * @param PathResolverInterface $pathResolver
25
     * @param RequestContext        $requestContext
26
     */
27
    public function __construct(
28
        Filesystem $filesystem,
29
        PathResolverInterface $pathResolver,
30
        RequestContext $requestContext
31
    ) {
32
        parent::__construct($filesystem, $pathResolver);
33
        $this->requestContext = $requestContext;
34
    }
35
36
    /**
37
     * {@inheritdoc}
38
     */
39
    public function resolve($path, $filter)
40
    {
41
        return sprintf(
42
            '%s/%s',
43
            rtrim($this->getBaseUrl(), '/'),
44
            ltrim($this->getPathResolver()->getFileUrl($path, $filter), '/')
45
        );
46
    }
47
48
    /**
49
     * @return string
50
     */
51
    protected function getBaseUrl()
52
    {
53
        $port = '';
54 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...
55
            $port = ":{$this->requestContext->getHttpsPort()}";
56
        }
57
58 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...
59
            $port = ":{$this->requestContext->getHttpPort()}";
60
        }
61
62
        $baseUrl = $this->requestContext->getBaseUrl();
63
        if ('.php' === mb_substr($this->requestContext->getBaseUrl(), -4)) {
64
            $baseUrl = pathinfo($this->requestContext->getBaseurl(), PATHINFO_DIRNAME);
65
        }
66
        $baseUrl = rtrim($baseUrl, '/\\');
67
68
        return sprintf(
69
            '%s://%s%s%s',
70
            $this->requestContext->getScheme(),
71
            $this->requestContext->getHost(),
72
            $port,
73
            $baseUrl
74
        );
75
    }
76
}
77