Completed
Push — master ( c49bae...43ad9f )
by
unknown
09:25
created

ImageGenerator::doGenerateImage()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 8
ccs 5
cts 5
cp 1
rs 9.4285
c 1
b 0
f 0
cc 1
eloc 5
nc 1
nop 3
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\ParameterBag;
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 4
    public function __construct(
51
        Server $server,
52
        FilenameGeneratorInterface $filenameGenerator,
53
        $defaultImageParameters = [],
54
        $fallbackImage = null,
55
        $tmpPath = null,
56
        $tmpPrefix = null
57
    ) {
58 4
        $this->server = $server;
59 4
        $this->filenameGenerator = $filenameGenerator;
60 4
        $this->defaultImageParameters = $defaultImageParameters;
61 4
        $this->fallbackImage = $fallbackImage;
62 4
        $this->tmpPath = $tmpPath;
63 4
        $this->tmpPrefix = $tmpPrefix;
64 4
    }
65
66
    /**
67
     * @param MediaInterface $media
68
     * @param ParameterBag $parameterBag
69
     * @return mixed
70
     */
71 4
    public function generate(MediaInterface $media, ParameterBag $parameterBag)
72
    {
73 4
        $parameterBag->setDefaults($this->defaultImageParameters);
74
75 4
        $filename = $this->filenameGenerator->generate($media, $parameterBag);
76
77 4
        if (!$this->server->getSource()->has($filename)) {
78 3
            $this->generateImage($media, $parameterBag, $filename);
79 3
        }
80
81 4
        return $filename;
82
    }
83
84
    /**
85
     * @param MediaInterface $media
86
     * @param ParameterBag $parameterBag
87
     * @param $filename
88
     * @throws FilesystemException
89
     * @throws \Exception
90
     */
91 3
    protected function generateImage(MediaInterface $media, ParameterBag $parameterBag, $filename)
92
    {
93 3
        $tmp = $this->getTemporaryFile();
94 3
        $imageData = $this->getImageData($media);
95
96 3
        if (@file_put_contents($tmp, $imageData) === false) {
97
            throw new FilesystemException('Unable to write temporary file');
98
        }
99
100
        try {
101 3
            $this->server->getCache()->put($filename, $this->doGenerateImage($media, $tmp, $parameterBag));
102 3
        } catch (\Exception $e) {
103
            throw new \Exception('Could not generate image', 0, $e);
104 3
        } finally {
105 3
            if (file_exists($tmp)) {
106 3
                if (!@unlink($tmp)) {
107
                    throw new FilesystemException('Unable to clean up temporary file');
108
                }
109 3
            }
110
        }
111 3
    }
112
113
    /**
114
     * @param MediaInterface $media
115
     * @return string
116
     * @throws FilesystemException
117
     */
118 3
    protected function getImageData(MediaInterface $media)
119
    {
120 3
        if ($this->server->getSource()->has($media->getImage())) {
121 3
            return $this->server->getSource()->read($media->getImage());
122
        }
123
124
        if (!is_null($this->fallbackImage)) {
125
            return file_get_contents($this->fallbackImage);
126
        }
127
128
        throw new FilesystemException('File not found');
129
    }
130
131
    /**
132
     * @param MediaInterface $media
133
     * @param string $tmp
134
     * @param ParameterBag $parameterBag
135
     * @return string
136
     */
137 3
    protected function doGenerateImage(MediaInterface $media, $tmp, ParameterBag $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...
138
    {
139 3
        $parameters = $parameterBag->getExtra();
140 3
        $parameters['w'] = $parameterBag->getWidth();
141 3
        $parameters['h'] = $parameterBag->getHeight();
142
143 3
        return $this->server->getApi()->run($tmp, $parameters);
144
    }
145
146
    /**
147
     * @return string
148
     */
149 3
    protected function getTemporaryFile()
150
    {
151 3
        if (empty($this->tmpPath)) {
152 3
            $this->tmpPath = sys_get_temp_dir();
153 3
        }
154 3
        if (empty($this->tmpPrefix)) {
155 3
            $this->tmpPrefix = 'media';
156 3
        }
157
158 3
        return tempnam($this->tmpPath, $this->tmpPrefix);
159
    }
160
161
    /**
162
     * @return Server
163
     */
164
    public function getServer()
165
    {
166
        return $this->server;
167
    }
168
169
    /**
170
     * @return FilenameGeneratorInterface
171
     */
172
    public function getFilenameGenerator()
173
    {
174
        return $this->filenameGenerator;
175
    }
176
177
    /**
178
     * @return string
179
     */
180
    public function getTmpPath()
181
    {
182
        return $this->tmpPath;
183
    }
184
185
    /**
186
     * @return string
187
     */
188
    public function getTmpPrefix()
189
    {
190
        return $this->tmpPrefix;
191
    }
192
193
    /**
194
     * @return string
195
     */
196
    public function getFallbackImage()
197
    {
198
        return $this->fallbackImage;
199
    }
200
}
201