Completed
Push — master ( c5041c...dff1db )
by
unknown
27:23
created

ImageGenerator::generate()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 3.009

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 15
ccs 9
cts 10
cp 0.9
rs 9.4285
c 1
b 0
f 0
cc 3
eloc 8
nc 4
nop 2
crap 3.009
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 4
    public function __construct(
54
        Server $server,
55
        FilenameGeneratorInterface $filenameGenerator,
56
        $defaultImageParameters = [],
57
        $fallbackImage = null,
58
        $tmpPath = null,
59
        $tmpPrefix = null
60
    ) {
61 4
        $this->server = $server;
62 4
        $this->filenameGenerator = $filenameGenerator;
63 4
        $this->defaultImageParameters = $defaultImageParameters;
64 4
        $this->fallbackImage = $fallbackImage;
65 4
        $this->tmpPath = $tmpPath;
66 4
        $this->tmpPrefix = $tmpPrefix;
67 4
    }
68
69
    /**
70
     * @param MediaInterface $media
71
     * @param ImageParameterBag $parameterBag
72
     * @return mixed
73
     */
74 3
    public function generate(MediaInterface $media, ImageParameterBag $parameterBag)
75
    {
76 3
        $parameterBag->setDefaults($this->defaultImageParameters);
77 3
        if (!$parameterBag->hasExtra('fit')) {
78 3
            $parameterBag->addExtra('fit', 'crop-'.$media->getFocalPoint());
79 3
        }
80
81 3
        $filename = $this->filenameGenerator->generate($media, $parameterBag);
82
83 3
        if (!$this->server->getCache()->has($filename)) {
84 2
            $this->generateImage($media, $parameterBag, $filename);
85
        }
86
87 1
        return $filename;
88
    }
89
90
    /**
91
     * @param MediaInterface $media
92
     * @param ImageParameterBag $parameterBag
93
     * @param $filename
94
     * @throws FilesystemException
95
     * @throws \Exception
96
     */
97 2
    protected function generateImage(MediaInterface $media, ImageParameterBag $parameterBag, $filename)
98
    {
99 2
        $tmp = $this->getTemporaryFile();
100 2
        $imageData = $this->getImageData($media);
101
102 2
        $this->disableErrorHandler();
103 2
        if (file_put_contents($tmp, $imageData) === false) {
104
            $this->restoreErrorHandler();
105
            throw new FilesystemException('Unable to write temporary file');
106
        }
107 2
        $this->restoreErrorHandler();
108
109
        try {
110 2
            $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
        } finally {
114 2
            if (file_exists($tmp)) {
115 2
                if (!@unlink($tmp)) {
116
                    throw new FilesystemException('Unable to clean up temporary file');
117
                }
118 2
            }
119 2
        }
120
    }
121
122
    /**
123
     * @param MediaInterface $media
124
     * @return string
125
     * @throws FilesystemException
126
     */
127 2
    protected function getImageData(MediaInterface $media)
128
    {
129 2
        if ($this->server->getSource()->has($media->getImage())) {
130 2
            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 2
    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 2
        $parameters = $parameterBag->getExtra();
149 2
        $parameters['w'] = $parameterBag->getWidth();
150 2
        $parameters['h'] = $parameterBag->getHeight();
151
152 2
        return $this->server->getApi()->run($tmp, $parameters);
153
    }
154
155
    /**
156
     * @return string
157
     */
158 2
    protected function getTemporaryFile()
159
    {
160 2
        if (empty($this->tmpPath)) {
161 1
            $this->tmpPath = sys_get_temp_dir();
162 1
        }
163 2
        if (empty($this->tmpPrefix)) {
164 2
            $this->tmpPrefix = 'media';
165 2
        }
166
167 2
        $this->disableErrorHandler();
168 2
        $tempFile = tempnam($this->tmpPath, $this->tmpPrefix);
169 2
        $this->restoreErrorHandler();
170
171 2
        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