Test Failed
Branch master (df1c42)
by Mostafa
15:23
created

ImageStyle::make()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 5
dl 0
loc 3
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 make(string $name, ?int $width = null, ?int $height = null, LaruploadMediaStyle $mode = LaruploadMediaStyle::AUTO, bool $padding = false): self
22
    {
23
        return new self($name, $width, $height, $mode, $padding);
24
    }
25
26
27
    private function validateDimension(): void
28
    {
29
        if ($this->mode === LaruploadMediaStyle::SCALE_HEIGHT) {
30
            if ($this->width === null or $this->width === 0) {
31
                throw new Exception(
32
                    'Width is required when you are in SCALE_HEIGHT mode'
33
                );
34
            }
35
        }
36
        else if ($this->mode === LaruploadMediaStyle::SCALE_WIDTH) {
37
            if ($this->height === null or $this->height === 0) {
38
                throw new Exception(
39
                    'Height is required when you are in SCALE_WIDTH mode'
40
                );
41
            }
42
        }
43
        else if (in_array($this->mode, [LaruploadMediaStyle::CROP, LaruploadMediaStyle::FIT])) {
44
            if (!$this->width or !$this->height) {
45
                throw new Exception(
46
                    'Width and Height are required when you are in CROP/FIT mode'
47
                );
48
            }
49
        }
50
        else if ($this->mode === LaruploadMediaStyle::AUTO) {
51
            if (!$this->width and !$this->height) {
52
                throw new Exception(
53
                    'Width and height are required when you are in auto mode'
54
                );
55
            }
56
        }
57
    }
58
}
59