Shortener::shorten()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.9
c 0
b 0
f 0
cc 1
nc 1
nop 2
1
<?php
2
3
namespace danielebuso\shortener;
4
5
use danielebuso\shortener\Models\ShortLink;
6
use Illuminate\Support\Str;
7
8
class Shortener
9
{
10
11
    /**
12
     *  Parse a rules array.
13
     *
14
     * @param string $url
15
     * @param array $opt = [
16
     *  'length' => 8, // Link length
17
     *  'expire_at' => null, // Link expiry date
18
     *  'not_before' => null, // Link validity start date
19
     * ]
20
     * @return ShortLink
21
     */
22
    public static function shorten(string $url, array $opt = [])
23
    {
24
        $link_length = $opt['length'] ?? config('shortener.link_length') ?? 8;
25
26
        return ShortLink::create([
27
            'id' => Str::random($link_length),
28
            'url' => $url,
29
            'expire_at' => $opt['expire_at'] ?? null,
30
            'not_before' => $opt['not_before'] ?? null,
31
        ]);
32
    }
33
34
}
35