Controller::init()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 2
eloc 9
c 2
b 0
f 0
nc 2
nop 0
dl 0
loc 12
rs 9.9666
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
        parent::init();
24
        if ($dbPath = realpath(__DIR__ . '/../exchange.db')) {
25
            $config = [
26
                'class' => 'yii\db\Connection',
27
                'dsn' => 'sqlite:' . $dbPath,
28
                'username' => '',
29
                'password' => '',
30
                'charset' => 'utf8',
31
            ];
32
            \Yii::$app->set('exchangeDb', $config);
33
        }
34
    }
35
36
    public function render($view, $params = [])
37
    {
38
        if (!\Yii::$app->getView()->title) {
39
            \Yii::$app->getView()->title = $this->action->id;
40
        }
41
        Breadcrumbs::formCrumbs($this->action, $params);
42
        return parent::render($view, $params);
43
    }
44
45
    public function behaviors()
46
    {
47
        $behaviors = [];
48
        if (\Yii::$app->user->isGuest) {
49
            if ($this->module->auth) {
50
                $auth = $this->module->auth;
51
            } else {
52
                $auth = [$this->module, 'auth'];
53
            }
54
            $behaviors = [
55
                'basicAuth' => [
56
                    'class' => HttpBasicAuth::class,
57
                    'auth' => $auth
58
                ],
59
            ];
60
        }
61
        return $behaviors;
62
    }
63
}