Completed
Push — master ( d4a912...134347 )
by
unknown
04:55
created

ImageGenerator::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 15
ccs 8
cts 8
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 13
nc 1
nop 6
crap 1
1
<?php
2
3
namespace MediaMonks\SonataMediaBundle\Generator;
4
5
use League\Glide\Filesystem\FilesystemException;
6
use League\Glide\Server;
7
use MediaMonks\SonataMediaBundle\Handler\ImageParameterBag;
8
use MediaMonks\SonataMediaBundle\Model\MediaInterface;
9
10
class ImageGenerator
11
{
12
    /**
13
     * @var Server
14
     */
15
    private $server;
16
17
    /**
18
     * @var FilenameGeneratorInterface
19
     */
20
    private $filenameGenerator;
21
22
    /**
23
     * @var array
24
     */
25
    private $defaultImageParameters;
26
27
    /**
28
     * @var string
29
     */
30
    private $tmpPath;
31
32
    /**
33
     * @var string
34
     */
35
    private $tmpPrefix;
36
37
    /**
38
     * @var string
39
     */
40
    private $fallbackImage;
41
42
    /**
43
     * @param Server $server
44
     * @param FilenameGeneratorInterface $filenameGenerator
45
     * @param array $defaultImageParameters
46
     * @param null $fallbackImage
47
     * @param null $tmpPath
48
     * @param null $tmpPrefix
49
     */
50 6
    public function __construct(
51
        Server $server,
52
        FilenameGeneratorInterface $filenameGenerator,
53
        $defaultImageParameters = [],
54
        $fallbackImage = null,
55
        $tmpPath = null,
56
        $tmpPrefix = null
57
    ) {
58 6
        $this->server = $server;
59 6
        $this->filenameGenerator = $filenameGenerator;
60 6
        $this->defaultImageParameters = $defaultImageParameters;
61 6
        $this->fallbackImage = $fallbackImage;
62 6
        $this->tmpPath = $tmpPath;
63 6
        $this->tmpPrefix = $tmpPrefix;
64 6
    }
65
66
    /**
67
     * @param MediaInterface $media
68
     * @param ImageParameterBag $parameterBag
69
     * @return mixed
70
     */
71 5
    public function generate(MediaInterface $media, ImageParameterBag $parameterBag)
72
    {
73 5
        $parameterBag->setDefaults($this->defaultImageParameters);
74 5
        if (!$parameterBag->hasExtra('fit')) {
75 5
            $parameterBag->addExtra('fit', 'crop-'.$media->getFocalPoint());
76
        }
77
78 5
        $filename = $this->filenameGenerator->generate($media, $parameterBag);
79
80 5
        if (!$this->server->getSource()->has($filename)) {
81 4
            $this->generateImage($media, $parameterBag, $filename);
82
        }
83
84 4
        return $filename;
85
    }
86
87
    /**
88
     * @param MediaInterface $media
89
     * @param ImageParameterBag $parameterBag
90
     * @param $filename
91
     * @throws FilesystemException
92
     * @throws \Exception
93
     */
94 4
    protected function generateImage(MediaInterface $media, ImageParameterBag $parameterBag, $filename)
95
    {
96 4
        $tmp = $this->getTemporaryFile();
97 4
        $imageData = $this->getImageData($media);
98
99 3
        if (@file_put_contents($tmp, $imageData) === false) {
100
            throw new FilesystemException('Unable to write temporary file');
101
        }
102
103
        try {
104 3
            $this->server->getCache()->put($filename, $this->doGenerateImage($media, $tmp, $parameterBag));
105
        } catch (\Exception $e) {
106
            throw new \Exception('Could not generate image', 0, $e);
107 3
        } finally {
108 3
            if (file_exists($tmp)) {
109 3
                if (!@unlink($tmp)) {
110 3
                    throw new FilesystemException('Unable to clean up temporary file');
111
                }
112
            }
113
        }
114 3
    }
115
116
    /**
117
     * @param MediaInterface $media
118
     * @return string
119
     * @throws FilesystemException
120
     */
121 4
    protected function getImageData(MediaInterface $media)
122
    {
123 4
        if ($this->server->getSource()->has($media->getImage())) {
124 3
            return $this->server->getSource()->read($media->getImage());
125
        }
126
127 1
        if (!is_null($this->fallbackImage)) {
128
            return file_get_contents($this->fallbackImage);
129
        }
130
131 1
        throw new FilesystemException('File not found');
132
    }
133
134
    /**
135
     * @param MediaInterface $media
136
     * @param string $tmp
137
     * @param ImageParameterBag $parameterBag
138
     * @return string
139
     */
140 3
    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...
141
    {
142 3
        $parameters = $parameterBag->getExtra();
143 3
        $parameters['w'] = $parameterBag->getWidth();
144 3
        $parameters['h'] = $parameterBag->getHeight();
145
146 3
        return $this->server->getApi()->run($tmp, $parameters);
147
    }
148
149
    /**
150
     * @return string
151
     */
152 4
    protected function getTemporaryFile()
153
    {
154 4
        if (empty($this->tmpPath)) {
155 3
            $this->tmpPath = sys_get_temp_dir();
156
        }
157 4
        if (empty($this->tmpPrefix)) {
158 4
            $this->tmpPrefix = 'media';
159
        }
160
161 4
        return @tempnam($this->tmpPath, $this->tmpPrefix);
162
    }
163
164
    /**
165
     * @return Server
166
     */
167 1
    public function getServer()
168
    {
169 1
        return $this->server;
170
    }
171
172
    /**
173
     * @return FilenameGeneratorInterface
174
     */
175 1
    public function getFilenameGenerator()
176
    {
177 1
        return $this->filenameGenerator;
178
    }
179
180
    /**
181
     * @return array
182
     */
183 1
    public function getDefaultImageParameters()
184
    {
185 1
        return $this->defaultImageParameters;
186
    }
187
188
    /**
189
     * @return string
190
     */
191 1
    public function getTmpPath()
192
    {
193 1
        return $this->tmpPath;
194
    }
195
196
    /**
197
     * @return string
198
     */
199 1
    public function getTmpPrefix()
200
    {
201 1
        return $this->tmpPrefix;
202
    }
203
204
    /**
205
     * @return string
206
     */
207 1
    public function getFallbackImage()
208
    {
209 1
        return $this->fallbackImage;
210
    }
211
}
212