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

Thumbnail::createIfNotExists()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 7

Duplication

Lines 3
Ratio 27.27 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 3
loc 11
ccs 0
cts 11
cp 0
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 7
nc 3
nop 3
crap 12
1
<?php
2
3
namespace MediaMonks\SonataMediaBundle\Helper;
4
5
use League\Glide\Filesystem\FilesystemException;
6
use League\Glide\Server;
7
8
class Thumbnail
9
{
10
    /**
11
     * @var Server
12
     */
13
    protected $server;
14
15
    /**
16
     * Thumbnail constructor.
17
     * @param Server $server
18
     */
19
    public function __construct(Server $server)
20
    {
21
        $this->server = $server;
22
    }
23
24
    /**
25
     * @param string $source
26
     * @param string $destination
27
     * @param array $parameters
28
     * @throws FilesystemException
29
     */
30
    public function createIfNotExists($source, $destination, array $parameters)
31
    {
32
        if (!$this->server->getSource()->has($destination)) {
33
            $tmp = tempnam(sys_get_temp_dir(), 'media');
34 View Code Duplication
            if (@file_put_contents($tmp, $this->server->getSource()->read($source)) === 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...
35
                throw new FilesystemException('unable_to_write_temporary_media_file');
36
            }
37
            $this->server->getCache()->write($destination, $this->server->getApi()->run($tmp, $parameters));
38
            @unlink($tmp);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
39
        }
40
    }
41
}
42