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

SlugHelper::getSlugSourceString()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Fomvasss\SlugMaker;
4
5
use Fomvasss\SlugMaker\Models\Slug;
6
7
/**
8
 * Class SlugGenerator
9
 *
10
 * @package \Fomvasss\SlugMaker
11
 */
12
class SlugHelper
13
{
14
    use \Fomvasss\SlugMaker\ServiceTraits\SlugGenerator;
15
16
    protected $slugOptions;
17
18
    protected $slugModel;
19
20
    protected $sourceString;
21
22
    protected $slugIsId = false;
23
24
    /**
25
     * SlugHelper constructor.
26
     *
27
     * @param \Fomvasss\SlugMaker\Models\Slug $slugModel
28
     */
29
    public function __construct(Slug $slugModel)
30
    {
31
        $this->slugModel = $slugModel;
32
        $this->slugOptions = $this->getSlugMakerOptions();
33
        $this->slugIsId = config('slugmaker.slug_is_id', false);
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

33
        $this->slugIsId = /** @scrutinizer ignore-call */ config('slugmaker.slug_is_id', false);
Loading history...
34
    }
35
36
    public function getModel($slug, $modelClass = null)
37
    {
38
        $slug = $this->slugModel
39
            ->byNameByClass($slug, $modelClass)
40
            ->first();
41
42
        return $slug ? $slug->slugable : null;
43
    }
44
45
    public function getModels(array $slugs, $modelClass = null)
46
    {
47
        $slugs = $this->slugModel
48
            ->with('slugable')
49
            ->byNamesByClass($slugs, $modelClass)
50
            ->get();
51
52
        return $slugs->map(function ($item) {
53
            return $item->slugable;
54
        });
55
    }
56
57
    public function getId($slug, $modelClass = null)
58
    {
59
        if ($this->slugIsId) {
60
            return $slug;
61
        }
62
        $model = $this->getModel($slug, $modelClass);
63
        return  $model ? $model->id : null;
64
    }
65
66
    public function getIds(array $slugs, $modelClass = null)
67
    {
68
        if ($this->slugIsId) {
69
            return $slugs;
70
        }
71
        return $this->getModels($slugs, $modelClass)
72
            ->pluck('id')
73
            ->toArray();
74
    }
75
76
    public function getIdsGroupedByClass(array $attributes, $useId = false)
77
    {
78
        $key = $useId ? 'slugable_id' : 'name';
79
80
        $slugs = $this->slugModel
81
            ->byClassesByNames($attributes, $key)
82
            ->get();
83
84
        return $this->groupedByClass($slugs
85
            ->pluck('slugable_type', 'slugable_id')
86
            ->toArray());
87
    }
88
89
    public function makeForModel($model, $slug)
90
    {
91
        $this->currentSlugModel = $model->slug;
92
93
        $newSlug = $this->getSlug($this->sourceString = $slug);
0 ignored issues
show
Unused Code introduced by
The call to Fomvasss\SlugMaker\SlugHelper::getSlug() has too many arguments starting with $this->sourceString = $slug. ( Ignorable by Annotation )

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

93
        /** @scrutinizer ignore-call */ 
94
        $newSlug = $this->getSlug($this->sourceString = $slug);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
94
95
        if ($model->slug) {
96
            return $model->slug()->update(['name' => $newSlug]);
97
        }
98
99
        return $model->slug()->create(['name' => $newSlug]);
100
    }
101
102
    public function deleteByModel($model)
103
    {
104
        if ($model->slug) {
105
            return $model->slug()->delete();
106
        }
107
108
        return;
109
    }
110
111
    public function getSlugMakerOptions(): SlugMakerOptions
112
    {
113
        return SlugMakerOptions::create()
114
            ->slugsShouldBeNoLongerThan(250)
115
            ->usingSeparator('-');
116
    }
117
118
    protected function getSlugSourceString(): string
119
    {
120
        return $this->getClippedSlugWithPrefixSuffix($this->sourceString);
121
    }
122
123
    private function groupedByClass($attributes): array
124
    {
125
        $res = [];
126
        foreach ($attributes as $id => $type) {
127
            $res[$type][] = $id;
128
        }
129
130
        return $res;
131
    }
132
}
133