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

WebPathResolver::store()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
c 0
b 0
f 0
rs 9.4285
cc 1
eloc 4
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\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('%s/%s',
42
            $this->getBaseUrl(),
43
            $this->getPathResolver()->getFileUrl($path, $filter)
44
        );
45
    }
46
    
47
    /**
48
     * @return string
49
     */
50
    protected function getBaseUrl()
51
    {
52
        $port = '';
53
        if ('https' == $this->requestContext->getScheme() && $this->requestContext->getHttpsPort() != 443) {
54
            $port = ":{$this->requestContext->getHttpsPort()}";
55
        }
56
57
        if ('http' == $this->requestContext->getScheme() && $this->requestContext->getHttpPort() != 80) {
58
            $port = ":{$this->requestContext->getHttpPort()}";
59
        }
60
61
        $baseUrl = $this->requestContext->getBaseUrl();
62
        if ('.php' == substr($this->requestContext->getBaseUrl(), -4)) {
63
            $baseUrl = pathinfo($this->requestContext->getBaseurl(), PATHINFO_DIRNAME);
64
        }
65
        $baseUrl = rtrim($baseUrl, '/\\');
66
67
        return sprintf('%s://%s%s%s',
68
            $this->requestContext->getScheme(),
69
            $this->requestContext->getHost(),
70
            $port,
71
            $baseUrl
72
        );
73
    }
74
}
75