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.
Passed
Push — master ( 2d5011...ba6635 )
by
unknown
10:06
created

PageController::behaviors()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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