Completed
Pull Request — 2.x (#1258)
by
unknown
01:36
created

FormatResolver   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 3
dl 0
loc 68
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 1
A getFilePath() 0 4 1
A getFileUrl() 0 4 1
A replaceImageFileExtension() 0 9 2
A getImageFormat() 0 7 1
1
<?php
2
3
/*
4
 * This file is part of the `liip/LiipImagineBundle` project.
5
 *
6
 * (c) https://github.com/liip/LiipImagineBundle/graphs/contributors
7
 *
8
 * For the full copyright and license information, please view the LICENSE.md
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Liip\ImagineBundle\Imagine\Cache\Resolver;
13
14
use Liip\ImagineBundle\Imagine\Filter\FilterManager;
15
use Symfony\Component\Filesystem\Filesystem;
16
use Symfony\Component\Routing\RequestContext;
17
18
class FormatResolver extends WebPathResolver
19
{
20
    /**
21
     * @var FilterManager
22
     */
23
    protected $filterManager;
24
25
    /**
26
     * @param string $webRootDir
27
     * @param string $cachePrefix
28
     */
29
    public function __construct(
30
        Filesystem $filesystem,
31
        RequestContext $requestContext,
32
        $webRootDir,
33
        $cachePrefix,
34
        FilterManager $filterManager
35
    ) {
36
        parent::__construct($filesystem, $requestContext, $webRootDir, $cachePrefix);
37
38
        $this->filterManager = $filterManager;
39
    }
40
41
    /**
42
     * {@inheritDoc}
43
     */
44
    protected function getFilePath($path, $filter)
45
    {
46
        return $this->webRoot.'/'.$this->getFileUrl($this->replaceImageFileExtension($path, $filter), $filter);
47
    }
48
49
    /**
50
     * {@inheritDoc}
51
     */
52
    protected function getFileUrl($path, $filter)
53
    {
54
        return $this->cachePrefix.'/'.$filter.'/'.ltrim($this->replaceImageFileExtension($path, $filter), '/');
55
    }
56
57
    /**
58
     * Replaces original image file extension to conversion format extension
59
     *
60
     * @param string $path
61
     * @param string $filter
62
     */
63
    protected function replaceImageFileExtension($path, $filter)
64
    {
65
        $newExtension = $this->getImageFormat($filter);
66
        if (null !== $newExtension) {
67
            $path = preg_replace('/\.[^.]+$/', '.'.$newExtension, $path);
68
        }
69
70
        return $path;
71
    }
72
73
    /**
74
     * Returns image conversion format
75
     *
76
     * @param $filterName
77
     */
78
    protected function getImageFormat($filterName)
79
    {
80
        $filterConfig = $this->filterManager->getFilterConfiguration();
81
        $currentFilterConfig = $filterConfig->get($filterName);
82
83
        return $currentFilterConfig['format'];
84
    }
85
}
86