Completed
Pull Request — 2.0 (#1059)
by Nick
02:41
created

WebPathResolver::getFileUrl()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 3

Duplication

Lines 7
Ratio 100 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 2
dl 7
loc 7
rs 9.4285
c 0
b 0
f 0
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
            $this->getBaseUrl(),
44
            $this->getPathResolver()->getFileUrl($path, $filter)
45
        );
46
    }
47
48
    /**
49
     * @return string
50
     */
51
    protected function getBaseUrl()
52
    {
53
        $port = '';
54
        if ('https' === $this->requestContext->getScheme() && 443 !== $this->requestContext->getHttpsPort()) {
55
            $port = ":{$this->requestContext->getHttpsPort()}";
56
        }
57
58
        if ('http' === $this->requestContext->getScheme() && 80 !== $this->requestContext->getHttpPort()) {
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