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 — filters ( fb817b...44fc4f )
by
unknown
11:13
created

PageController::actionShow()   B

Complexity

Conditions 4
Paths 5

Size

Total Lines 31
Code Lines 17

Duplication

Lines 9
Ratio 29.03 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 9
loc 31
rs 8.5806
cc 4
eloc 17
nc 5
nop 1
1
<?php
2
3
namespace app\modules\page\controllers;
4
5
use app\components\Controller;
6
use app\modules\page\models\Page;
7
use app\models\Search;
8
use app\modules\seo\behaviors\MetaBehavior;
9
use app\traits\LoadModel;
10
use devgroup\TagDependencyHelper\ActiveRecordHelper;
11
use Yii;
12
use yii\caching\TagDependency;
13
use yii\data\Pagination;
14
use yii\db\ActiveQuery;
15
use yii\web\ForbiddenHttpException;
16
use yii\web\NotFoundHttpException;
17
use yii\web\Response;
18
19
class PageController extends Controller
20
{
21
    use LoadModel;
22
23
    /**
24
     * @param $id
25
     * @return string
26
     * @throws NotFoundHttpException
27
     * @throws \yii\web\ServerErrorHttpException
28
     */
29
    public function actionShow($id)
30
    {
31
        if (null === $model = Page::findById($id)) {
32
            throw new NotFoundHttpException;
33
        }
34
35 View Code Duplication
        if (!empty($model->meta_description)) {
36
            $this->view->registerMetaTag(
37
                [
38
                    'name' => 'description',
39
                    'content' => $model->meta_description,
40
                ],
41
                'meta_description'
42
            );
43
        }
44
45
        $this->view->title = $model->title;
46
        if (!empty($model->h1)) {
47
            $this->view->blocks['h1'] = $model->h1;
48
        }
49
        $this->view->blocks['content'] = $model->content;
50
        $this->view->blocks['announce'] = $model->announce;
51
52
        return $this->render(
53
            $this->computeViewFile($model, 'show'),
54
            [
55
                'model' => $model,
56
                'breadcrumbs' => $this->buildBreadcrumbsArray($model)
57
            ]
58
        );
59
    }
60
61
    /**
62
     * @param $id
63
     * @return string
64
     * @throws NotFoundHttpException
65
     * @throws \yii\web\ServerErrorHttpException
66
     */
67
    public function actionList($id)
68
    {
69
        if (null === $model = Page::findById($id)) {
70
            throw new NotFoundHttpException;
71
        }
72 View Code Duplication
        if (!empty($model->meta_description)) {
73
            $this->view->registerMetaTag(
74
                [
75
                    'name' => 'description',
76
                    'content' => $model->meta_description,
77
                ],
78
                'meta_description'
79
            );
80
        }
81
82
        $cacheKey = 'PagesList:'.$model->id;
83
84
        // query that needed for pages retrieve and pagination
85
        $children = Page::find()
86
            ->where(['parent_id' => $model->id, 'published' => 1])
87
            ->orderBy('date_added DESC, sort_order')
88
            ->with('images');
89
90
        // count all pages
91
        $count = Yii::$app->cache->get($cacheKey.';count');
92
        if ($count === false) {
93
            $countQuery = clone $children;
94
            $count = $countQuery->count();
95
            Yii::$app->cache->set($cacheKey.';count', $count, 86400, new TagDependency([
96
                'tags' => [
97
                    ActiveRecordHelper::getCommonTag(Page::className())
98
                ]
99
            ]));
100
        }
101
102
        $pages = new Pagination(
103
            [
104
                'defaultPageSize' => $this->module->pagesPerList,
105
                'forcePageParam' => false,
106
                'pageSizeLimit' => [
107
                    $this->module->minPagesPerList,
108
                    $this->module->maxPagesPerList
109
                ],
110
                'totalCount' => $count,
111
            ]
112
        );
113
114
        // append current page number to cache key
115
        $cacheKey .= ';page:' . $pages->page;
116
117
        $childrenModels = Yii::$app->cache->get($cacheKey);
118
        if ($childrenModels === false) {
119
120
            /** @var ActiveQuery $children */
121
122
            $children = $children->offset($pages->offset)
123
                ->limit($pages->limit)
124
                ->all();
125
            Yii::$app->cache->set($cacheKey, $children, 86400, new TagDependency([
126
                'tags' => [
127
                    ActiveRecordHelper::getCommonTag(Page::className())
128
                ]
129
            ]));
130
        } else {
131
            $children = $childrenModels;
132
        }
133
134
        $this->view->title = $model->title;
135
        if (!empty($model->h1)) {
136
            $this->view->blocks['h1'] = $model->h1;
137
        }
138
        $this->view->blocks['content'] = $model->content;
139
        $this->view->blocks['announce'] = $model->announce;
140
141
        return $this->render(
142
            $this->computeViewFile($model, 'list'),
143
            [
144
                'model' => $model,
145
                'children' => $children,
146
                'pages' => $pages,
147
                'breadcrumbs' => $this->buildBreadcrumbsArray($model),
148
            ]
149
        );
150
    }
151
152
    /**
153
     * @return array
154
     * @throws ForbiddenHttpException
155
     */
156
    public function actionSearch()
157
    {
158
        /**
159
         * @param $module \app\modules\page\PageModule
160
         */
161
        if (!Yii::$app->request->isAjax) {
162
            throw new ForbiddenHttpException();
163
        }
164
        $model = new Search();
165
        $model->load(Yii::$app->request->get());
166
        $cacheKey = 'PageSearchIds: ' . $model->q;
167
        $ids = Yii::$app->cache->get($cacheKey);
168
        if ($ids === false) {
169
            $ids = $model->searchPagesByDescription();
170
            Yii::$app->cache->set(
171
                $cacheKey,
172
                $ids,
173
                86400,
174
                new TagDependency(
175
                    [
176
                        'tags' => ActiveRecordHelper::getCommonTag(Page::className()),
177
                    ]
178
                )
179
            );
180
        }
181
        $pages = new Pagination(
182
            [
183
                'defaultPageSize' => $this->module->searchResultsLimit,
184
                'forcePageParam' => false,
185
                'pageSizeLimit' => [
186
                    $this->module->minPagesPerList,
187
                    $this->module->maxPagesPerList
188
                ],
189
                'totalCount' => count($ids),
190
            ]
191
        );
192
        $cacheKey .= ' : ' . $pages->offset;
193
        $pagelist = Yii::$app->cache->get($cacheKey);
194 View Code Duplication
        if ($pagelist === false) {
195
            $pagelist = Page::find()->where(
196
                [
197
                    'in',
198
                    '`id`',
199
                    array_slice(
200
                        $ids,
201
                        $pages->offset,
202
                        $pages->limit
203
                    )
204
                ]
205
            )->addOrderBy('sort_order')->with('images')->all();
206
            Yii::$app->cache->set(
207
                $cacheKey,
208
                $pagelist,
209
                86400,
210
                new TagDependency(
211
                    [
212
                        'tags' => ActiveRecordHelper::getCommonTag(Page::className()),
213
                    ]
214
                )
215
            );
216
        }
217
        Yii::$app->response->format = Response::FORMAT_JSON;
218
        return [
219
            'view' => $this->renderPartial(
220
                'search',
221
                [
222
                    'model' => $model,
223
                    'pagelist' => $pagelist,
224
                    'pages' => $pages,
225
                ]
226
            ),
227
            'totalCount' => count($ids),
228
        ];
229
    }
230
231
    /*
232
    * This function build array for widget "Breadcrumbs"
233
    *   $model - model of current page
234
    * Return an array for widget or empty array
235
    */
236
    private function buildBreadcrumbsArray($model)
237
    {
238
        if ($model === null || $model->id === 1) {
239
            return [];
240
        }
241
242
        // init
243
        $breadcrumbs = [];
244
        $crumbs[$model->slug] = $model->breadcrumbs_label;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$crumbs was never initialized. Although not strictly required by PHP, it is generally a good practice to add $crumbs = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
245
246
        // get basic data
247
        $parent = Page::findById($model->parent_id);
248
        // if parent exists and not a main page
249 View Code Duplication
        while ($parent !== null && $parent->id != 1) {
250
            $crumbs[$parent->slug] = $parent->breadcrumbs_label;
251
            $parent = $parent->parent;
252
        }
253
254
        // build array for widget
255
        $url = '';
256
        $crumbs = array_reverse($crumbs, true);
257 View Code Duplication
        foreach ($crumbs as $slug => $label) {
258
            $url .= '/' . $slug;
259
            $breadcrumbs[] = [
260
                'label' => (string) $label,
261
                'url' => $url
262
            ];
263
        }
264
        unset($breadcrumbs[count($breadcrumbs) - 1]['url']); // last item is not a link
265
266
        return $breadcrumbs;
267
    }
268
}
269