GetLink   A
last analyzed

Complexity

Total Complexity 25

Size/Duplication

Total Lines 171
Duplicated Lines 0 %

Importance

Changes 6
Bugs 1 Features 2
Metric Value
eloc 42
c 6
b 1
f 2
dl 0
loc 171
rs 10
wmc 25

12 Methods

Rating   Name   Duplication   Size   Complexity  
A getLink() 0 4 1
A getLinkWithParams() 0 3 1
A getFirstCostValueAttribute() 0 7 3
A linkParam() 0 4 1
A linkQuery() 0 3 1
A getFirstCostValueTitleAttribute() 0 9 3
A getCostLink() 0 18 4
A getFirstCostValueIdAttribute() 0 9 3
A getCostValuesAttribute() 0 17 5
A link() 0 3 1
A linkAttribute() 0 5 1
A getAllLinks() 0 3 1
1
<?php
2
3
namespace Larrock\Core\Traits;
4
5
use Cache;
6
use Larrock\Core\Models\Link;
7
8
trait GetLink
9
{
10
    /**
11
     * Get link query builder.
12
     * @param $childModel
13
     * @return mixed
14
     */
15
    public function linkQuery($childModel)
16
    {
17
        return $this->hasMany(Link::class, 'id_parent')->whereModelParent($this->config->model)->whereModelChild($childModel);
0 ignored issues
show
Bug introduced by
It seems like hasMany() 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

17
        return $this->/** @scrutinizer ignore-call */ hasMany(Link::class, 'id_parent')->whereModelParent($this->config->model)->whereModelChild($childModel);
Loading history...
18
    }
19
20
    /**
21
     * Get link data.
22
     * @param $childModel
23
     * @return mixed
24
     */
25
    public function link($childModel)
26
    {
27
        return $this->hasMany(Link::class, 'id_parent')->whereModelParent($this->config->model)->whereModelChild($childModel)->get();
28
    }
29
30
    /**
31
     * Get link data.
32
     * @return mixed
33
     */
34
    public function linkParam()
35
    {
36
        return $this->hasMany(Link::class, 'id_parent')->whereModelParent($this->config->model)
37
            ->whereModelChild('Larrock\ComponentCatalog\Models\Param');
38
    }
39
40
    /**
41
     * Get Model Component + link in attrubute.
42
     * @param $childModel
43
     * @return $this
44
     */
45
    public function linkAttribute($childModel)
46
    {
47
        $this->{$childModel} = $this->hasMany(Link::class, 'id_parent')->whereModelParent($this->config->model)->whereModelChild($childModel)->get();
48
49
        return $this;
50
    }
51
52
    /**
53
     * Метод для attach() и detach().
54
     * @param string $childModel
55
     * @return mixed
56
     */
57
    public function getLink($childModel)
58
    {
59
        return $this->belongsToMany($childModel, 'link', 'id_parent', 'id_child')
0 ignored issues
show
Bug introduced by
It seems like belongsToMany() 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

59
        return $this->/** @scrutinizer ignore-call */ belongsToMany($childModel, 'link', 'id_parent', 'id_child')
Loading history...
60
            ->whereModelParent($this->config->getModelName())->whereModelChild($childModel);
61
    }
62
63
    /**
64
     * Метод для совместимости привязки сторонних расширений (роли у пользователей)
65
     * Например: $data->getLinkWithParams($row_settings->modelChild, 'role_user', 'role_id', 'user_id')->get()
66
     * для получения прилинкованных ролей пользователя.
67
     * @param $childModel
68
     * @param $tableLink
69
     * @param $parentRow
70
     * @param $childRow
71
     * @return mixed
72
     */
73
    public function getLinkWithParams($childModel, $tableLink, $parentRow, $childRow)
74
    {
75
        return $this->belongsToMany($childModel, $tableLink, $parentRow, $childRow);
76
    }
77
78
    /**
79
     * Получение всех связей.
80
     * @return mixed
81
     */
82
    public function getAllLinks()
83
    {
84
        return $this->hasMany(Link::class, 'id_parent')->whereModelParent($this->config->model);
85
    }
86
87
    /**
88
     * Получение связи модификации цены.
89
     * @param $childModel
90
     * @return null
91
     */
92
    public function getCostLink($childModel)
93
    {
94
        $cache_key = sha1('getCostLink'.$this->config->model.$childModel.$this->id);
95
96
        return Cache::rememberForever($cache_key, function () use ($childModel) {
97
            $query = $this->hasMany(Link::class, 'id_parent')->whereModelParent($this->config->model)->whereModelChild($childModel);
98
            if ($getLink = $query->get()) {
99
                foreach ($getLink as $key => $item) {
100
                    $class = new $childModel();
101
                    if ($param = $class->whereId($item->id_child)->first()) {
102
                        $getLink[$key]->title = $param->title;
103
                    }
104
                }
105
106
                return $getLink;
107
            }
108
109
            return null;
110
        });
111
    }
112
113
    /**
114
     * Получение модификаций товаров
115
     * Товар->cost_value.
116
     * @return mixed|null
117
     */
118
    public function getCostValuesAttribute()
119
    {
120
        $cache_key = sha1('cost_value'.$this->id.$this->getConfig()->name);
0 ignored issues
show
Bug introduced by
It seems like getConfig() 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

120
        $cache_key = sha1('cost_value'.$this->id.$this->/** @scrutinizer ignore-call */ getConfig()->name);
Loading history...
121
122
        return Cache::rememberForever($cache_key, function () {
123
            $values = null;
124
            foreach ($this->config->rows as $row) {
125
                if (isset($row->costValue) && $row->costValue) {
126
                    if (null === $values) {
127
                        $values = $this->getCostLink($row->modelChild, $row->modelChildWhereKey, $row->modelChildWhereValue);
0 ignored issues
show
Unused Code introduced by
The call to Larrock\Core\Traits\GetLink::getCostLink() has too many arguments starting with $row->modelChildWhereKey. ( Ignorable by Annotation )

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

127
                        /** @scrutinizer ignore-call */ 
128
                        $values = $this->getCostLink($row->modelChild, $row->modelChildWhereKey, $row->modelChildWhereValue);

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...
128
                    } else {
129
                        $values = $values->merge($this->getCostLink($row->modelChild, $row->modelChildWhereKey, $row->modelChildWhereValue));
130
                    }
131
                }
132
            }
133
134
            return $values;
135
        });
136
    }
137
138
    /**
139
     * Получение первой цены модификации товара.
140
     * @return mixed
141
     */
142
    public function getFirstCostValueAttribute()
143
    {
144
        if (($costValues = $this->getCostValuesAttribute()) && $costValues->count() > 0) {
145
            return $costValues->first()->cost;
146
        }
147
148
        return $this->cost;
149
    }
150
151
    /**
152
     * Получение ID первой цены модификации товара.
153
     * @return mixed
154
     */
155
    public function getFirstCostValueIdAttribute()
156
    {
157
        if ($costValues = $this->getCostValuesAttribute()) {
158
            if ($costValues->count() > 0) {
159
                return $costValues->first()->id;
160
            }
161
        }
162
163
        return null;
164
    }
165
166
    /**
167
     * Получение title первой цены модификации товара.
168
     * @return mixed
169
     */
170
    public function getFirstCostValueTitleAttribute()
171
    {
172
        if ($costValues = $this->getCostValuesAttribute()) {
173
            if ($costValues->count() > 0) {
174
                return $costValues->first()->title;
175
            }
176
        }
177
178
        return null;
179
    }
180
}
181