Passed
Push — master ( 95f6ae...aca519 )
by Dev
09:10
created

ImagineWebCacheResolver::getFullPath()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 6
ccs 0
cts 4
cp 0
crap 2
rs 10
1
<?php
2
3
namespace PiedWeb\CMSBundle\Service;
4
5
use Symfony\Component\Filesystem\Filesystem;
6
use Symfony\Component\Routing\RequestContext;
7
8
//implements ResolverInterface
9
class ImagineWebCacheResolver extends \Liip\ImagineBundle\Imagine\Cache\Resolver\WebPathResolver
10
{
11
    public function __construct(
12
        Filesystem $filesystem,
13
        RequestContext $requestContext,
14
        $webRootDir,
15
        $cachePrefix = 'media'
16
    ) {
17
        $this->filesystem = $filesystem;
18
        $this->requestContext = $requestContext;
19
        $this->webRoot = rtrim(str_replace('//', '/', $webRootDir), '/');
20
        $this->cachePrefix = ltrim(str_replace('//', '/', $cachePrefix), '/');
21
        $this->cacheRoot = $this->webRoot.'/'.$this->cachePrefix;
22
    }
23
24
    public function resolve($path, $filter)
25
    {
26
        $path = ltrim($path, '/media');
27
28
        return '/'.$this->getFileUrl($path, $filter);
29
    }
30
31
    protected function getFilePath($path, $filter)
32
    {
33
        $path = ltrim($path, '/media');
34
35
        return $this->webRoot.'/'.$this->getFullPath($path, $filter);
36
    }
37
38
    private function getFullPath($path, $filter)
39
    {
40
        // crude way of sanitizing URL scheme ("protocol") part
41
        $path = str_replace('://', '---', $path);
42
43
        return $this->cachePrefix.'/'.$filter.'/'.ltrim($path, '/');
44
    }
45
}
46