Completed
Pull Request — master (#1300)
by Gocha
03:26 queued 01:32
created

FormatExtensionResolver::isStored()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
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\Binary\BinaryInterface;
15
use Liip\ImagineBundle\Imagine\Filter\FilterConfiguration;
16
17
class FormatExtensionResolver implements ResolverInterface
18
{
19
    /**
20
     * @var ResolverInterface
21
     */
22
    private $resolver;
23
24
    /**
25
     * @var FilterConfiguration
26
     */
27
    private $filterConfig;
28
29
    /**
30
     * @param ResolverInterface   $resolver
31
     * @param FilterConfiguration $filterConfig
32
     */
33
    public function __construct(ResolverInterface $resolver, FilterConfiguration $filterConfig)
34
    {
35
        $this->resolver = $resolver;
36
        $this->filterConfig = $filterConfig;
37
    }
38
39
    /**
40
     * {@inheritdoc}
41
     */
42
    public function resolve($path, $filter)
43
    {
44
        $path = $this->replaceExtension($path, $filter);
45
46
        return $this->resolver->resolve($path, $filter);
47
    }
48
49
    /**
50
     * {@inheritdoc}
51
     */
52
    public function store(BinaryInterface $binary, $targetPath, $filter)
53
    {
54
        $targetPath = $this->replaceExtension($targetPath, $filter);
55
56
        return $this->resolver->store($binary, $targetPath, $filter);
57
    }
58
59
    /**
60
     * {@inheritdoc}
61
     */
62
    public function isStored($path, $filter)
63
    {
64
        $path = $this->replaceExtension($path, $filter);
65
66
        return $this->resolver->isStored($path, $filter);
67
    }
68
69
    /**
70
     * {@inheritdoc}
71
     */
72
    public function remove(array $paths, array $filters)
73
    {
74
        $newPaths = [];
75
        foreach ($paths as $path) {
76
            foreach ($filters as $filter) {
77
                $newPath = $this->replaceExtension($path, $filter);
78
                if (!in_array($newPath, $newPaths)) {
79
                    $newPaths[] = $newPath;
80
                }
81
            }
82
        }
83
84
        return $this->resolver->remove($newPaths, $filters);
85
    }
86
87
    private function replaceExtension(string $path, string $filter): string
88
    {
89
        $config = $this->filterConfig->get($filter);
90
        if (!$config['format']) {
91
            return $path;
92
        }
93
94
        $extension = pathinfo($path, PATHINFO_EXTENSION);
95
        $path = ($extension ? mb_substr($path, 0, -mb_strlen($extension)) : $path . '.') . $config['format'];
96
97
        return $path;
98
    }
99
}
100