Completed
Push — master ( 61134e...d54c31 )
by Razon
02:14
created

ArticleController   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
eloc 30
c 1
b 0
f 0
dl 0
loc 61
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A afterAction() 0 9 2
A actionView() 0 6 1
A actionIndex() 0 22 1
A findModel() 0 9 2
1
<?php
2
namespace App\Http\Controller;
3
4
use App\Model\Article;
5
use App\Model\StatusInterface;
6
use yii\data\Pagination;
7
use yii\web\NotFoundHttpException;
8
9
class ArticleController extends Controller
10
{
11
    public function afterAction($action, $result)
12
    {
13
        if ($action->id === 'view') {
14
            $this->model->updateCounters([
15
                'views' => 1
16
            ]);
17
        }
18
19
        return parent::afterAction($action, $result);
20
    }
21
22
    public function actionIndex()
23
    {
24
        $query = Article::find()
25
            ->alias('a')
26
            ->joinWith('category')
27
            ->where([
28
                'a.status' => StatusInterface::STATUS_ACTIVE,
29
                'a.is_deleted' => 0,
30
            ])
31
            ->andWhere(['<=', 'a.release_time', time()])
32
            ->orderBy(['a.release_time' => SORT_DESC]);
33
34
        $count = $query->count();
35
        $pagination = new Pagination(['totalCount' => $count]);
36
37
        $articles = $query->offset($pagination->offset)
38
            ->limit($pagination->limit)
39
            ->all();
40
        
41
        return $this->render('index', [
42
            'articles' => $articles,
43
            'pagination' => $pagination,
44
        ]);
45
    }
46
47
    public function actionView($id)
48
    {
49
        $model = $this->findModel($id);
50
        
51
        return $this->render('view', [
52
            'model' => $model,
53
        ]);
54
    }
55
56
    /**
57
     * @var Article $model
58
     */
59
    private $model;
60
61
    private function findModel($id)
62
    {
63
        $model = Article::findOne($id);
64
        if (!$model) {
0 ignored issues
show
introduced by
$model is of type yii\db\ActiveRecord, thus it always evaluated to true.
Loading history...
65
            throw new NotFoundHttpException('Article does not exists');
66
        }
67
68
        $this->model = $model;
69
        return $this->model;
70
    }
71
}
72