Passed
Push — master ( de9f16...6836c0 )
by Aleksandr
02:35
created

Controller::behaviors()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 17
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 11
nc 3
nop 0
dl 0
loc 17
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
4
namespace carono\exchange1c\controllers;
5
6
7
use carono\exchange1c\components\Breadcrumbs;
8
use carono\exchange1c\ExchangeModule;
9
use yii\filters\auth\HttpBasicAuth;
10
11
/**
12
 * Class Controller
13
 *
14
 * @property ExchangeModule module
15
 * @package carono\exchange1c\controllers
16
 */
17
abstract class Controller extends \yii\web\Controller
18
{
19
    public $layout = '@vendor/carono/yii2-1c-exchange/views/layouts/main';
20
21
    public function init()
22
    {
23
        if ($dbPath = realpath(__DIR__ . '/../exchange.db')) {
24
            $config = [
25
                'class' => 'yii\db\Connection',
26
                'dsn' => 'sqlite:' . $dbPath,
27
                'username' => '',
28
                'password' => '',
29
                'charset' => 'utf8',
30
            ];
31
            \Yii::$app->set('exchangeDb', $config);
32
        }
33
    }
34
35
    public function render($view, $params = [])
36
    {
37
        if (!\Yii::$app->getView()->title) {
38
            \Yii::$app->getView()->title = $this->action->id;
39
        }
40
        Breadcrumbs::formCrumbs($this->action, $params);
41
        return parent::render($view, $params);
42
    }
43
44
    public function behaviors()
45
    {
46
        $behaviors = [];
47
        if (\Yii::$app->user->isGuest) {
48
            if ($this->module->auth) {
49
                $auth = $this->module->auth;
50
            } else {
51
                $auth = [$this->module, 'auth'];
52
            }
53
            $behaviors = [
54
                'basicAuth' => [
55
                    'class' => HttpBasicAuth::className(),
56
                    'auth' => $auth
57
                ],
58
            ];
59
        }
60
        return $behaviors;
61
    }
62
}