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

ImagineWebCacheResolver   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 12
c 2
b 0
f 0
dl 0
loc 35
ccs 0
cts 26
cp 0
rs 10
wmc 4

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 1
A resolve() 0 5 1
A getFilePath() 0 5 1
A getFullPath() 0 6 1
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