Completed
Push — master ( 4dc64d...690bae )
by
unknown
03:31
created

ImageGenerator::generate()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 10
ccs 0
cts 6
cp 0
rs 9.4285
cc 2
eloc 5
nc 2
nop 2
crap 6
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
    private 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($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
    private 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 $filename
119
     * @param $tmp
120
     * @param array $parameters
121
     */
122
    private function doGenerateImage($filename, $tmp, array $parameters)
123
    {
124
        $this->server->getCache()->write($filename, $this->server->getApi()->run($tmp, $parameters));
125
    }
126
127
    /**
128
     * @return string
129
     */
130
    private function getTemporaryFile()
131
    {
132
        if (empty($this->tmpPath)) {
133
            $this->tmpPath = sys_get_temp_dir();
134
        }
135
        if (empty($this->tmpPrefix)) {
136
            $this->tmpPrefix = 'media';
137
        }
138
139
        return tempnam($this->tmpPath, $this->tmpPrefix);
140
    }
141
}
142