LoremPicsumUrl::imageInfoUrl()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
3
namespace Limsum\UrlMakers;
4
5
use Illuminate\Support\Arr;
6
use Limsum\Contracts\LimsumUrlMaker;
7
8
class LoremPicsumUrl implements LimsumUrlMaker
9
{
10
    use HasSize, HasExtension;
11
12
    /**
13
     * Lorem picsum site url.
14
     *
15
     * @var string
16
     */
17
    public string $url = 'https://picsum.photos';
18
19
    /**
20
     * Image grayscale.
21
     *
22
     * @var bool
23
     */
24
    protected bool $grayscale;
25
26
    /**
27
     * Image blur.
28
     *
29
     * @var bool|int
30
     */
31
    protected bool|int $blur;
32
33
    /**
34
     * Set to use random image.
35
     *
36
     * @var bool|int
37
     */
38
    protected bool|int $random;
39
40
    /**
41
     * Seed name.
42
     *
43
     * @var bool|string
44
     */
45
    protected bool|string $seed;
46
47
    /**
48
     * Set specific image ID.
49
     *
50
     * @var bool|int
51
     */
52
    protected bool|int $id;
53
54
    /**
55
     * Predefined Types.
56
     *
57
     * @var array
58
     */
59
    protected array $types = [];
60
61
    /**
62
     * Initial Config.
63
     *
64
     * @var array
65
     */
66
    protected array $config = [];
67
68
    /**
69
     * @param array $config
70
     */
71 12
    public function __construct(array $config)
72
    {
73 12
        $this->config = $config;
74
75 12
        if (isset($this->config['picsum_url'])) {
76 1
            $this->url = (string) $this->config['picsum_url'];
77
        }
78 12
        $this->reset();
79
    }
80
81
    /**
82
     * Get information link about a specific image.
83
     *
84
     * @param int $id
85
     *
86
     * @return string
87
     */
88 1
    public function imageInfoUrl(int $id): string
89
    {
90 1
        return rtrim($this->url) . "/id/{$id}/info";
91
    }
92
93
    /**
94
     * Get a list of images.
95
     *
96
     * @param int $page
97
     * @param int $limit
98
     *
99
     * @return string
100
     */
101 1
    public function listImagesUrl(int $page = 1, int $limit = 30): string
102
    {
103 1
        return rtrim($this->url) . "/v2/list?page={$page}&limit={$limit}";
104
    }
105
106
    /**
107
     * Reset to initial state.
108
     *
109
     * @return static
110
     */
111 12
    public function reset(): static
112
    {
113 12
        $this->width     = (float) Arr::get($this->config, 'default.width', 100);
114 12
        $this->height    = (float) Arr::get($this->config, 'default.height', 100);
115 12
        $this->extension = (string) Arr::get($this->config, 'default.extension', 'jpg');
116 12
        $this->grayscale = (bool) Arr::get($this->config, 'default.grayscale', false);
117 12
        $this->blur      = Arr::get($this->config, 'default.blur', false);
118 12
        $this->random    = Arr::get($this->config, 'default.random', false);
119 12
        $this->seed      = Arr::get($this->config, 'default.seed', false);
120 12
        $this->id        = Arr::get($this->config, 'default.id', false);
121
122 12
        $this->types = (array) Arr::get($this->config, 'types', []);
123
124 12
        return $this;
125
    }
126
127
    /**
128
     * @inheritDoc
129
     */
130 10
    public function url(array|string $params = [], bool $reset = true): string
131
    {
132 10
        if (is_string($params)) {
0 ignored issues
show
introduced by
The condition is_string($params) is always false.
Loading history...
133 1
            $this->type($params);
134
        }
135
136 10
        return $this->generateUrl(is_array($params) ? $params : []);
0 ignored issues
show
introduced by
The condition is_array($params) is always true.
Loading history...
137
    }
138
139
    /**
140
     * @param array $params
141
     *
142
     * @return string
143
     */
144 10
    protected function generateUrl(array $params): string
145
    {
146 10
        $url = rtrim($this->url);
147
148 10
        if (is_numeric($id = $params['id'] ?? $this->id)) {
149 1
            $url = "{$url}/id/{$id}";
150 9
        } elseif ($seed = $params['seed'] ?? $this->seed) {
151 1
            $url = "{$url}/seed/{$seed}";
152
        }
153
154 10
        $width  = $params['width']  ?? $this->width;
155 10
        $height = $params['height'] ?? $this->height;
156 10
        $url    = "{$url}/{$width}/{$height}";
157
158 10
        if ($extension = $params['extension'] ?? $this->extension) {
159 1
            $url = "{$url}.{$extension}";
160
        }
161
162 10
        $query = [];
163
164 10
        if ($params['grayscale'] ?? $this->grayscale) {
165 1
            $query['grayscale'] = 1;
166
        }
167
168 10
        if ($blur = $params['blur'] ?? $this->blur) {
169 2
            $query['blur'] = (int) $blur;
170
        }
171
172 10
        if ($random = $params['random'] ?? $this->random) {
173 1
            $query['random'] = $random === true ? uniqid() : $random;
174
        }
175
176 10
        if (!empty($query)) {
177 4
            $url .= '?' . http_build_query($query);
178
        }
179
180 10
        return $url;
181
    }
182
183
    /**
184
     * @return static
185
     */
186 1
    public function jpg(): static
187
    {
188 1
        return $this->extension(__FUNCTION__);
189
    }
190
191
    /**
192
     * @return static
193
     */
194 1
    public function webp(): static
195
    {
196 1
        return $this->extension(__FUNCTION__);
197
    }
198
199
    /**
200
     * @param int|bool $id
201
     *
202
     * @return static
203
     */
204 1
    public function id(int|bool $id): static
205
    {
206 1
        $this->id = $id;
207
208 1
        return $this;
209
    }
210
211
    /**
212
     * @param string|bool $seed
213
     *
214
     * @return static
215
     */
216 1
    public function seed(string|bool $seed): static
217
    {
218 1
        $this->seed = $seed;
219
220 1
        return $this;
221
    }
222
223
    /**
224
     * @param bool $grayscale
225
     *
226
     * @return static
227
     */
228 1
    public function grayscale(bool $grayscale = true): static
229
    {
230 1
        $this->grayscale = $grayscale;
231
232 1
        return $this;
233
    }
234
235
    /**
236
     * @param bool|int $blur
237
     *
238
     * @return static
239
     */
240 1
    public function blur(bool|int $blur = true): static
241
    {
242 1
        $this->blur = $blur;
243
244 1
        return $this;
245
    }
246
247
    /**
248
     * @param bool|int $random
249
     *
250
     * @return static
251
     */
252 1
    public function random(bool|int $random = true): static
253
    {
254 1
        $this->random = $random;
255
256 1
        return $this;
257
    }
258
259
    /**
260
     * @param string $type
261
     *
262
     * @return static
263
     */
264 1
    public function type(string $type): static
265
    {
266 1
        if (!isset($this->types[ $type ]) || !is_array($data = $this->types[ $type ])) {
267 1
            throw new \InvalidArgumentException("Type [{$type}] not supported.");
268
        }
269
270
        foreach (
271
            [
272 1
                'width',
273
                'height',
274
                'extension',
275
                'grayscale',
276
                'blur',
277
                'random',
278
                'seed',
279
                'id',
280
            ] as $key
281
        ) {
282 1
            if (array_key_exists($key, $data)) {
283 1
                $this->$key = $data[ $key ];
284
            }
285
        }
286
287
288 1
        return $this;
289
    }
290
}
291