Completed
Push — master ( 285b1b...2fc430 )
by
unknown
26:57
created

ImageGenerator::generateImage()   B

Complexity

Conditions 5
Paths 10

Size

Total Lines 24
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 5.0488

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 24
ccs 14
cts 16
cp 0.875
rs 8.5125
c 1
b 0
f 0
cc 5
eloc 16
nc 10
nop 3
crap 5.0488
1
<?php
2
3
namespace MediaMonks\SonataMediaBundle\Generator;
4
5
use League\Glide\Filesystem\FilesystemException;
6
use League\Glide\Server;
7
use MediaMonks\SonataMediaBundle\ErrorHandlerTrait;
8
use MediaMonks\SonataMediaBundle\ParameterBag\ImageParameterBag;
9
use MediaMonks\SonataMediaBundle\Model\MediaInterface;
10
11
class ImageGenerator
12
{
13
    use ErrorHandlerTrait;
14
15
    /**
16
     * @var Server
17
     */
18
    private $server;
19
20
    /**
21
     * @var FilenameGeneratorInterface
22
     */
23
    private $filenameGenerator;
24
25
    /**
26
     * @var array
27
     */
28
    private $defaultImageParameters;
29
30
    /**
31
     * @var string
32
     */
33
    private $tmpPath;
34
35
    /**
36
     * @var string
37
     */
38
    private $tmpPrefix;
39
40
    /**
41
     * @var string
42
     */
43
    private $fallbackImage;
44
45
    /**
46
     * @param Server $server
47
     * @param FilenameGeneratorInterface $filenameGenerator
48
     * @param array $defaultImageParameters
49
     * @param null $fallbackImage
50
     * @param null $tmpPath
51
     * @param null $tmpPrefix
52
     */
53 8
    public function __construct(
54
        Server $server,
55
        FilenameGeneratorInterface $filenameGenerator,
56
        $defaultImageParameters = [],
57
        $fallbackImage = null,
58
        $tmpPath = null,
59
        $tmpPrefix = null
60
    ) {
61 8
        $this->server = $server;
62 8
        $this->filenameGenerator = $filenameGenerator;
63 8
        $this->defaultImageParameters = $defaultImageParameters;
64 8
        $this->fallbackImage = $fallbackImage;
65 8
        $this->tmpPath = $tmpPath;
66 8
        $this->tmpPrefix = $tmpPrefix;
67 8
    }
68
69
    /**
70
     * @param MediaInterface $media
71
     * @param ImageParameterBag $parameterBag
72
     * @return mixed
73
     */
74 7
    public function generate(MediaInterface $media, ImageParameterBag $parameterBag)
75
    {
76 7
        $parameterBag->setDefaults($this->defaultImageParameters);
77 7
        if (!$parameterBag->hasExtra('fit')) {
78 7
            $parameterBag->addExtra('fit', 'crop-'.$media->getFocalPoint());
79
        }
80
81 7
        $filename = $this->filenameGenerator->generate($media, $parameterBag);
82
83 7
        if (!$this->server->getCache()->has($filename)) {
84 6
            $this->generateImage($media, $parameterBag, $filename);
85
        }
86
87 5
        return $filename;
88
    }
89
90
    /**
91
     * @param MediaInterface $media
92
     * @param ImageParameterBag $parameterBag
93
     * @param $filename
94
     * @throws FilesystemException
95
     * @throws \Exception
96
     */
97 6
    protected function generateImage(MediaInterface $media, ImageParameterBag $parameterBag, $filename)
98
    {
99 6
        $tmp = $this->getTemporaryFile();
100 6
        $imageData = $this->getImageData($media);
101
102 6
        $this->disableErrorHandler();
103 6
        if (file_put_contents($tmp, $imageData) === false) {
104
            $this->restoreErrorHandler();
105
            throw new FilesystemException('Unable to write temporary file');
106
        }
107 6
        $this->restoreErrorHandler();
108
109
        try {
110 6
            $this->server->getCache()->put($filename, $this->doGenerateImage($media, $tmp, $parameterBag));
111 2
        } catch (\Exception $e) {
112 2
            throw new FilesystemException('Could not generate image', 0, $e);
113 4
        } finally {
114 6
            if (file_exists($tmp)) {
115 6
                if (!@unlink($tmp)) {
116 6
                    throw new FilesystemException('Unable to clean up temporary file');
117
                }
118
            }
119
        }
120 4
    }
121
122
    /**
123
     * @param MediaInterface $media
124
     * @return string
125
     * @throws FilesystemException
126
     */
127 6
    protected function getImageData(MediaInterface $media)
128
    {
129 6
        if ($this->server->getSource()->has($media->getImage())) {
130 6
            return $this->server->getSource()->read($media->getImage());
131
        }
132
133
        if (!is_null($this->fallbackImage)) {
134
            return file_get_contents($this->fallbackImage);
135
        }
136
137
        throw new FilesystemException('File not found');
138
    }
139
140
    /**
141
     * @param MediaInterface $media
142
     * @param string $tmp
143
     * @param ImageParameterBag $parameterBag
144
     * @return string
145
     */
146 6
    protected function doGenerateImage(MediaInterface $media, $tmp, ImageParameterBag $parameterBag)
0 ignored issues
show
Unused Code introduced by
The parameter $media is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
147
    {
148 6
        $parameters = $parameterBag->getExtra();
149 6
        $parameters['w'] = $parameterBag->getWidth();
150 6
        $parameters['h'] = $parameterBag->getHeight();
151
152 6
        return $this->server->getApi()->run($tmp, $parameters);
153
    }
154
155
    /**
156
     * @return string
157
     */
158 6
    protected function getTemporaryFile()
159
    {
160 6
        if (empty($this->tmpPath)) {
161 5
            $this->tmpPath = sys_get_temp_dir();
162
        }
163 6
        if (empty($this->tmpPrefix)) {
164 6
            $this->tmpPrefix = 'media';
165
        }
166
167 6
        $this->disableErrorHandler();
168 6
        $tempFile = tempnam($this->tmpPath, $this->tmpPrefix);
169 6
        $this->restoreErrorHandler();
170
171 6
        return $tempFile;
172
    }
173
174
    /**
175
     * @return Server
176
     */
177 1
    public function getServer()
178
    {
179 1
        return $this->server;
180
    }
181
182
    /**
183
     * @return FilenameGeneratorInterface
184
     */
185 1
    public function getFilenameGenerator()
186
    {
187 1
        return $this->filenameGenerator;
188
    }
189
190
    /**
191
     * @return array
192
     */
193 1
    public function getDefaultImageParameters()
194
    {
195 1
        return $this->defaultImageParameters;
196
    }
197
198
    /**
199
     * @return string
200
     */
201 1
    public function getTmpPath()
202
    {
203 1
        return $this->tmpPath;
204
    }
205
206
    /**
207
     * @return string
208
     */
209 1
    public function getTmpPrefix()
210
    {
211 1
        return $this->tmpPrefix;
212
    }
213
214
    /**
215
     * @return string
216
     */
217 1
    public function getFallbackImage()
218
    {
219 1
        return $this->fallbackImage;
220
    }
221
}
222