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

FormatResolver::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.9
c 0
b 0
f 0
cc 1
nc 1
nop 5
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 Filesystem     $filesystem
27
     * @param RequestContext $requestContext
28
     * @param string         $webRootDir
29
     * @param string         $cachePrefix
30
     * @param FilterManager  $filterManager
31
     */
32
    public function __construct(
33
        Filesystem     $filesystem,
34
        RequestContext $requestContext,
35
        string         $webRootDir,
36
        string         $cachePrefix,
37
        FilterManager  $filterManager
38
    ) {
39
        parent::__construct($filesystem, $requestContext, $webRootDir, $cachePrefix);
40
41
        $this->filterManager = $filterManager;
42
    }
43
44
    /**
45
     * {@inheritDoc}
46
     */
47
    protected function getFilePath($path, $filter)
48
    {
49
        return $this->webRoot.'/'.$this->getFileUrl($this->replaceImageFileExtension($path, $filter), $filter);
50
    }
51
52
    /**
53
     * {@inheritDoc}
54
     */
55
    protected function getFileUrl($path, $filter)
56
    {
57
        return $this->cachePrefix.'/'.$filter.'/'.ltrim($this->replaceImageFileExtension($path, $filter), '/');
58
    }
59
60
    /**
61
     * Replaces original image file extension to conversion format extension
62
     *
63
     * @param string $path
64
     * @param string $filter
65
     *
66
     * @return mixed
67
     */
68
    protected function replaceImageFileExtension(string $path, string $filter): string
69
    {
70
        $newExtension = $this->getImageFormat($filter);
71
        if (null !== $newExtension) {
72
            $path = preg_replace('/\.[^.]+$/', '.'.$newExtension, $path);
73
        }
74
75
        return $path;
76
    }
77
78
    /**
79
     * Returns image conversion format
80
     *
81
     * @param $filterName
82
     *
83
     * @return mixed
84
     */
85
    protected function getImageFormat($filterName)
86
    {
87
        $filterConfig = $this->filterManager->getFilterConfiguration();
88
        $currentFilterConfig = $filterConfig->get($filterName);
89
90
        return $currentFilterConfig['format'];
91
    }
92
}
93