ModelHasSlug::getSlugName()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 0
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Fomvasss\SlugMaker;
4
5
trait ModelHasSlug
6
{
7
    public $slugSourceFields;
8
9
    /**
10
     * @return array
11
     */
12
    public function getSlugSourceFields(): array
13
    {
14
        $sourceFields = config('slugmaker.default_source_fields', []);
0 ignored issues
show
Bug introduced by
The function config was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

14
        $sourceFields = /** @scrutinizer ignore-call */ config('slugmaker.default_source_fields', []);
Loading history...
15
16
        return is_array($sourceFields) ? $sourceFields : [$sourceFields];
17
    }
18
19
    /**
20
     * @return mixed
21
     */
22
    public function slug()
23
    {
24
        return $this->morphOne(config('slugmaker.model', \Fomvasss\SlugMaker\Models\Slug::class), 'slugable');
0 ignored issues
show
Bug introduced by
The function config was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

24
        return $this->morphOne(/** @scrutinizer ignore-call */ config('slugmaker.model', \Fomvasss\SlugMaker\Models\Slug::class), 'slugable');
Loading history...
Bug introduced by
It seems like morphOne() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

24
        return $this->/** @scrutinizer ignore-call */ morphOne(config('slugmaker.model', \Fomvasss\SlugMaker\Models\Slug::class), 'slugable');
Loading history...
25
    }
26
27
    /**
28
     * The scope for fet slug name.
29
     */
30
    public function getSlugName()
31
    {
32
        if ($this->slug) {
33
            return $this->slug->name;
34
        }
35
        return;
36
    }
37
38
    /**
39
     * @param $slug
40
     * @return mixed
41
     */
42
    public function makeSlug($slug = '')
43
    {
44
        return slug_make($this, $slug);
45
    }
46
47
    /**
48
     * Models by slugs.
49
     *
50
     * @param $query
51
     * @param string $slug
52
     * @return mixed
53
     */
54
    public function scopeBySlugs($query, $slug)
55
    {
56
        $slugs = is_array($slug) ? $slug : [$slug];
0 ignored issues
show
introduced by
The condition is_array($slug) is always false.
Loading history...
57
58
        return $query->whereHas('slug', function ($q) use ($slugs) {
59
            $q->whereIn('name', $slugs);
60
        });
61
    }
62
63
    /**
64
     * Find first model by slug.
65
     *
66
     * @param $query
67
     * @param string $slug
68
     * @return mixed
69
     */
70
    public function scopeFindBySlug($query, string $slug)
71
    {
72
        return $this->scopeBySlugs($query, $slug)->first();
73
    }
74
75
    /**
76
     * Find first model by slug or throw exciption.
77
     *
78
     * @param $query
79
     * @param string $slug
80
     * @return mixed
81
     */
82
    public function scopeFindOrFailBySlug($query, string $slug)
83
    {
84
        return $this->scopeBySlugs($query, $slug)->firstOrFail();
85
    }
86
87
    /**
88
     * Get models by slugs.
89
     *
90
     * @param $query
91
     * @param $slugs
92
     * @return mixed
93
     */
94
    public function scopeGetBySlugs($query, $slugs)
95
    {
96
        return $this->scopeBySlugs($query, $slugs)->get();
97
    }
98
99
    /**
100
     * Get array ids by slugs.
101
     *
102
     * @param $query
103
     * @param array $slugs
104
     * @return array
105
     */
106
    public function scopeGetArrayIdsBySlugs($query, array $slugs): array
107
    {
108
        return $this->scopeBySlugs($query, $slugs)->pluck('id')->toArray();
0 ignored issues
show
Bug introduced by
$slugs of type array is incompatible with the type string expected by parameter $slug of Fomvasss\SlugMaker\ModelHasSlug::scopeBySlugs(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

108
        return $this->scopeBySlugs($query, /** @scrutinizer ignore-type */ $slugs)->pluck('id')->toArray();
Loading history...
109
    }
110
}
111