Passed
Push — master ( ac2b2c...2480f5 )
by Jonás
08:34 queued 06:14
created

Scale::verifyIfThePropertyChange()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 2
eloc 1
c 1
b 0
f 1
nc 2
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Jonasdamher\Simplifyimage\Utils;
6
7
use Jonasdamher\Simplifyimage\Core\ResponseHandler;
8
9
/**
10
 * Add new scale to image.
11
 */
12
class Scale
13
{
14
15
	private array $dimensions = [
16
		'width' => -1,
17
		'height' => -1
18
	];
19
20
	public function get(): array
21
	{
22
		return $this->dimensions;
23
	}
24
25
	/**
26
	 * New width and height image.
27
	 * @param int $width
28
	 * @param int $height (optional) by default equal width
29
	 */
30
	public function set(int $width, int $height = -1)
31
	{
32
		$this->dimensions['width'] = $width;
33
		$this->dimensions['height'] = $height;
34
	}
35
36
	private function verifyIfThePropertyChange(): bool
37
	{
38
		return ($this->get()['width'] != -1 || $this->get()['height'] != -1);
39
	}
40
41
	public function modify($image)
42
	{
43
		try {
44
			if ($this->verifyIfThePropertyChange()) {
45
46
				$imageWithNewScale = imagescale(
47
					$image,
48
					$this->get()['width'],
49
					$this->get()['height'],
50
					IMG_BILINEAR_FIXED
51
				);
52
				if (!$imageWithNewScale) {
53
					throw new \Exception('Error with scale, dimesions(' . $this->get()['width'] . ',' . $this->get()['height'] . ').');
54
				}
55
56
				$image = $imageWithNewScale;
57
			}
58
		} catch (\Exception $e) {
59
			ResponseHandler::fail($e->getMessage());
60
		} finally {
61
			return $image;
62
		}
63
	}
64
}
65