Link::getCode()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 10
c 0
b 0
f 0
ccs 4
cts 4
cp 1
cc 2
nc 2
nop 0
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace MuhamedDidovic\Shortener\Models;
6
7
use Illuminate\Database\Eloquent\Model;
8
use MuhamedDidovic\Shortener\Exceptions\CodeGenerationException;
9
use MuhamedDidovic\Shortener\Facades\Shortener;
10
use MuhamedDidovic\Shortener\Traits\Eloquent\TouchesTimestamps;
11
12
/**
13
 * Class Link.
14
 */
15
class Link extends Model
16
{
17
    use TouchesTimestamps;
18
19
    /**
20
     * @var array
21
     */
22
    protected $fillable = [
23
        'original_url',
24
        'code',
25
        'requested_count',
26
        'used_count',
27
        'last_requested',
28
        'last_used',
29
    ];
30
31
    /**
32
     * @var array
33
     */
34
    protected $dates = [
35
        'last_requested',
36
        'last_used',
37
    ];
38
39
    /**
40
     * Override or change name of the table from config file.
41
     * @return \Illuminate\Config\Repository|mixed|string
42
     */
43 30
    public function getTable()
44
    {
45 30
        return config('shortener.table');
46
    }
47
48
    /**
49
     * Get the code from the ID of table.
50
     * @return mixed|string
51
     * @throws CodeGenerationException
52
     */
53 19
    public function getCode()
54
    {
55 19
        if ($this->id === null) {
56 1
            throw new CodeGenerationException;
57
        }
58
59 18
        return Shortener::toBase($this->id);
60
    }
61
62
    /**
63
     * Get resource by code.
64
     * @param $code
65
     * @return mixed
66
     */
67 7
    public static function byCode($code)
68
    {
69 7
        return static::where('code', $code);
70
    }
71
72
    /**
73
     * Get full new shortened URL with code.
74
     * @return string|null
75
     */
76 11
    public function shortenedUrl()
77
    {
78 11
        if ($this->code === null) {
79 3
            return;
80
        }
81
82 8
        return config('shortener.url').'/'.$this->code;
83
    }
84
}
85