Completed
Push — master ( 0fbd50...a303a7 )
by
unknown
04:01
created

ImageGenerator::getServer()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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