Passed
Push — master ( 3d9115...77fb6a )
by Petr
08:28
created

ImageRotate::getImage()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
c 1
b 0
f 0
1
<?php
2
3
namespace kalanis\kw_images\Content;
4
5
6
use kalanis\kw_files\FilesException;
7
use kalanis\kw_images\Graphics;
8
use kalanis\kw_images\ImagesException;
9
use kalanis\kw_images\Interfaces\IIMTranslations;
10
use kalanis\kw_images\Interfaces\ISizes;
11
use kalanis\kw_images\Sources;
12
use kalanis\kw_images\Traits\TLang;
13
use kalanis\kw_mime\MimeException;
14
use kalanis\kw_paths\PathsException;
15
16
17
/**
18
 * Class ImageRotate
19
 * Rotate image against the passed data
20
 * @package kalanis\kw_images\Content
21
 */
22
class ImageRotate
23
{
24
    use TLang;
25
26
    protected Sources\Image $libImage;
27
    protected Graphics $libGraphics;
28
    protected ISizes $config;
29
30 11
    public function __construct(Graphics $graphics, ISizes $config, Sources\Image $image, ?IIMTranslations $lang = null)
31
    {
32 11
        $this->setImLang($lang);
33 11
        $this->libImage = $image;
34 11
        $this->libGraphics = $graphics;
35 11
        $this->config = $config;
36 11
    }
37
38
    /**
39
     * @param string[] $sourcePath
40
     * @param float|null $rotateAngle target angle
41
     * @param int|null $flipMode as defined by function imageflip [IMG_FLIP_HORIZONTAL, IMG_FLIP_VERTICAL, IMG_FLIP_BOTH]
42
     * @param string[]|null $targetPath mainly for tests
43
     * @throws FilesException
44
     * @throws ImagesException
45
     * @throws MimeException
46
     * @throws PathsException
47
     * @return bool
48
     */
49 4
    public function process(array $sourcePath, ?float $rotateAngle, ?int $flipMode = null, ?array $targetPath = null): bool
50
    {
51 4
        $sourceFull = array_values($sourcePath);
52 4
        $targetFull = $targetPath ? array_values($targetPath) : $sourceFull;
53
54 4
        $tempPath = strval(tempnam(sys_get_temp_dir(), $this->config->getTempPrefix()));
55
56
        // get from the storage
57 4
        $resource = $this->libImage->get($sourceFull);
58 4
        if (empty($resource)) {
59 1
            @unlink($tempPath);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition for unlink(). This can introduce security issues, and is generally not recommended. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unhandled  annotation

59
            /** @scrutinizer ignore-unhandled */ @unlink($tempPath);

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...
60 1
            throw new FilesException($this->getImLang()->imThumbCannotGetBaseImage());
61
        }
62
63 3
        if (false === @file_put_contents($tempPath, $resource)) {
64
            // @codeCoverageIgnoreStart
65
            @unlink($tempPath);
66
            throw new FilesException($this->getImLang()->imThumbCannotStoreTemporaryImage());
67
        }
68
        // @codeCoverageIgnoreEnd
69
70
        try {
71
            // now process image locally
72 3
            $this->libGraphics->rotate($rotateAngle, $flipMode, $tempPath, $sourceFull, $targetFull);
73 1
        } catch (ImagesException $ex) {
74
            // clear when fails
75 1
            @unlink($tempPath);
76 1
            throw $ex;
77
        }
78
79
        // return result to the storage as new file
80 2
        $result = @file_get_contents($tempPath);
81 2
        if (false === $result) {
82
            // @codeCoverageIgnoreStart
83
            @unlink($tempPath);
84
            throw new FilesException($this->getImLang()->imThumbCannotLoadTemporaryImage());
85
        }
86
        // @codeCoverageIgnoreEnd
87
88 2
        $set = $this->libImage->set($targetFull, $result);
89 2
        @unlink($tempPath);
90 2
        return $set;
91
    }
92
93
    public function getImage(): Sources\Image
94
    {
95
        return $this->libImage;
96
    }
97
}
98