UserController::findModel()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 8
Ratio 100 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 8
loc 8
rs 9.4285
cc 2
eloc 4
nc 2
nop 1
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\User;
12
use Yii;
13
use yii\data\Pagination;
14
use yii\web\Controller;
15
use yii\web\NotFoundHttpException;
16
17
/**
18
 * Class UserController
19
 *
20
 * @author Agiel K. Saputra <[email protected]>
21
 * @since 0.1.0
22
 */
23
class UserController extends Controller
24
{
25
    /**
26
     * Displays a single User model.
27
     *
28
     * @param null $id User ID
29
     * @param string|null $user Username
30
     * @return mixed
31
     * @throws \yii\web\NotFoundHttpException
32
     */
33 View Code Duplication
    public function actionView($id = null, $user = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
34
    {
35
        $render = '/user/view';
36
37
        if ($id) {
38
            $model = $this->findModel($id);
39
        } elseif ($user) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $user of type string|null is loosely compared to true; this is ambiguous if the string can be empty. 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 string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
40
            $model = $this->findModelByUsername($user);
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
        $query->offset($pages->offset)->limit($pages->limit);
55
        $posts = $query->all();
56
57
        if ($posts) {
58
            if (is_file($this->view->theme->basePath . '/user/view-' . $model->username . '.php')) {
59
                $render = 'view-' . $model->username . '.php';
60
            }
61
62
            return $this->render($render, [
63
                'user' => $model,
64
                'posts' => $posts,
65
                'pages' => isset($pages) ? $pages : null,
66
            ]);
67
        }
68
69
        throw new NotFoundHttpException(Yii::t('writesdown', 'The requested page does not exist.'));
70
    }
71
72
    /**
73
     * Finds the User model based on its primary key value.
74
     * If the model is not found, a 404 HTTP exception will be thrown.
75
     *
76
     * @param int $id User ID
77
     * @return User the loaded model
78
     * @throws NotFoundHttpException if the model cannot be found
79
     */
80 View Code Duplication
    protected function findModel($id)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
81
    {
82
        if (($model = User::findOne($id)) !== null) {
83
            return $model;
84
        }
85
86
        throw new NotFoundHttpException(Yii::t('writesdown', 'The requested page does not exist.'));
87
    }
88
89
    /**
90
     * Finds the User model based on its primary key value.
91
     * If the model is not found, a 404 HTTP exception will be thrown.
92
     *
93
     * @param string $username Username
94
     * @return User the loaded model
95
     * @throws NotFoundHttpException if the model cannot be found
96
     */
97 View Code Duplication
    protected function findModelByUsername($username)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
98
    {
99
        if (($model = User::findOne(['username' => $username])) !== null) {
100
            return $model;
101
        }
102
103
        throw new NotFoundHttpException(Yii::t('writesdown', 'The requested page does not exist.'));
104
    }
105
}
106