ImagineWebCacheResolver::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 5
c 1
b 0
f 0
nc 1
nop 4
dl 0
loc 11
ccs 0
cts 11
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
        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