• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

liip / LiipImagineBundle / 10701363663

04 Sep 2024 12:11PM UTC coverage: 81.688% (-0.06%) from 81.746%
10701363663

push

github

web-flow
Merge pull request #1610 from liip/cs-fix

apply latest cs fixer

67 of 84 new or added lines in 42 files covered. (79.76%)

2 existing lines in 1 file now uncovered.

2275 of 2785 relevant lines covered (81.69%)

103.1 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

95.45
/Imagine/Data/DataManager.php
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\Exception\InvalidArgumentException;
18
use Liip\ImagineBundle\Exception\LogicException;
19
use Liip\ImagineBundle\Imagine\Filter\FilterConfiguration;
20
use Liip\ImagineBundle\Model\Binary;
21
use Symfony\Component\HttpFoundation\File\MimeType\ExtensionGuesserInterface as DeprecatedExtensionGuesserInterface;
22
use Symfony\Component\Mime\MimeTypesInterface;
23

24
class DataManager
25
{
26
    /**
27
     * @var MimeTypeGuesserInterface
28
     */
29
    protected $mimeTypeGuesser;
30

31
    /**
32
     * @var DeprecatedExtensionGuesserInterface|MimeTypesInterface
33
     */
34
    protected $extensionGuesser;
35

36
    /**
37
     * @var FilterConfiguration
38
     */
39
    protected $filterConfig;
40

41
    /**
42
     * @var string|null
43
     */
44
    protected $defaultLoader;
45

46
    /**
47
     * @var string|null
48
     */
49
    protected $globalDefaultImage;
50

51
    /**
52
     * @var LoaderInterface[]
53
     */
54
    protected $loaders = [];
55

56
    /**
57
     * @param DeprecatedExtensionGuesserInterface|MimeTypesInterface $extensionGuesser
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 (!$extensionGuesser instanceof MimeTypesInterface && !$extensionGuesser instanceof DeprecatedExtensionGuesserInterface) {
169✔
69
            throw new InvalidArgumentException('$extensionGuesser must be an instance of Symfony\Component\Mime\MimeTypesInterface or Symfony\Component\HttpFoundation\File\MimeType\ExtensionGuesserInterface');
13✔
70
        }
71

72
        if (interface_exists(MimeTypesInterface::class) && $extensionGuesser instanceof DeprecatedExtensionGuesserInterface) {
156✔
NEW
73
            @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);
×
74
        }
75

76
        $this->mimeTypeGuesser = $mimeTypeGuesser;
156✔
77
        $this->filterConfig = $filterConfig;
156✔
78
        $this->defaultLoader = $defaultLoader;
156✔
79
        $this->extensionGuesser = $extensionGuesser;
156✔
80
        $this->globalDefaultImage = $globalDefaultImage;
156✔
81
    }
84✔
82

83
    /**
84
     * Adds a loader to retrieve images for the given filter.
85
     *
86
     * @param string $filter
87
     */
88
    public function addLoader($filter, LoaderInterface $loader)
89
    {
90
        $this->loaders[$filter] = $loader;
143✔
91
    }
77✔
92

93
    /**
94
     * Returns a loader previously attached to the given filter.
95
     *
96
     * @param string $filter
97
     *
98
     * @throws \InvalidArgumentException
99
     *
100
     * @return LoaderInterface
101
     */
102
    public function getLoader($filter)
103
    {
104
        $config = $this->filterConfig->get($filter);
117✔
105

106
        $loaderName = empty($config['data_loader']) ? $this->defaultLoader : $config['data_loader'];
117✔
107

108
        if (!isset($this->loaders[$loaderName])) {
117✔
109
            throw new \InvalidArgumentException(\sprintf('Could not find data loader "%s" for "%s" filter type', $loaderName, $filter));
13✔
110
        }
111

112
        return $this->loaders[$loaderName];
104✔
113
    }
114

115
    /**
116
     * Retrieves an image with the given filter applied.
117
     *
118
     * @param string $filter
119
     * @param string $path
120
     *
121
     * @throws LogicException
122
     *
123
     * @return BinaryInterface
124
     */
125
    public function find($filter, $path)
126
    {
127
        $loader = $this->getLoader($filter);
117✔
128

129
        $binary = $loader->find($path);
104✔
130
        if (!$binary instanceof BinaryInterface) {
104✔
131
            $mimeType = $this->mimeTypeGuesser->guess($binary);
78✔
132

133
            $extension = $this->getExtension($mimeType);
78✔
134
            $binary = new Binary(
78✔
135
                $binary,
78✔
136
                $mimeType,
66✔
137
                $extension
66✔
138
            );
36✔
139
        }
140

141
        if (null === $binary->getMimeType()) {
104✔
142
            throw new LogicException(\sprintf('The mime type of image %s was not guessed.', $path));
26✔
143
        }
144

145
        if (0 !== mb_strpos($binary->getMimeType(), 'image/') && 'application/pdf' !== $binary->getMimeType()) {
78✔
146
            throw new LogicException(\sprintf('The mime type of file %s must be image/xxx or application/pdf, got %s.', $path, $binary->getMimeType()));
26✔
147
        }
148

149
        return $binary;
52✔
150
    }
151

152
    /**
153
     * Get default image url with the given filter applied.
154
     *
155
     * @param string $filter
156
     *
157
     * @return string|null
158
     */
159
    public function getDefaultImageUrl($filter)
160
    {
161
        $config = $this->filterConfig->get($filter);
26✔
162

163
        $defaultImage = null;
26✔
164
        if (false === empty($config['default_image'])) {
26✔
165
            $defaultImage = $config['default_image'];
13✔
166
        } elseif (!empty($this->globalDefaultImage)) {
13✔
167
            $defaultImage = $this->globalDefaultImage;
13✔
168
        }
169

170
        return $defaultImage;
26✔
171
    }
172

173
    private function getExtension(?string $mimeType): ?string
174
    {
175
        if ($this->extensionGuesser instanceof DeprecatedExtensionGuesserInterface) {
78✔
176
            return $this->extensionGuesser->guess($mimeType);
×
177
        }
178

179
        if (null === $mimeType) {
78✔
180
            return null;
13✔
181
        }
182

183
        return $this->extensionGuesser->getExtensions($mimeType)[0] ?? null;
65✔
184
    }
185
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc