Completed
Push — 5.3 ( df7aca...b92c9c )
by Jeroen
06:07 queued 13s
created

WebPathResolver::getFilePath()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 0
cts 5
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
crap 2
1
<?php
2
3
namespace Kunstmaan\MediaBundle\Helper\Imagine;
4
5
use Liip\ImagineBundle\Imagine\Filter\FilterConfiguration;
6
use Symfony\Component\Filesystem\Filesystem;
7
use Symfony\Component\Routing\RequestContext;
8
9
class WebPathResolver extends \Liip\ImagineBundle\Imagine\Cache\Resolver\WebPathResolver
10
{
11
    /**
12
     * @var FilterConfiguration
13
     */
14
    protected $filterConfig;
15
16
    /**
17
     * @param Filesystem          $filesystem
18
     * @param RequestContext      $requestContext
19
     * @param string              $webRootDir
20
     * @param string              $cachePrefix
21
     * @param FilterConfiguration $filterConfig
22
     */
23
    public function __construct(Filesystem $filesystem, RequestContext $requestContext, $webRootDir, $cachePrefix = 'media/cache', FilterConfiguration $filterConfig)
24
    {
25
        parent::__construct($filesystem, $requestContext, $webRootDir, $cachePrefix);
26
27
        $this->filterConfig = $filterConfig;
28
    }
29
30
    /**
31
     * {@inheritdoc}
32
     */
33
    protected function getFileUrl($path, $filter)
34
    {
35
        $filterConf = $this->filterConfig->get($filter);
36
        $path = $this->changeFileExtension($path, $filterConf['format']);
37
38
        return parent::getFileUrl($path, $filter);
39
    }
40
41
    protected function getFilePath($path, $filter)
42
    {
43
        $filterConf = $this->filterConfig->get($filter);
44
        $path = $this->changeFileExtension($path, $filterConf['format']);
45
        $fullPath = $this->getFullPath($path, $filter);
46
47
        return $this->webRoot.'/'.$fullPath;
48
    }
49
50
    /**
51
     * {@inheritdoc}
52
     */
53
    public function resolve($path, $filter)
54
    {
55
        return sprintf('%s/%s', $this->getBaseUrl(), $this->getFileUrl($path, $filter));
56
    }
57
58
    /**
59
     * @param string $path
60
     * @param string $format
61
     *
62
     * @return string
63
     */
64 View Code Duplication
    private function changeFileExtension($path, $format)
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...
65
    {
66
        if (!$format) {
67
            return $path;
68
        }
69
70
        $info = pathinfo($path);
71
        $path = $info['dirname'] . DIRECTORY_SEPARATOR . $info['filename'] . '.' . $format;
72
73
        return $path;
74
    }
75
76
    /**
77
     * Copy from \Liip\ImagineBundle\Imagine\Cache\Resolver\WebPathResolver::getFullPath
78
     */
79
    private function getFullPath($path, $filter)
0 ignored issues
show
Bug introduced by
Consider using a different method name as you override a private method of the parent class.

Overwriting private methods is generally fine as long as you also use private visibility. It might still be preferable for understandability to use a different method name.

Loading history...
80
    {
81
        // crude way of sanitizing URL scheme ("protocol") part
82
        $path = str_replace('://', '---', $path);
83
84
        return $this->cachePrefix.'/'.$filter.'/'.ltrim($path, '/');
85
    }
86
}
87