Test Failed
Push — master ( c898b7...b0c798 )
by Petr
02:29
created

ImageOrientate::process()   B

Complexity

Conditions 6
Paths 10

Size

Total Lines 41
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
eloc 21
c 1
b 0
f 0
nc 10
nop 2
dl 0
loc 41
rs 8.9617
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 ImageOrientate
19
 * Orientate image against the data in its exif
20
 * @package kalanis\kw_images\Content
21
 */
22
class ImageOrientate
23
{
24
    use TLang;
25
26
    protected Sources\Image $libImage;
27
    protected Graphics $libGraphics;
28
    protected ISizes $config;
29
30
    public function __construct(Graphics $graphics, ISizes $config, Sources\Image $image, ?IIMTranslations $lang = null)
31
    {
32
        $this->setImLang($lang);
33
        $this->libImage = $image;
34
        $this->libGraphics = $graphics;
35
        $this->config = $config;
36
    }
37
38
    /**
39
     * @param string[] $sourcePath
40
     * @param string[]|null $targetPath
41
     * @throws FilesException
42
     * @throws ImagesException
43
     * @throws MimeException
44
     * @throws PathsException
45
     * @return bool
46
     */
47
    public function process(array $sourcePath, ?array $targetPath = null): bool
48
    {
49
        $sourceFull = array_values($sourcePath);
50
        $targetFull = $targetPath ? array_values($targetPath) : $sourceFull;
51
52
        $tempPath = strval(tempnam(sys_get_temp_dir(), $this->config->getTempPrefix()));
53
54
        // get from the storage
55
        $resource = $this->libImage->get($sourceFull);
56
        if (empty($resource)) {
57
            @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

57
            /** @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...
58
            throw new FilesException($this->getImLang()->imThumbCannotGetBaseImage());
59
        }
60
61
        if (false === @file_put_contents($tempPath, $resource)) {
62
            // @codeCoverageIgnoreStart
63
            throw new FilesException($this->getImLang()->imThumbCannotStoreTemporaryImage());
64
        }
65
        // @codeCoverageIgnoreEnd
66
67
        try {
68
            // now process image locally
69
            $this->libGraphics->orientate($tempPath, $sourceFull, $targetFull);
70
        } catch (ImagesException $ex) {
71
            // clear when fails
72
            @unlink($tempPath);
73
            throw $ex;
74
        }
75
76
        // return result to the storage as new file
77
        $result = @file_get_contents($tempPath);
78
        if (false === $result) {
79
            // @codeCoverageIgnoreStart
80
            @unlink($tempPath);
81
            throw new FilesException($this->getImLang()->imThumbCannotLoadTemporaryImage());
82
        }
83
        // @codeCoverageIgnoreEnd
84
85
        $set = $this->libImage->set($targetFull, $result);
0 ignored issues
show
Bug introduced by
It seems like $targetFull can also be of type null; however, parameter $path of kalanis\kw_images\Sources\Image::set() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

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

85
        $set = $this->libImage->set(/** @scrutinizer ignore-type */ $targetFull, $result);
Loading history...
86
        @unlink($tempPath);
87
        return $set;
88
    }
89
90
    public function getImage(): Sources\Image
91
    {
92
        return $this->libImage;
93
    }
94
}
95