Passed
Push — master ( 8feef3...f9dc8e )
by Petr
09:08 queued 06:38
created

ImageSize::process()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 31
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 4.0466

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 13
c 1
b 0
f 0
nc 4
nop 2
dl 0
loc 31
ccs 12
cts 14
cp 0.8571
crap 4.0466
rs 9.8333
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\TLang;
13
use kalanis\kw_mime\MimeException;
14
use kalanis\kw_paths\PathsException;
15
16
17
/**
18
 * Class ImageSize
19
 * Process image from source
20
 * @package kalanis\kw_images\Content
21
 */
22
class ImageSize
23
{
24
    use TLang;
25
26
    /** @var Sources\Image */
27
    protected $libImage = null;
28
    /** @var Graphics */
29
    protected $libGraphics = null;
30
    /** @var ISizes */
31
    protected $config = null;
32
33 12
    public function __construct(Graphics $graphics, ISizes $config, Sources\Image $image, ?IIMTranslations $lang = null)
34
    {
35 12
        $this->setLang($lang);
36 12
        $this->libImage = $image;
37 12
        $this->libGraphics = $graphics;
38 12
        $this->config = $config;
39 12
    }
40
41
    /**
42
     * @param string[] $sourcePath
43
     * @param string[] $targetPath
44
     * @throws FilesException
45
     * @throws ImagesException
46
     * @throws MimeException
47
     * @throws PathsException
48
     * @return bool
49
     */
50 6
    public function process(array $sourcePath, array $targetPath): bool
51
    {
52 6
        $sourceFull = array_values($sourcePath);
53 6
        $targetFull = array_values($targetPath);
54
55 6
        $tempPath = strval(tempnam(sys_get_temp_dir(), $this->config->getTempPrefix()));
56
57
        // get from the storage
58 6
        $resource = $this->libImage->get($sourceFull);
59 6
        if (empty($resource)) {
60 1
            throw new FilesException($this->getLang()->imThumbCannotGetBaseImage());
61
        }
62
63 5
        if (false === @file_put_contents($tempPath, $resource)) {
64
            // @codeCoverageIgnoreStart
65
            throw new FilesException($this->getLang()->imThumbCannotStoreTemporaryImage());
66
        }
67
        // @codeCoverageIgnoreEnd
68
69
        // now process image locally
70 5
        $this->libGraphics->setSizes($this->config)->resize($tempPath, $sourceFull, $targetFull);
71
72
        // return result to the storage as new file
73 5
        $result = @file_get_contents($tempPath);
74 5
        if (false === $result) {
75
            // @codeCoverageIgnoreStart
76
            throw new FilesException($this->getLang()->imThumbCannotLoadTemporaryImage());
77
        }
78
        // @codeCoverageIgnoreEnd
79
80 5
        return $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

80
        return $this->libImage->set(/** @scrutinizer ignore-type */ $targetFull, $result);
Loading history...
81
    }
82
83 3
    public function getImage(): Sources\Image
84
    {
85 3
        return $this->libImage;
86
    }
87
}
88