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

ImageGenerator   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 94
Duplicated Lines 3.19 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 6
dl 3
loc 94
ccs 0
cts 43
cp 0
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 1
A generate() 0 10 2
A generateImage() 3 14 3
A getTemporaryFile() 0 11 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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