Passed
Push — master ( 0dde30...57f1fc )
by Jim
03:20
created

Route::routable()   A

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 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
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