GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Test Setup Failed
Push — enhance/generation ( 245bdd )
by Alexander
13:56
created

PropertyPart   A

Complexity

Total Complexity 28

Size/Duplication

Total Lines 150
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 28
lcom 1
cbo 3
dl 0
loc 150
rs 10
c 1
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
B checkIncludeIfValue() 0 15 5
A addParam() 0 12 3
C getNextPart() 0 56 11
D appendPart() 0 45 9
1
<?php
2
3
namespace app\properties\url;
4
5
use app\models\Property;
6
use app\models\PropertyStaticValues;
7
use devgroup\TagDependencyHelper\ActiveRecordHelper;
8
use Yii;
9
use yii\caching\TagDependency;
10
use yii\helpers\ArrayHelper;
11
12
class PropertyPart extends UrlPart
13
{
14
    public $include_if_value = [];
15
    public $optional = true;
16
    public $property_id = null;
17
18
    private function checkIncludeIfValue()
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
19
    {
20
        if (is_object($this->model)) {
21
            $object_property_values = $this->model->getPropertyValuesByPropertyId($this->property_id);
22
            if (!is_object($object_property_values)) {
23
                return false;
24
            }
25
            foreach ($object_property_values->values as $val) {
26
                if (in_array($val['value'], $this->include_if_value)) {
27
                    return $val['slug'];
28
                }
29
            }
30
        }
31
        return false;
32
    }
33
34
    /**
35
     * Helper method to properly populate $this->params
36
     *
37
     * @param string $paramName
38
     * @param null $paramValue
39
     */
40
    private function addParam($paramName = "", $paramValue = null)
41
    {
42
        if (empty($paramName)) {
43
            throw new \InvalidArgumentException("Parameter name must be present");
44
        }
45
46
        if (isset($this->parameters[$paramName])) {
47
            $this->parameters[$paramName][] = $paramValue;
48
        } else {
49
            $this->parameters[$paramName] = [$paramValue];
50
        }
51
    }
52
53
    /**
54
     * @inheritdoc
55
     */
56
    public function getNextPart($full_url, $next_part, &$previous_parts)
57
    {
58
        $property = Property::findById($this->property_id);
59
        if (is_null($property)) {
60
            return false;
61
        }
62
        if ($property->has_static_values && $property->has_slugs_in_values) {
63
            $cacheTags = [];
64
            $static_values = PropertyStaticValues::getValuesForPropertyId($this->property_id);
65
            $slugs = explode('/', $next_part);
66
            $currentSlug = 0;
67
            $slugsCount = count($slugs);
68
            $appliedParts = [];
69
            foreach ($static_values as $value) {
70
                if ($slugs[$currentSlug] === $value['slug']) {
71
                    $appliedParts[] = $value['slug'];
72
                    if (isset($this->parameters['properties'][$this->property_id])) {
73
                        $this->parameters['properties'][$this->property_id][] = $value['id'];
74
                    } else {
75
                        $this->parameters = [
76
                            'properties' => [
77
                                $this->property_id => [$value['id']],
78
                            ],
79
                        ];
80
                    }
81
                    if (!empty($value['title_append'])) {
82
                        if ($value["title_prepend"] == 1) {
83
                            $this->addParam("title_prepend", $value["title_append"]);
84
                        } else {
85
                            $this->addParam("title_append", $value["title_append"]);
86
                        }
87
                    }
88
                    $cacheTags[] = ActiveRecordHelper::getObjectTag(PropertyStaticValues::className(), $value['id']);
89
                    $currentSlug++;
90
                    if ($currentSlug == $slugsCount) {
91
                        break;
92
                    }
93
                }
94
            }
95
            if ($currentSlug > 0) {
96
                $appliedPartsString = implode('/', $appliedParts);
97
                $part = new self(
98
                    [
99
                        'gathered_part' => $appliedPartsString,
100
                        'rest_part' => mb_substr($next_part, mb_strlen($appliedPartsString)),
101
                        'parameters' => $this->parameters,
102
                        'cacheTags' => $cacheTags,
103
                    ]
104
                );
105
                return $part;
106
            }
107
            return false;
108
        } else {
109
            return false;
110
        }
111
    }
112
113
    /**
114
     * @inheritdoc
115
     */
116
    public function appendPart($route, $parameters = [], &$used_params = [], &$cacheTags = [])
117
    {
118
        if (isset($parameters['properties'])) {
119
            $used_params[] = 'properties';
120
            if (isset($parameters['properties'][$this->property_id]) &&
121
                is_array($parameters['properties'][$this->property_id])
122
            ) {
123
                $property_id = $this->property_id;
124
                $psvs = Yii::$app->db->cache(
125
                    function($db) use ($property_id, $parameters) {
0 ignored issues
show
Unused Code introduced by
The parameter $db is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
126
                        $vals = array_values($parameters['properties'][$property_id]);
127
                        $vals = array_map(function($item){
128
                            return intval($item);
129
                        }, $vals);
130
                        return PropertyStaticValues::find()
131
                            ->where(['id' => $vals])
132
                            ->orderBy('sort_order ASC, name ASC')
133
                            ->asArray(true)
134
                            ->all();
135
                    },
136
                    86400, new TagDependency(['tags'=>[ActiveRecordHelper::getCommonTag(PropertyStaticValues::className())]])
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 125 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
137
                );
138
139
                foreach ($psvs as $psv) {
140
                    if (count($this->include_if_value) > 0) {
141
                        if (!in_array($psv['value'], $this->include_if_value)) {
142
                            return false;
143
                        }
144
                    }
145
                }
146
                if (!empty($psvs)) {
147
                    foreach ($psvs as $psv) {
148
                        $cacheTags[] = ActiveRecordHelper::getObjectTag(PropertyStaticValues::className(), $psv['id']);
149
                    }
150
                    return implode('/', ArrayHelper::getColumn($psvs, 'slug'));
151
                } else {
152
                    return false;
153
                }
154
            } else {
155
                return $this->checkIncludeIfValue();
156
            }
157
        } else {
158
            return $this->checkIncludeIfValue();
159
        }
160
    }
161
}
162