Route   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 102
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 22
dl 0
loc 102
ccs 29
cts 29
cp 1
rs 10
c 1
b 0
f 0
wmc 16

10 Methods

Rating   Name   Duplication   Size   Complexity  
A redirect() 0 3 1
A canonical() 0 3 1
A isCanonical() 0 3 1
A getRedirectUrlAttribute() 0 11 5
A scopeIsRedirect() 0 6 1
A scopeWhereIsCanonical() 0 3 1
A routable() 0 3 1
A setUrlAttribute() 0 3 1
A parseUrl() 0 3 1
A shouldRedirect() 0 3 3
1
<?php
2
3
namespace Oddvalue\DbRouter;
4
5
use Illuminate\Database\Eloquent\Model;
6
use Illuminate\Database\Eloquent\SoftDeletes;
7
use Oddvalue\DbRouter\Exceptions\NoRedirectUrlException;
8
9
class Route extends Model
10
{
11
    use SoftDeletes;
12
13
    /*
14
    |--------------------------------------------------------------------------
15
    | GLOBAL VARIABLES
16
    |--------------------------------------------------------------------------
17
    */
18
    protected $fillable = [
19
        'url',
20
        'canonical_id',
21
    ];
22
23
    /*
24
    |--------------------------------------------------------------------------
25
    | FUNCTIONS
26
    |--------------------------------------------------------------------------
27
    */
28
29 9
    public function shouldRedirect() : bool
30
    {
31 9
        return $this->redirect || ($this->trashed() && $this->routable);
32
    }
33
34 6
    public function isCanonical() : bool
35
    {
36 6
        return $this->canonical_id !== null;
37
    }
38
39 36
    public function parseUrl(string $url)
40
    {
41 36
        return str_replace(url(''), '', $url);
42
    }
43
44
    /*
45
    |--------------------------------------------------------------------------
46
    | RELATIONS
47
    |--------------------------------------------------------------------------
48
    */
49 36
    public function routable()
50
    {
51 36
        return $this->morphTo();
52
    }
53
54 3
    public function canonical()
55
    {
56 3
        return $this->belongsTo(self::class);
57
    }
58
59 15
    public function redirect()
60
    {
61 15
        return $this->belongsTo(self::class);
62
    }
63
64
    /*
65
    |--------------------------------------------------------------------------
66
    | SCOPES
67
    |--------------------------------------------------------------------------
68
    */
69
70 18
    public function scopeWhereIsCanonical($query)
71
    {
72 18
        $query->whereNull('canonical_id');
73 18
    }
74
75 3
    public function scopeIsRedirect($query)
76
    {
77
        $query->where(function ($query) {
78 3
            $query->onlyTrashed();
79 3
            $query->orWhereNotNull('redirect_id');
80 3
        })->withTrashed();
81 3
    }
82
83
    /*
84
    |--------------------------------------------------------------------------
85
    | MUTATORS
86
    |--------------------------------------------------------------------------
87
    */
88
89 36
    public function setUrlAttribute(string $value)
90
    {
91 36
        $this->attributes['url'] = $this->parseUrl($value);
92 36
    }
93
94
    /*
95
    |--------------------------------------------------------------------------
96
    | ACCESSORS
97
    |--------------------------------------------------------------------------
98
    */
99
100 9
    public function getRedirectUrlAttribute()
101
    {
102 9
        if ($this->routable && $this->trashed() && $this->routable->canonicalRoute) {
103 3
            return $this->routable->canonicalRoute->url;
104
        }
105
106 6
        if ($this->redirect) {
107 3
            return $this->redirect->url;
108
        }
109
110 3
        throw NoRedirectUrlException::forRoute($this);
111
    }
112
}
113