Completed
Pull Request — 2.x (#1258)
by
unknown
01:48
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
    public function __construct(
26
        Filesystem $filesystem,
27
        RequestContext $requestContext,
28
        string $webRootDir,
29
        string $cachePrefix,
30
        FilterManager $filterManager
31
    ) {
32
        parent::__construct($filesystem, $requestContext, $webRootDir, $cachePrefix);
33
34
        $this->filterManager = $filterManager;
35
    }
36
37
    /**
38
     * {@inheritDoc}
39
     */
40
    protected function getFilePath($path, $filter)
41
    {
42
        return $this->webRoot.'/'.$this->getFileUrl($this->replaceImageFileExtension($path, $filter), $filter);
43
    }
44
45
    /**
46
     * {@inheritDoc}
47
     */
48
    protected function getFileUrl($path, $filter)
49
    {
50
        return $this->cachePrefix.'/'.$filter.'/'.\ltrim($this->replaceImageFileExtension($path, $filter), '/');
51
    }
52
53
    /**
54
     * Replaces original image file extension to conversion format extension
55
     *
56
     * @return mixed
57
     */
58
    protected function replaceImageFileExtension(string $path, string $filter): string
59
    {
60
        $newExtension = $this->getImageFormat($filter);
61
        if (null !== $newExtension) {
62
            $path = \preg_replace('/\.[^.]+$/', '.'.$newExtension, $path);
63
        }
64
65
        return $path;
66
    }
67
68
    /**
69
     * Returns image conversion format
70
     *
71
     * @param $filterName
72
     */
73
    protected function getImageFormat($filterName)
74
    {
75
        $filterConfig        = $this->filterManager->getFilterConfiguration();
76
        $currentFilterConfig = $filterConfig->get($filterName);
77
78
        return $currentFilterConfig['format'];
79
    }
80
}
81