Completed
Push — master ( 9910ce...f4d691 )
by Razon
02:34
created

ArticleController::findModel()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 4
c 1
b 0
f 0
dl 0
loc 8
rs 10
cc 2
nc 2
nop 1
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 actionIndex()
12
    {
13
        $query = Article::find()
14
            ->alias('a')
15
            ->joinWith('category')
16
            ->where([
17
                'a.status' => StatusInterface::STATUS_ACTIVE,
18
                'a.is_deleted' => 0,
19
            ])
20
            ->andWhere(['<=', 'a.release_time', time()])
21
            ->orderBy(['a.release_time' => SORT_DESC]);
22
23
        $count = $query->count();
24
        $pagination = new Pagination(['totalCount' => $count]);
25
26
        $articles = $query->offset($pagination->offset)
27
            ->limit($pagination->limit)
28
            ->all();
29
        
30
        return $this->render('index', [
31
            'articles' => $articles,
32
            'pagination' => $pagination,
33
        ]);
34
    }
35
36
    public function actionView($id)
37
    {
38
        $model = $this->findModel($id);
39
        
40
        return $this->render('view', [
41
            'model' => $model,
42
        ]);
43
    }
44
45
    private function findModel($id)
46
    {
47
        $model = Article::findOne($id);
48
        if (!$model) {
0 ignored issues
show
introduced by
$model is of type yii\db\ActiveRecord, thus it always evaluated to true.
Loading history...
49
            throw new NotFoundHttpException('Article does not exists');
50
        }
51
52
        return $model;
53
    }
54
}