Completed
Push — master ( 11d5f2...dfaaf8 )
by
unknown
04:43
created

ImageGenerator::getTemporaryFile()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 11
c 0
b 0
f 0
ccs 8
cts 8
cp 1
rs 9.4285
cc 3
eloc 6
nc 4
nop 0
crap 3
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
     * @param Server $server
33
     * @param FilenameGeneratorInterface $filenameGenerator
34
     * @param string $tmpPath
35
     * @param string $tmpPrefix
36
     */
37 1
    public function __construct(
38
        Server $server,
39
        FilenameGeneratorInterface $filenameGenerator,
40
        $tmpPath = null,
41
        $tmpPrefix = null
42
    ) {
43 1
        $this->server = $server;
44 1
        $this->filenameGenerator = $filenameGenerator;
45 1
        $this->tmpPath = $tmpPath;
46 1
        $this->tmpPrefix = $tmpPrefix;
47 1
    }
48
49
    /**
50
     * @param MediaInterface $media
51
     * @param array $parameters
52
     * @return string
53
     * @throws FilesystemException
54
     */
55 1
    public function generate(MediaInterface $media, array $parameters)
56
    {
57 1
        $filename = $this->filenameGenerator->generate($media, $parameters);
58
59 1
        if (!$this->server->getSource()->has($filename)) {
60 1
            $this->generateImage($media, $parameters, $filename);
61 1
        }
62
63 1
        return $filename;
64
    }
65
66
    /**
67
     * @param MediaInterface $media
68
     * @param array $parameters
69
     * @param $filename
70
     * @throws FilesystemException
71
     * @throws \Exception
72
     */
73 1
    private function generateImage(MediaInterface $media, array $parameters, $filename)
74
    {
75 1
        $tmp = $this->getTemporaryFile();
76 1
        if (@file_put_contents($tmp, $this->server->getSource()->read($media->getImage())) === false) {
77
            throw new FilesystemException('Unable to write temporary file');
78
        }
79
        try {
80 1
            $this->server->getCache()->write($filename, $this->server->getApi()->run($tmp, $parameters));
81 1
        } catch (\Exception $e) {
82
            throw new \Exception('Could not generate image', 0, $e);
83 1
        } finally {
84 1
            @unlink($tmp);
85
        }
86 1
    }
87
88
    /**
89
     * @return string
90
     */
91 1
    private function getTemporaryFile()
92
    {
93 1
        if (empty($this->tmpPath)) {
94 1
            $this->tmpPath = sys_get_temp_dir();
95 1
        }
96 1
        if (empty($this->tmpPrefix)) {
97 1
            $this->tmpPrefix = 'media';
98 1
        }
99
100 1
        return tempnam($this->tmpPath, $this->tmpPrefix);
101
    }
102
}
103