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

Processor::__construct()   B

Complexity

Conditions 10
Paths 2

Size

Total Lines 20
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 10.0454

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 10
eloc 12
c 1
b 0
f 0
nc 2
nop 2
dl 0
loc 20
ccs 12
cts 13
cp 0.9231
crap 10.0454
rs 7.6666

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace kalanis\kw_images\Graphics;
4
5
6
use kalanis\kw_images\ImagesException;
7
use kalanis\kw_images\Interfaces\IIMTranslations;
8
use kalanis\kw_images\Traits\TLang;
9
10
11
/**
12
 * Class Processor
13
 * @package kalanis\kw_images
14
 * Pass images in temporary local storage  - cannot work with images directly in main storage
15
 */
16
class Processor
17
{
18
    use TLang;
19
20
    protected Format\Factory $factory;
21
    /** @var resource|\GdImage|null */
22
    protected $resource = null;
23
24
    /**
25
     * @param Format\Factory $factory
26
     * @param IIMTranslations|null $lang
27
     * @throws ImagesException
28
     */
29 38
    public function __construct(Format\Factory $factory, ?IIMTranslations $lang = null)
30
    {
31 38
        $this->setImLang($lang);
32
33 38
        if (!(function_exists('imagecreatetruecolor')
34 38
            && function_exists('imagecolorallocate')
35 38
            && function_exists('imagesetpixel')
36 38
            && function_exists('imagecopyresized')
37 38
            && function_exists('imagecopyresampled')
38 38
            && function_exists('imagesx')
39 38
            && function_exists('imagesy')
40 38
            && function_exists('imagerotate')
41 38
            && function_exists('imageflip')
42
        )) {
43
            // @codeCoverageIgnoreStart
44
            throw new ImagesException($this->getImLang()->imGdLibNotPresent());
45
        }
46
        // @codeCoverageIgnoreEnd
47
48 38
        $this->factory = $factory;
49 38
    }
50
51
    /**
52
     * @param string $type
53
     * @param string $tempPath
54
     * @throws ImagesException
55
     * @return $this
56
     */
57 15
    public function load(string $type, string $tempPath): self
58
    {
59 15
        $processor = $this->factory->getByType($type, $this->getImLang());
60 14
        $this->resource = $processor->load($tempPath);
61 14
        return $this;
62
    }
63
64
    /**
65
     * @param string $type
66
     * @param string $tempPath
67
     * @throws ImagesException
68
     * @return $this
69
     */
70 11
    public function save(string $type, string $tempPath): self
71
    {
72 11
        $processor = $this->factory->getByType($type, $this->getImLang());
73 11
        $processor->save($tempPath, $this->getResource());
74 11
        return $this;
75
    }
76
77
    /**
78
     * Change image size - cut it to desired size
79
     * @param int|null $width
80
     * @param int|null $height
81
     * @throws ImagesException
82
     * @return $this
83
     */
84 1
    public function resize(?int $width = null, ?int $height = null): self
85
    {
86 1
        $fromWidth = $this->width();
87 1
        $fromHeight = $this->height();
88 1
        $width = (!is_null($width) && (0 < $width)) ? intval($width) : $fromWidth;
89 1
        $height = (!is_null($height) && (0 < $height)) ? intval($height) : $fromHeight;
90 1
        $resource = $this->create($width, $height);
91 1
        if (false === imagecopyresized($resource, $this->getResource(), 0, 0, 0, 0, $width, $height, $fromWidth, $fromHeight)) {
92
            // @codeCoverageIgnoreStart
93
            imagedestroy($resource);
94
            throw new ImagesException($this->getImLang()->imImageCannotResize());
95
        }
96
        // @codeCoverageIgnoreEnd
97 1
        imagedestroy($this->getResource());
98 1
        $this->resource = $resource;
99 1
        return $this;
100
    }
101
102
    /**
103
     * Change image size - content will change its proportions according the passed sizes
104
     * @param int|null $width
105
     * @param int|null $height
106
     * @throws ImagesException
107
     * @return $this
108
     */
109 9
    public function resample(?int $width = null, ?int $height = null): self
110
    {
111 9
        $fromWidth = $this->width();
112 9
        $fromHeight = $this->height();
113 9
        $width = (!is_null($width) && (0 < $width)) ? intval($width) : $fromWidth;
114 9
        $height = (!is_null($height) && (0 < $height)) ? intval($height) : $fromHeight;
115 9
        $resource = $this->create($width, $height);
116 9
        if (false === imagecopyresampled($resource, $this->getResource(), 0, 0, 0, 0, $width, $height, $fromWidth, $fromHeight)) {
117
            // @codeCoverageIgnoreStart
118
            imagedestroy($resource);
119
            throw new ImagesException($this->getImLang()->imImageCannotResample());
120
        }
121
        // @codeCoverageIgnoreEnd
122 9
        imagedestroy($this->getResource());
123 9
        $this->resource = $resource;
124 9
        return $this;
125
    }
126
127
    /**
128
     * Rotate image by passed angle
129
     * @param float $angle
130
     * @throws ImagesException
131
     * @return $this
132
     */
133 1
    public function rotate(float $angle): self
134
    {
135 1
        $image = imagerotate($this->resource, $angle, 0);
136 1
        if (empty($image)) {
137
            // @codeCoverageIgnoreStart
138
            throw new ImagesException($this->getImLang()->imImageCannotOrientate());
139
        }
140
        // @codeCoverageIgnoreEnd
141 1
        $this->resource = $image;
142 1
        return $this;
143
    }
144
145
    /**
146
     * Flip image
147
     * @return $this
148
     */
149 1
    public function flip(int $mode): self
150
    {
151 1
        if (in_array($mode, [IMG_FLIP_HORIZONTAL, IMG_FLIP_VERTICAL, IMG_FLIP_BOTH])) {
152 1
            imageflip($this->resource, $mode);
153
        }
154 1
        return $this;
155
    }
156
157
    /**
158
     * Create empty image resource
159
     * @param int $width
160
     * @param int $height
161
     * @throws ImagesException
162
     * @return \GdImage|resource
163
     */
164 10
    protected function create(int $width, int $height)
165
    {
166 10
        $resource = imagecreatetruecolor($width, $height);
167 10
        if (false === $resource) {
168
            // @codeCoverageIgnoreStart
169
            throw new ImagesException($this->getImLang()->imImageCannotCreateEmpty());
170
        }
171
        // @codeCoverageIgnoreEnd
172 10
        return $resource;
173
    }
174
175
    /**
176
     * @throws ImagesException
177
     * @return int
178
     */
179 13
    public function width(): int
180
    {
181 13
        $size = imagesx($this->getResource());
182 13
        if (false === $size) {
183
            // @codeCoverageIgnoreStart
184
            throw new ImagesException($this->getImLang()->imImageCannotGetSize());
185
        }
186
        // @codeCoverageIgnoreEnd
187 13
        return intval($size);
188
    }
189
190
    /**
191
     * @throws ImagesException
192
     * @return int
193
     */
194 15
    public function height(): int
195
    {
196 15
        $size = imagesy($this->getResource());
197 14
        if (false === $size) {
198
            // @codeCoverageIgnoreStart
199
            throw new ImagesException($this->getImLang()->imImageCannotGetSize());
200
        }
201
        // @codeCoverageIgnoreEnd
202 14
        return intval($size);
203
    }
204
205
    /**
206
     * @throws ImagesException
207
     * @return \GdImage|resource
208
     */
209 15
    public function getResource()
210
    {
211 15
        if (empty($this->resource)) {
212 1
            throw new ImagesException($this->getImLang()->imImageLoadFirst());
213
        }
214 14
        return $this->resource;
215
    }
216
}
217