TermController::actionView()   B
last analyzed

Complexity

Conditions 4
Paths 5

Size

Total Lines 35
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
c 4
b 0
f 0
dl 0
loc 35
rs 8.5806
cc 4
eloc 25
nc 5
nop 2
1
<?php
2
/**
3
 * @link http://www.writesdown.com/
4
 * @copyright Copyright (c) 2015 WritesDown
5
 * @license http://www.writesdown.com/license/
6
 */
7
8
namespace frontend\controllers;
9
10
use common\models\Option;
11
use common\models\Term;
12
use Yii;
13
use yii\data\Pagination;
14
use yii\web\Controller;
15
use yii\web\NotFoundHttpException;
16
17
/**
18
 * Class TermController
19
 *
20
 * @author Agiel K. Saputra <[email protected]>
21
 * @since 0.1.0
22
 */
23
class TermController extends Controller
24
{
25
    /**
26
     * Displays a single Term model.
27
     *
28
     * @param integer $id Term ID
29
     * @param null $slug Term slug
30
     * @throws \yii\web\NotFoundHttpException
31
     * @return mixed
32
     */
33
    public function actionView($id = null, $slug = null)
34
    {
35
        $render = 'view';
36
37
        if ($id) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $id of type integer|null is loosely compared to true; this is ambiguous if the integer can be zero. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
38
            $model = $this->findModel($id);
39
        } elseif ($slug) {
40
            $model = $this->findModelBySlug($slug);
41
        } else {
42
            throw new NotFoundHttpException(Yii::t('writesdown', 'The requested page does not exist.'));
43
        }
44
45
        $query = $model->getPosts()
0 ignored issues
show
Bug introduced by
The method getPosts cannot be called on $model (of type array|boolean).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
46
            ->andWhere(['status' => 'publish'])
47
            ->andWhere(['<=', 'date', date('Y-m-d H:i:s')])
48
            ->orderBy(['date' => SORT_DESC]);
49
        $countQuery = clone $query;
50
        $pages = new Pagination([
51
            'totalCount' => $countQuery->count(),
52
            'pageSize' => Option::get('posts_per_page'),
53
        ]);
54
        $posts = $query->offset($pages->offset)
55
            ->limit($pages->limit)
56
            ->all();
57
58
        if (is_file($this->view->theme->basePath . '/term/view-' . $model->taxonomy->name . '.php')) {
59
            $render = 'view-' . $model->taxonomy->name;
60
        }
61
62
        return $this->render($render, [
63
            'posts' => $posts,
64
            'pages' => $pages,
65
            'term' => $model,
66
        ]);
67
    }
68
69
    /**
70
     * Finds the Post model based on its primary key value.
71
     * If the model is not found, a 404 HTTP exception will be thrown.
72
     *
73
     * @param integer $id Term ID
74
     * @return Term the loaded model
75
     * @throws NotFoundHttpException if the model cannot be found
76
     */
77
    protected function findModel($id)
78
    {
79
        $model = Term::findOne(['id' => $id]);
80
81
        if ($model) {
82
            return $model;
83
        }
84
85
        throw new NotFoundHttpException(Yii::t('writesdown', 'The requested page does not exist.'));
86
    }
87
88
    /**
89
     * Finds the Post model based on its primary key value.
90
     * If the model is not found, a 404 HTTP exception will be thrown.
91
     *
92
     * @param string $slug Term Slug
93
     * @throws \yii\web\NotFoundHttpException
94
     * @internal param string $postslug
95
     * @return Term the loaded model
96
     */
97
    protected function findModelBySlug($slug)
98
    {
99
        $model = Term::findOne(['slug' => $slug]);
100
101
        if ($model) {
102
            return $model;
103
        }
104
105
        throw new NotFoundHttpException(Yii::t('writesdown', 'The requested page does not exist.'));
106
    }
107
}
108