ThumbnailUrlBuilder::width()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 7
ccs 5
cts 5
cp 1
crap 2
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Urvin\Gokaru;
6
7
use Urvin\Gokaru\Exception\InvalidArgumentException;
8
use Urvin\Gokaru\Signature\Generator;
9
10
class ThumbnailUrlBuilder
11
{
12
    /** @var string */
13
    protected string $urlPublic;
14
    /** @var Generator */
15
    protected Generator $signature;
16
    /** @var string */
17
    protected string $sourceType;
18
19
    /** @var int */
20
    protected int $width = 0;
21
    /** @var int */
22
    protected int $height = 0;
23
    /** @var int */
24
    protected int $cast = 0;
25
    /** @var string */
26
    protected string $category = '';
27
    /** @var string */
28
    protected string $filename = '';
29
    /** @var string */
30
    protected string $extension = '';
31
32
    /**
33
     * ThumbnailUrlBuilder constructor.
34
     * @param string $urlPublic
35
     * @param string $sourceType
36
     * @param Generator $signature
37
     */
38 21
    public function __construct(string $urlPublic, string $sourceType, Generator $signature)
39
    {
40 21
        if (empty($urlPublic)) {
41 1
            throw new Exception\InvalidArgumentException('Public url should not be empty');
42
        }
43 20
        if (empty($sourceType)) {
44 1
            throw new Exception\InvalidArgumentException('Source type should not be empty');
45
        }
46 19
        $this->urlPublic = rtrim($urlPublic, '/');
47 19
        $this->sourceType = $sourceType;
48 19
        $this->signature = $signature;
49 19
    }
50
51
    /**
52
     * @param string $filename
53
     * @return $this
54
     */
55 17
    public function filename(string $filename): self
56
    {
57 17
        $this->validateFilename($filename);
58 17
        $this->filename = $filename;
59 17
        return $this;
60
    }
61
62
    /**
63
     * @param string $extension
64
     * @return $this
65
     */
66 17
    public function extension(string $extension): self
67
    {
68 17
        $this->validateExtension($extension);
69 17
        $this->extension = $extension;
70 17
        return $this;
71
    }
72
73
    /**
74
     * @param string $category
75
     * @return $this
76
     */
77 17
    public function category(string $category): self
78
    {
79 17
        $this->validateCategory($category);
80 17
        $this->category = $category;
81 17
        return $this;
82
    }
83
84
    /**
85
     * @param int $value
86
     * @return $this
87
     */
88 18
    public function width(int $value): self
89
    {
90 18
        if ($value < 0) {
91 1
            throw new InvalidArgumentException('Width should be positive');
92
        }
93 18
        $this->width = $value;
94 18
        return $this;
95
    }
96
97
    /**
98
     * @param int $value
99
     * @return $this
100
     */
101 18
    public function height(int $value): self
102
    {
103 18
        if ($value < 0) {
104 1
            throw new InvalidArgumentException('Height should be positive');
105
        }
106 18
        $this->height = $value;
107 18
        return $this;
108
    }
109
110
    /**
111
     * @param int $cast
112
     * @return $this
113
     */
114 18
    public function cast(int $cast): self
115
    {
116 18
        if (empty($cast)) {
117 1
            $this->cast = 0;
118 18
        } elseif ($cast < 0) {
119 1
            throw new InvalidArgumentException('Cast flag should be positive');
120
        } else {
121 18
            $this->cast |= $cast;
122
        }
123
124 18
        return $this;
125
    }
126
127
    /**
128
     * @param string $filename
129
     */
130 18
    protected function validateFilename(string $filename): void
131
    {
132 18
        if (empty($filename)) {
133 2
            throw new InvalidArgumentException('Filename should not be empty');
134
        }
135 17
    }
136
137
    /**
138
     * @param string $category
139
     */
140 18
    protected function validateCategory(string $category): void
141
    {
142 18
        if (empty($category)) {
143 2
            throw new InvalidArgumentException('Category should not be empty');
144
        }
145 17
    }
146
147
    /**
148
     * @param string $extension
149
     */
150 18
    protected function validateExtension(string $extension): void
151
    {
152 18
        if (empty($extension)) {
153 2
            throw new InvalidArgumentException('Extension should not be empty');
154
        }
155 17
    }
156
157
    /**
158
     * @return string
159
     */
160 12
    public function __toString()
161
    {
162 12
        $this->validateCategory($this->category);
163 11
        $this->validateFilename($this->filename);
164 10
        $this->validateExtension($this->extension);
165
166 9
        $hash = $this->signature->Sign(
167 9
            $this->sourceType,
168 9
            $this->category,
169 9
            $this->filename . '.' . $this->extension,
170 9
            $this->width,
171 9
            $this->height,
172 9
            $this->cast
173
        );
174
175 9
        return $this->urlPublic . '/' . implode(
176 9
                '/',
177 9
                array_map(
178 9
                    'urlencode',
179
                    [
180 9
                        $hash,
181 9
                        $this->category,
182 9
                        $this->width,
183 9
                        $this->height,
184 9
                        $this->cast,
185 9
                        $this->filename . '.' . $this->extension
186
                    ]
187
                )
188
            );
189
    }
190
191
}