Completed
Push — master ( f28d10...67914a )
by Vasyl
02:01
created

ModelHasSlug::getSlugSourceFields()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 2
nc 2
nop 0
dl 0
loc 5
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 mixed
11
     */
12
    public function slug()
13
    {
14
        return $this->morphOne(config('slugmaker.model', \Fomvasss\SlugMaker\Models\Slug::class), 'slugable');
0 ignored issues
show
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

14
        return $this->/** @scrutinizer ignore-call */ morphOne(config('slugmaker.model', \Fomvasss\SlugMaker\Models\Slug::class), 'slugable');
Loading history...
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
        return $this->morphOne(/** @scrutinizer ignore-call */ config('slugmaker.model', \Fomvasss\SlugMaker\Models\Slug::class), 'slugable');
Loading history...
15
    }
16
17
    /**
18
     * The scope for fet slug name.
19
     */
20
    public function getSlugName()
21
    {
22
        if ($this->slug) {
0 ignored issues
show
Bug Best Practice introduced by
The property slug does not exist on Fomvasss\SlugMaker\ModelHasSlug. Did you maybe forget to declare it?
Loading history...
23
            return $this->slug->name;
24
        }
25
        return;
26
    }
27
28
    /**
29
     * @param $slug
30
     * @return mixed
31
     */
32
    public function makeSlug($slug = '')
33
    {
34
        return slug_make($this, $slug);
35
    }
36
37
    /**
38
     * Models by slugs.
39
     *
40
     * @param $query
41
     * @param string $slug
42
     * @return mixed
43
     */
44
    public function scopeBySlugs($query, $slug)
45
    {
46
        $slugs = is_array($slug) ? $slug : [$slug];
47
48
        return $query->whereHas('slug', function ($q) use ($slugs) {
49
            $q->whereIn('name', $slugs);
50
        });
51
    }
52
53
    /**
54
     * Find first model by slug.
55
     *
56
     * @param $query
57
     * @param string $slug
58
     * @return mixed
59
     */
60
    public function scopeFindBySlug($query, string $slug)
61
    {
62
        return $this->scopeBySlugs($query, $slug)->first();
63
    }
64
65
    /**
66
     * Find first model by slug or throw exciption.
67
     *
68
     * @param $query
69
     * @param string $slug
70
     * @return mixed
71
     */
72
    public function scopeFindOrFailBySlug($query, string $slug)
73
    {
74
        return $this->scopeBySlugs($query, $slug)->firstOrFail();
75
    }
76
77
    /**
78
     * Get models by slugs.
79
     *
80
     * @param $query
81
     * @param $slugs
82
     * @return mixed
83
     */
84
    public function scopeGetBySlugs($query, $slugs)
85
    {
86
        return $this->scopeBySlugs($query, $slugs)->get();
87
    }
88
89
    /**
90
     * Get array ids by slugs.
91
     *
92
     * @param $query
93
     * @param array $slugs
94
     * @return array
95
     */
96
    public function scopeGetArrayIdsBySlugs($query, array $slugs): array
97
    {
98
        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

98
        return $this->scopeBySlugs($query, /** @scrutinizer ignore-type */ $slugs)->pluck('id')->toArray();
Loading history...
99
    }
100
101
    public function getSlugSourceFields(): array
102
    {
103
        $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

103
        $sourceFields = /** @scrutinizer ignore-call */ config('slugmaker.default_source_fields', []);
Loading history...
104
105
        return is_array($sourceFields) ? $sourceFields : [$sourceFields];
106
    }
107
}
108