Completed
Pull Request — master (#1217)
by
unknown
01:44
created

DataManager::find()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 26
rs 9.504
c 0
b 0
f 0
cc 4
nc 6
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\Data;
13
14
use Liip\ImagineBundle\Binary\BinaryInterface;
15
use Liip\ImagineBundle\Binary\Loader\LoaderInterface;
16
use Liip\ImagineBundle\Binary\MimeTypeGuesserInterface;
17
use Liip\ImagineBundle\Imagine\Filter\FilterConfiguration;
18
use Liip\ImagineBundle\Model\Binary;
19
use Symfony\Component\HttpFoundation\File\MimeType\ExtensionGuesserInterface as DeprecatedExtensionGuesserInterface;
20
use Symfony\Component\Mime\MimeTypesInterface;
21
22
class DataManager
23
{
24
    /**
25
     * @var MimeTypeGuesserInterface
26
     */
27
    protected $mimeTypeGuesser;
28
29
    /**
30
     * @var DeprecatedExtensionGuesserInterface|MimeTypesInterface
31
     */
32
    protected $extensionGuesser;
33
34
    /**
35
     * @var FilterConfiguration
36
     */
37
    protected $filterConfig;
38
39
    /**
40
     * @var string|null
41
     */
42
    protected $defaultLoader;
43
44
    /**
45
     * @var string|null
46
     */
47
    protected $globalDefaultImage;
48
49
    /**
50
     * @var LoaderInterface[]
51
     */
52
    protected $loaders = [];
53
54
    /**
55
     * @param MimeTypeGuesserInterface                               $mimeTypeGuesser
56
     * @param DeprecatedExtensionGuesserInterface|MimeTypesInterface $extensionGuesser
57
     * @param FilterConfiguration                                    $filterConfig
58
     * @param string                                                 $defaultLoader
59
     * @param string                                                 $globalDefaultImage
60
     */
61
    public function __construct(
62
        MimeTypeGuesserInterface $mimeTypeGuesser,
63
        $extensionGuesser,
64
        FilterConfiguration $filterConfig,
65
        $defaultLoader = null,
66
        $globalDefaultImage = null
67
    ) {
68
        if (interface_exists(MimeTypesInterface::class) && $extensionGuesser instanceof DeprecatedExtensionGuesserInterface) {
69
            @trigger_error(sprintf('Passing a %s to "%s()" is deprecated since Symfony 4.3, pass a "%s" instead.', DeprecatedExtensionGuesserInterface::class, __METHOD__, MimeTypesInterface::class), E_USER_DEPRECATED);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
70
        }
71
72
        $this->mimeTypeGuesser = $mimeTypeGuesser;
73
        $this->filterConfig = $filterConfig;
74
        $this->defaultLoader = $defaultLoader;
75
        $this->extensionGuesser = $extensionGuesser;
76
        $this->globalDefaultImage = $globalDefaultImage;
77
    }
78
79
    /**
80
     * Adds a loader to retrieve images for the given filter.
81
     *
82
     * @param string          $filter
83
     * @param LoaderInterface $loader
84
     */
85
    public function addLoader($filter, LoaderInterface $loader)
86
    {
87
        $this->loaders[$filter] = $loader;
88
    }
89
90
    /**
91
     * Returns a loader previously attached to the given filter.
92
     *
93
     * @param string $filter
94
     *
95
     * @throws \InvalidArgumentException
96
     *
97
     * @return LoaderInterface
98
     */
99
    public function getLoader($filter)
100
    {
101
        $config = $this->filterConfig->get($filter);
102
103
        $loaderName = empty($config['data_loader']) ? $this->defaultLoader : $config['data_loader'];
104
105
        if (!isset($this->loaders[$loaderName])) {
106
            throw new \InvalidArgumentException(sprintf(
107
                'Could not find data loader "%s" for "%s" filter type',
108
                $loaderName,
109
                $filter
110
            ));
111
        }
112
113
        return $this->loaders[$loaderName];
114
    }
115
116
    /**
117
     * Retrieves an image with the given filter applied.
118
     *
119
     * @param string $filter
120
     * @param string $path
121
     *
122
     * @throws \LogicException
123
     *
124
     * @return BinaryInterface
125
     */
126
    public function find($filter, $path)
127
    {
128
        $loader = $this->getLoader($filter);
129
130
        $binary = $loader->find($path);
131
        if (!$binary instanceof BinaryInterface) {
132
            $mimeType = $this->mimeTypeGuesser->guess($binary);
133
134
            $extension = $this->getExtension($mimeType);
135
            $binary = new Binary(
136
                $binary,
137
                $mimeType,
138
                $extension
139
            );
140
        }
141
142
        if (null === $binary->getMimeType()) {
143
            throw new \LogicException(sprintf('The mime type of image %s was not guessed.', $path));
144
        }
145
146
        if (0 !== mb_strpos($binary->getMimeType(), 'image/')) {
147
            throw new \LogicException(sprintf('The mime type of image %s must be image/xxx got %s.', $path, $binary->getMimeType()));
148
        }
149
150
        return $binary;
151
    }
152
153
    /**
154
     * Get default image url with the given filter applied.
155
     *
156
     * @param string $filter
157
     *
158
     * @return string|null
159
     */
160
    public function getDefaultImageUrl($filter)
161
    {
162
        $config = $this->filterConfig->get($filter);
163
164
        $defaultImage = null;
165
        if (false === empty($config['default_image'])) {
166
            $defaultImage = $config['default_image'];
167
        } elseif (!empty($this->globalDefaultImage)) {
168
            $defaultImage = $this->globalDefaultImage;
169
        }
170
171
        return $defaultImage;
172
    }
173
174
    private function getExtension(?string $mimeType): ?string
175
    {
176
        if ($this->extensionGuesser instanceof DeprecatedExtensionGuesserInterface) {
177
            return $this->extensionGuesser->guess($mimeType);
178
        }
179
180
        if (null === $mimeType) {
181
            return null;
182
        }
183
184
        return $this->extensionGuesser->getExtensions($mimeType)[0] ?? null;
185
    }
186
}
187