Completed
Push — master ( 4b253c...0bc4e9 )
by Vasyl
01:52
created

HasSlugMaker::otherRecordExistsWithSlug()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
3
namespace Fomvasss\SlugMaker;
4
5
use Fomvasss\SlugMaker\ServiceTraits\SlugGenerator;
6
use Fomvasss\SlugMaker\ServiceTraits\SlugScopes;
7
use Illuminate\Database\Eloquent\Model;
0 ignored issues
show
Bug introduced by
The type Illuminate\Database\Eloquent\Model was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
8
9
trait HasSlugMaker
10
{
11
    use SlugScopes, SlugGenerator;
12
13
    protected $slugOptions;
14
15
    /**
16
     * The morph relations (has one) for the model.
17
     *
18
     * @return mixed
19
     */
20
    public function slug()
21
    {
22
        return $this->morphOne(\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

22
        return $this->/** @scrutinizer ignore-call */ morphOne(\Fomvasss\SlugMaker\Models\Slug::class, 'slugable');
Loading history...
23
    }
24
25
    /**
26
     * Get the options for generating the slug.
27
     */
28
    abstract public function getSlugMakerOptions(): SlugMakerOptions;
29
30
    /**
31
     * The boot trait events in class (model).
32
     */
33
    protected static function bootHasSlugMaker()
34
    {
35
        static::created(function (Model $model) {
36
            $model->generateSlugOnCreate();
37
        });
38
39
        static::updated(function (Model $model) {
40
            $model->generateSlugOnUpdate();
41
        });
42
43
        static::deleted(function (Model $model) {
44
            $model->removeSlugsOnDeleted();
45
        });
46
    }
47
48
    /**
49
     * Handle adding slug on model creation.
50
     */
51
    protected function generateSlugOnCreate()
52
    {
53
        $this->slugOptions = $this->getSlugMakerOptions();
54
55
        if (! $this->slugOptions->generateSlugsOnCreate) {
56
            return;
57
        }
58
59
        $slug = $this->getSlug();
60
        $this->storeSlug($slug);
61
    }
62
63
    /**
64
     * Handle adding slug on model update.
65
     */
66
    protected function generateSlugOnUpdate()
67
    {
68
        $this->slugOptions = $this->getSlugMakerOptions();
69
        $this->currentSlugModel = $this->slug;
0 ignored issues
show
Bug Best Practice introduced by
The property slug does not exist on Fomvasss\SlugMaker\HasSlugMaker. Did you maybe forget to declare it?
Loading history...
Bug Best Practice introduced by
The property currentSlugModel does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
70
71
        if (! $this->slugOptions->generateSlugsOnUpdate) {
72
            return;
73
        }
74
75
        $slug = $this->getSlug();
76
        $this->storeSlug($slug);
77
    }
78
79
    /**
80
     * The store (create or update) slug in database.
81
     *
82
     * @param $slug
83
     */
84
    protected function storeSlug($slug)
85
    {
86
        if ($this->slug) {
0 ignored issues
show
Bug Best Practice introduced by
The property slug does not exist on Fomvasss\SlugMaker\HasSlugMaker. Did you maybe forget to declare it?
Loading history...
87
            $this->slug()->update(['name' => $slug]);
88
        } else {
89
            $this->slug()->create(['name' => $slug]);
90
        }
91
    }
92
93
    /**
94
     * Remove the slug in database. Default migration not using softdelete.
95
     */
96
    protected function removeSlugsOnDeleted()
97
    {
98
        $this->slugOptions = $this->getSlugMakerOptions();
99
100
        if ($this->slug && $this->slugOptions->removeSlugsOnDelete) {
0 ignored issues
show
Bug Best Practice introduced by
The property slug does not exist on Fomvasss\SlugMaker\HasSlugMaker. Did you maybe forget to declare it?
Loading history...
101
            $this->slug()->delete();
102
        }
103
    }
104
}
105