Url::scopeWhereUrl()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
rs 10
cc 1
nc 1
nop 2
1
<?php
2
3
namespace Neurony\Url\Models;
4
5
use Illuminate\Database\Eloquent\Builder;
6
use Illuminate\Database\Eloquent\Model;
7
use Illuminate\Database\Eloquent\ModelNotFoundException;
8
use Illuminate\Database\Eloquent\Relations\MorphTo;
9
use Illuminate\Support\Facades\Request;
10
use Neurony\Url\Contracts\UrlModelContract;
11
12
class Url extends Model implements UrlModelContract
13
{
14
    /**
15
     * The database table.
16
     *
17
     * @var string
18
     */
19
    protected $table = 'urls';
20
21
    /**
22
     * The attributes that are mass assignable.
23
     *
24
     * @var array
25
     */
26
    protected $fillable = [
27
        'url',
28
        'urlable_id',
29
        'urlable_type',
30
    ];
31
32
    /**
33
     * Get all of the owning urlable models.
34
     *
35
     * @return MorphTo
36
     */
37
    public function urlable() : MorphTo
38
    {
39
        return $this->morphTo();
40
    }
41
42
    /**
43
     * Filter the query by url.
44
     *
45
     * @param Builder $query
46
     * @param string $url
47
     */
48
    public function scopeWhereUrl(Builder $query, string $url)
49
    {
50
        $query->where('url', $url);
51
    }
52
53
    /**
54
     * Filter the query by the urlable morph relation.
55
     *
56
     * @param Builder $query
57
     * @param int $id
58
     * @param string $type
59
     */
60
    public function scopeWhereUrlable(Builder $query, int $id, string $type)
61
    {
62
        $query->where([
63
            'urlable_id' => $id,
64
            'urlable_type' => $type,
65
        ]);
66
    }
67
68
    /**
69
     * Sort the query alphabetically by url.
70
     *
71
     * @param Builder $query
72
     */
73
    public function scopeInAlphabeticalOrder(Builder $query)
74
    {
75
        $query->orderBy('url', 'asc');
0 ignored issues
show
Bug introduced by
The method orderBy() does not exist on Illuminate\Database\Eloquent\Builder. Did you maybe mean enforceOrderBy()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
76
    }
77
78
    /**
79
     * Get the model instance correlated with the accessed url.
80
     *
81
     * @param bool $silent
82
     * @return Model|null
83
     * @throws ModelNotFoundException
84
     */
85
    public static function getUrlable(bool $silent = true): ?Model
86
    {
87
        $model = Request::route()->action['model'] ?? null;
88
89
        if ($model && $model instanceof Model && $model->exists) {
90
            return $model;
91
        }
92
93
        if ($silent === false) {
94
            throw new ModelNotFoundException;
95
        }
96
    }
97
98
    /**
99
     * Get the model instance correlated with the accessed url.
100
     * Throw a ModelNotFoundException if the model doesn't exist.
101
     *
102
     * @return Model|null
103
     * @throws ModelNotFoundException
104
     */
105
    public static function getUrlableOrFail(): ?Model
106
    {
107
        return static::getUrlable(false);
108
    }
109
}
110