Completed
Push — master ( 000445...c8f0e6 )
by
unknown
04:21
created

ImageGenerator::generateImage()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 14
Code Lines 10

Duplication

Lines 3
Ratio 21.43 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 3
loc 14
ccs 0
cts 14
cp 0
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 10
nc 4
nop 3
crap 12
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
    public function __construct(
38
        Server $server,
39
        FilenameGeneratorInterface $filenameGenerator,
40
        $tmpPath = null,
41
        $tmpPrefix = null
42
    ) {
43
        $this->server = $server;
44
        $this->filenameGenerator = $filenameGenerator;
45
        $this->tmpPath = $tmpPath;
46
        $this->tmpPrefix = $tmpPrefix;
47
    }
48
49
    /**
50
     * @param MediaInterface $media
51
     * @param array $parameters
52
     * @return mixed
53
     * @throws FilesystemException
54
     */
55
    public function generate(MediaInterface $media, array $parameters)
56
    {
57
        $filename = $this->filenameGenerator->generate($media, $parameters);
58
59
        if (!$this->server->getSource()->has($filename)) {
60
            $this->generateImage($media, $parameters, $filename);
61
        }
62
63
        return $filename;
64
    }
65
66
    /**
67
     * @param MediaInterface $media
68
     * @param array $parameters
69
     * @param $filename
70
     * @throws FilesystemException
71
     * @throws \Exception
72
     */
73
    private function generateImage(MediaInterface $media, array $parameters, $filename)
74
    {
75
        $tmp = $this->getTemporaryFile();
76 View Code Duplication
        if (@file_put_contents($tmp, $this->server->getSource()->read($media->getImage())) === false) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
77
            throw new FilesystemException('Unable to write temporary file');
78
        }
79
        try {
80
            $this->server->getCache()->write($filename, $this->server->getApi()->run($tmp, $parameters));
81
        } catch (\Exception $e) {
82
            throw new \Exception('Could not generate image', 0, $e);
83
        } finally {
84
            @unlink($tmp);
85
        }
86
    }
87
88
    /**
89
     * @return string
90
     */
91
    private function getTemporaryFile()
92
    {
93
        if (empty($this->tmpPath)) {
94
            $this->tmpPath = sys_get_temp_dir();
95
        }
96
        if (empty($this->tmpPrefix)) {
97
            $this->tmpPrefix = 'media';
98
        }
99
100
        return tempnam($this->tmpPath, $this->tmpPrefix);
101
    }
102
}
103