Completed
Push — master ( eb4993...5ea51c )
by Igor
03:27
created

BaseController   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 4

Importance

Changes 9
Bugs 0 Features 1
Metric Value
wmc 7
c 9
b 0
f 1
lcom 2
cbo 4
dl 0
loc 61
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getJsBundle() 0 4 1
A getCssBundle() 0 4 1
A loadModel() 0 10 4
A response() 0 5 1
1
<?php
2
3
namespace app\components;
4
5
use Yii;
6
use yii\web\Controller;
7
use yii\helpers\ArrayHelper;
8
use app\helpers\Http;
9
10
/**
11
 * Global base controller
12
 */
13
class BaseController extends Controller
14
{
15
    /**
16
     * @var string Path to js bundle
17
     */
18
    public $jsBundle = 'front.js';
19
    /**
20
     * @var string Path to css bundle
21
     */
22
    public $cssBundle = 'front.css';
23
24
    /**
25
     * @return string Return CSS bundle
26
     */
27
    public function getCssBundle()
28
    {
29
        return '/assets/' . ArrayHelper::getValue(Yii::$app->params['assets'], $this->cssBundle);
30
    }
31
32
    /**
33
     * @return string Return JS bundle
34
     */
35
    public function getJsBundle()
36
    {
37
        return '/assets/' . ArrayHelper::getValue(Yii::$app->params['assets'], $this->jsBundle);
38
    }
39
40
    /**
41
     * Load the model based on its primary key value or WHERE condition.
42
     * If the model is not found or access denied, a 404 HTTP exception will be thrown.
43
     *
44
     * @param ActiveRecord $model
45
     * @param int|array $id primary key or WHERE condition
46
     * @param callable $checkAccess
47
     * @return ActiveRecord
48
     * @throws NotFoundHttpException
49
     */
50
    public function loadModel($model, $id, $checkAccess = null)
51
    {
52
        $model = $model::findOne($id);
53
54
        if ($model === null || ($checkAccess !== null && !$model->$checkAccess())) {
55
            Http::exception(404);
56
        } // @codeCoverageIgnore
57
58
        return $model;
59
    }
60
61
    /**
62
     * HTTP Response
63
     *
64
     * @param mixed $data
65
     * @param string $format The response format
66
     * @see http://www.yiiframework.com/doc-2.0/yii-web-response.html
67
     */
68
    public function response($data, $format = \yii\web\Response::FORMAT_JSON)
69
    {
70
        \Yii::$app->response->format = $format;
71
        return $data;
72
    }
73
}
74