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

SlugHelper::makeForModel()   B

Complexity

Conditions 5
Paths 8

Size

Total Lines 19
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 10
nc 8
nop 2
dl 0
loc 19
rs 8.8571
c 0
b 0
f 0
1
<?php
2
3
namespace Fomvasss\SlugMaker;
4
5
/**
6
 * Class SlugGenerator
7
 *
8
 * @package \Fomvasss\SlugGenerator
9
 */
10
class SlugHelper
11
{
12
    use SlugGenerator;
13
14
    protected $slugModelClass;
15
16
    protected $currentSlugModel = null;
17
18
    protected $slugConfig = [
19
        'separator' => '-',
20
        'prefix' => '',
21
        'suffix' => '',
22
        'maximum_length' => 190,
23
        'model' => \Fomvasss\SlugGenerator\Models\Slug::class,
0 ignored issues
show
Bug introduced by
The type Fomvasss\SlugGenerator\Models\Slug 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...
24
    ];
25
26
    /**
27
     * SlugHelper constructor.
28
     */
29
    public function __construct()
30
    {
31
        //$this->slugConfig = array_merge($this->slugConfig, config('slugmaker', []));
32
        $this->slugConfig = config('slugmaker');
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

32
        $this->slugConfig = /** @scrutinizer ignore-call */ config('slugmaker');
Loading history...
33
        $this->slugModelClass = app()->make($this->slugConfig['model']);
0 ignored issues
show
Bug introduced by
The function app 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->slugModelClass = /** @scrutinizer ignore-call */ app()->make($this->slugConfig['model']);
Loading history...
34
    }
35
36
    /**
37
     * @param $slug
38
     * @param null $modelClass
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $modelClass is correct as it would always require null to be passed?
Loading history...
39
     * @return null
40
     */
41
    public function getModel($slug, $modelClass = null)
42
    {
43
        $slug = $this->slugModelClass
44
            ->byNameByClass($slug, $modelClass)
45
            ->first();
46
47
        return $slug ? $slug->slugable : null;
48
    }
49
50
    /**
51
     * @param array $slugs
52
     * @param null $modelClass
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $modelClass is correct as it would always require null to be passed?
Loading history...
53
     * @return mixed
54
     */
55
    public function getModels(array $slugs, $modelClass = null)
56
    {
57
        $slugs = $this->slugModelClass
58
            ->with('slugable')
59
            ->byNamesByClass($slugs, $modelClass)
60
            ->get();
61
62
        return $slugs->map(function ($item) {
63
            return $item->slugable;
64
        });
65
    }
66
67
    /**
68
     * @param $slug
69
     * @param null $modelClass
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $modelClass is correct as it would always require null to be passed?
Loading history...
70
     * @return null
71
     */
72
    public function getId($slug, $modelClass = null)
73
    {
74
        $model = $this->getModel($slug, $modelClass);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $model is correct as $this->getModel($slug, $modelClass) targeting Fomvasss\SlugMaker\SlugHelper::getModel() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
75
        return  $model ? $model->id : null;
76
    }
77
78
    /**
79
     * @param array $slugs
80
     * @param null $modelClass
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $modelClass is correct as it would always require null to be passed?
Loading history...
81
     * @return mixed
82
     */
83
    public function getIds(array $slugs, $modelClass = null)
84
    {
85
        return $this->getModels($slugs, $modelClass)
86
            ->pluck('id')
87
            ->toArray();
88
    }
89
90
    /**
91
     * @param array $attributes
92
     * @param bool $useId
93
     * @return array
94
     */
95
    public function getIdsGroupedByClass(array $attributes, $useId = false)
96
    {
97
        $key = $useId ? 'slugable_id' : 'name';
98
99
        $slugs = $this->slugModelClass
100
            ->byClassesByNames($attributes, $key)
101
            ->get();
102
103
        return $this->groupedByClass($slugs
104
            ->pluck('slugable_type', 'slugable_id')
105
            ->toArray());
106
    }
107
108
    /**
109
     * @param $model
110
     * @param $slug
111
     * @return mixed
112
     */
113
    public function makeForModel($model, $slug = '')
114
    {
115
        $this->currentSlugModel = $model->slug;
116
        $slug = empty($slug) ? $this->getStrSlugByModelFields($model) : $slug;
117
118
        $newSlug = $this->getSlug($slug);
119
120
        if ($model->slug) {
121
            if ($this->slugConfig['generate_on_update']) {
122
                $model->slug()->update(['name' => $newSlug]);
123
            }
124
            return $newSlug;
125
        }
126
127
        if ($this->slugConfig['generate_on_create']) {
128
            $model->slug()->create(['name' => $newSlug]);
129
        }
130
131
        return $newSlug;
132
    }
133
134
    /**
135
     * @param $model
136
     * @return string
137
     */
138
    protected function getStrSlugByModelFields($model)
139
    {
140
        $str = '';
141
        foreach ($model->getSlugSourceFields() as $field) {
142
            $str .= $model->{$field}.'-';
143
        }
144
145
        return trim($str, '-');
146
    }
147
148
    /**
149
     * @param $model
150
     */
151
    public function deleteByModel($model)
152
    {
153
        if ($model->slug) {
154
            return $model->slug()->delete();
155
        }
156
157
        return;
158
    }
159
160
    /**
161
     * @param $attributes
162
     * @return array
163
     */
164
    private function groupedByClass($attributes): array
165
    {
166
        $res = [];
167
        foreach ($attributes as $id => $type) {
168
            $res[$type][] = $id;
169
        }
170
171
        return $res;
172
    }
173
}
174