Test Failed
Push — master ( 178b51...f1c521 )
by Mostafa
56s queued 13s
created

ImageStyle::__set_state()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 1
eloc 6
c 1
b 1
f 0
nc 1
nop 1
dl 0
loc 8
rs 10
1
<?php
2
3
namespace Mostafaznv\Larupload\DTOs\Style;
4
5
use Exception;
6
use Mostafaznv\Larupload\Enums\LaruploadMediaStyle;
7
8
class ImageStyle extends Style
9
{
10
    public readonly LaruploadMediaStyle $mode;
11
12
    public function __construct(string $name, ?int $width = null, ?int $height = null, LaruploadMediaStyle $mode = LaruploadMediaStyle::AUTO, bool $padding = false)
13
    {
14
        parent::__construct($name, $width, $height, $padding);
15
16
        $this->mode = $mode;
0 ignored issues
show
Bug introduced by
The property mode is declared read-only in Mostafaznv\Larupload\DTOs\Style\ImageStyle.
Loading history...
17
18
        $this->validateDimension();
19
    }
20
21
    public static function __set_state(array $array): object
22
    {
23
        return new self(
24
            name: $array['name'],
25
            width: $array['width'],
26
            height: $array['height'],
27
            mode: $array['mode'],
28
            padding: $array['padding']
29
        );
30
    }
31
32
    public static function make(string $name, ?int $width = null, ?int $height = null, LaruploadMediaStyle $mode = LaruploadMediaStyle::AUTO, bool $padding = false): self
33
    {
34
        return new self($name, $width, $height, $mode, $padding);
35
    }
36
37
38
    private function validateDimension(): void
39
    {
40
        if ($this->mode === LaruploadMediaStyle::SCALE_HEIGHT) {
41
            if ($this->width === null or $this->width === 0) {
42
                throw new Exception(
43
                    'Width is required when you are in SCALE_HEIGHT mode'
44
                );
45
            }
46
        }
47
        else if ($this->mode === LaruploadMediaStyle::SCALE_WIDTH) {
48
            if ($this->height === null or $this->height === 0) {
49
                throw new Exception(
50
                    'Height is required when you are in SCALE_WIDTH mode'
51
                );
52
            }
53
        }
54
        else if (in_array($this->mode, [LaruploadMediaStyle::CROP, LaruploadMediaStyle::FIT])) {
55
            if (!$this->width or !$this->height) {
56
                throw new Exception(
57
                    'Width and Height are required when you are in CROP/FIT mode'
58
                );
59
            }
60
        }
61
        else if ($this->mode === LaruploadMediaStyle::AUTO) {
62
            if (!$this->width and !$this->height) {
63
                throw new Exception(
64
                    'Width and height are required when you are in auto mode'
65
                );
66
            }
67
        }
68
    }
69
}
70