ImagineWebCacheResolver   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 5
Bugs 1 Features 0
Metric Value
eloc 18
c 5
b 1
f 0
dl 0
loc 45
ccs 0
cts 36
cp 0
rs 10
wmc 8

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 1
A resolve() 0 10 3
A getFilePath() 0 10 3
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
        if (0 === strpos($path, 'media')) {
27
            $path = substr($path, 5);
28
        }
29
        if (0 === strpos($path, '/media')) {
30
            $path = substr($path, 6);
31
        }
32
33
        return '/'.$this->getFileUrl($path, $filter);
34
    }
35
36
    protected function getFilePath($path, $filter)
37
    {
38
        if (0 === strpos($path, 'media')) {
39
            $path = substr($path, 5);
40
        }
41
        if (0 === strpos($path, '/media')) {
42
            $path = substr($path, 6);
43
        }
44
45
        return $this->webRoot.'/'.$this->getFullPath($path, $filter);
46
    }
47
48
    private function getFullPath($path, $filter)
49
    {
50
        // crude way of sanitizing URL scheme ("protocol") part
51
        $path = str_replace('://', '---', $path);
52
53
        return $this->cachePrefix.'/'.$filter.'/'.ltrim($path, '/');
54
    }
55
}
56