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.

DynamicContentTrait   A
last analyzed

Complexity

Total Complexity 32

Size/Duplication

Total Lines 150
Duplicated Lines 17.33 %

Coupling/Cohesion

Components 0
Dependencies 9

Importance

Changes 0
Metric Value
wmc 32
lcom 0
cbo 9
dl 26
loc 150
rs 9.84
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
F loadDynamicContent() 26 147 32

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace app\traits;
4
5
use app\models\BaseObject;
6
use app\models\DynamicContent;
7
use app\modules\core\helpers\ContentBlockHelper;
8
use devgroup\TagDependencyHelper\ActiveRecordHelper;
9
use Yii;
10
use yii\caching\TagDependency;
11
use yii\helpers\Json;
12
13
trait DynamicContentTrait
14
{
15
    public function loadDynamicContent($object_id, $route, $selections)
16
    {
17
        /** @var \app\components\Response $response */
18
        $response = Yii::$app->response;
19
        if ($response->is_prefiltered_page === true) {
20
            // DynamicContent should not work on prefiltered pages - all needed content is set in corresponding model
21
            return;
22
        }
23
        /**
24
         * @var $this \yii\web\Controller
25
         */
26
27
        $dynamicCacheKey = 'dynamicCacheKey' . json_encode([$object_id, $route, $selections]);
28
29
        if (!$dynamicResult = Yii::$app->cache->get($dynamicCacheKey)) {
30
            
31
            $dynamicResult = [];
32
            $models = DynamicContent::find()
33
                ->where([
34
                    'object_id' => $object_id,
35
                    'route' => $route,
36
                ])
37
                ->all();
38
39
            if (isset($selections['properties']) === false) {
40
                $selections['properties'] = [];
41
            }
42
43
            /**
44
             * @var $model DynamicContent
45
             */
46
            foreach ($models as $model) {
47
                if (is_int($model->apply_if_last_category_id) === true && $model->apply_if_last_category_id !== 0) {
48
                    if (!isset($selections['last_category_id']) || $selections['last_category_id'] != $model->apply_if_last_category_id) {
49
                        continue;
50
                    }
51
                }
52
                $model_selections = Json::decode($model->apply_if_params);
53
                if (is_array($model_selections) === true) {
54
                    if (isset($selections['properties']) === true) {
55
                        $matches = true;
56
                        foreach ($model_selections as $property_id => $values) {
57
                            if (!is_array($values)) {
58
                                $values = [$values];
59
                            }
60
                            sort($values);
61
                            $values = array_unique($values);
62
                            if (isset($selections['properties'][$property_id]) === true) {
63
                                if (count($values) === count($selections['properties'][$property_id])) {
64
                                    if (
65
                                        count(array_diff($values, $selections['properties'][$property_id])) === 0 &&
66
                                        count(array_diff($selections['properties'][$property_id], $values)) === 0
67
                                    ) {
68
69
                                    } else {
70
                                        $matches = false;
71
                                    }
72
                                } else {
73
                                    $matches = false;
74
                                }
75
                            } else {
76
                                $matches = false;
77
                                break;
78
                            }
79
80
                        }
81
                    } else {
82
                        $matches = false;
83
                    }
84
                    if ($matches === false) {
85
                        continue;
86
                    }
87
                    if (count($selections['properties']) != count($model_selections)) {
88
                        $matches = false;
89
                    }
90
91
                    if ($matches === true) {
92
                        $dynamicResult['model'] = $model;
93 View Code Duplication
                        if ($model->title) {
94
                            $dynamicResult['title'] = ContentBlockHelper::compileContentString(
95
                                $model->title,
96
                                self::class . "{$model->id}:title",
97
                                new TagDependency(
98
                                    [
99
                                        "tags" => [
100
                                            ActiveRecordHelper::getObjectTag($model, $model->id)
101
                                        ]
102
                                    ]
103
                                )
104
                            );
105
                        }
106 View Code Duplication
                        if ($model->meta_description) {
107
                            $dynamicResult['meta_description'] = ContentBlockHelper::compileContentString(
108
                                $model->meta_description,
109
                                self::class . ":{$model->id}:meta_description",
110
                                new TagDependency(
111
                                    [
112
                                        "tags" => [
113
                                            ActiveRecordHelper::getObjectTag($model, $model->id)
114
                                        ]
115
                                    ]
116
                                )
117
                            );
118
                        }
119
                        if ($model->h1) {
120
                            $dynamicResult['blocks']['h1'] = $model->h1;
121
                        }
122
                        $dynamicResult['blocks']['announce'] = $model->announce;
123
                        $dynamicResult['blocks'][$model->content_block_name] = $model->content;
124
                    }
125
                }
126
            }
127
128
            Yii::$app->cache->set(
129
                $dynamicCacheKey,
130
                $dynamicResult,
131
                86400,
132
                new TagDependency(
133
                    [
134
                        'tags' => [
135
                            ActiveRecordHelper::getCommonTag(DynamicContent::className()),
0 ignored issues
show
Deprecated Code introduced by
The method yii\base\BaseObject::className() has been deprecated with message: since 2.0.14. On PHP >=5.5, use `::class` instead.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
136
                            ActiveRecordHelper::getObjectTag(BaseObject::className(), $object_id),
0 ignored issues
show
Deprecated Code introduced by
The method yii\base\BaseObject::className() has been deprecated with message: since 2.0.14. On PHP >=5.5, use `::class` instead.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
137
                            $route,
138
                        ]
139
                    ]
140
                )
141
            );
142
        }
143
        if (is_array($dynamicResult) === true && $dynamicResult !== []) {
144
            $response->dynamic_content_trait = true;
145
            $response->matched_dynamic_content_trait_model = $dynamicResult['model'];
146
            if (isset($dynamicResult['title']) && $dynamicResult['title']) {
147
                $response->title = $dynamicResult['title'];
148
                $response->dynamic_content_title_rewrited = true;
149
            }
150
            if (isset($dynamicResult['meta_description']) && $dynamicResult['meta_description']) {
151
                $response->meta_description = $dynamicResult['meta_description'];
152
                $response->dynamic_content_meta_description_rewrited = true;
153
            }
154
            if (isset($dynamicResult['blocks']) && is_array($dynamicResult['blocks'])) {
155
                foreach ($dynamicResult['blocks'] as $nameBlock => $contentBlock) {
156
                    $response->blocks[$nameBlock] = $contentBlock;
157
                    $response->dynamic_content_blocks_rewrited[$nameBlock] = true;
158
                }
159
            }
160
        }
161
    }
162
}
163