Bootstrap::bootstrap()   C
last analyzed

Complexity

Conditions 10
Paths 55

Size

Total Lines 50
Code Lines 33

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 50
c 0
b 0
f 0
rs 5.7647
cc 10
eloc 33
nc 55
nop 1

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * Bootstrap script for the module yii2-activeuser
4
 *
5
 * @link https://github.com/inblank/yii2-activeuser
6
 * @copyright Copyright (c) 2016 Pavel Aleksandrov <[email protected]>
7
 * @license http://opensource.org/licenses/MIT
8
 */
9
namespace inblank\activeuser;
10
11
use inblank\activeuser\traits\CommonTrait;
12
use yii;
13
use yii\base\Application;
14
use yii\console\Application as ConsoleApplication;
15
use yii\i18n\PhpMessageSource;
16
use yii\web\GroupUrlRule;
17
18
/**
19
 * Bootstrap class for the module yii2-activeuser
20
 * @package inblank\activeuser
21
 */
22
class Bootstrap implements yii\base\BootstrapInterface
23
{
24
    use CommonTrait;
25
26
    /** @var array Model's map */
27
    private $_modelMap = [
28
    ];
29
30
    /**
31
     * Bootstrap method to be called during application bootstrap stage.
32
     * @param Application $app the application currently running
33
     */
34
    public function bootstrap($app)
0 ignored issues
show
Coding Style Best Practice introduced by
Please use __construct() instead of a PHP4-style constructor that is named after the class.
Loading history...
35
    {
36
        /** @var Module $module */
37
        /** @var \yii\db\ActiveRecord $modelName */
38
        if ($app->hasModule('activeuser') && ($module = $app->getModule('activeuser')) instanceof Module) {
39
            $this->_modelMap = array_merge($this->_modelMap, $module->modelMap);
40
            foreach ($this->_modelMap as $name => $definition) {
41
                $class = "inblank\\activeuser\\models\\" . $name;
42
                Yii::$container->set($class, $definition);
43
                $modelName = is_array($definition) ? $definition['class'] : $definition;
44
                $module->modelMap[$name] = $modelName;
45
            }
46
            if ($app instanceof ConsoleApplication) {
47
                $app->controllerMap['activeuser'] = [
48
                    'class' => 'inblank\activeuser\commands\DefaultController',
49
                ];
50
            } else {
51
                // init user
52
                Yii::$container->set('yii\web\User', [
53
                    'loginUrl' => ['/activeuser/account/login'],
54
                    'identityClass' => self::di('User'),
55
                ]);
56
                $configUrlRule = [
57
                    'prefix' => $module->urlPrefix,
58
                    'rules' => defined('IS_BACKEND') ? $module->urlRulesBackend : $module->urlRulesFrontend,
59
                ];
60
                if ($module->urlPrefix != 'activeuser') {
61
                    $configUrlRule['routePrefix'] = 'activeuser';
62
                }
63
                $app->urlManager->addRules([new GroupUrlRule($configUrlRule)], false);
64
                if (defined('IS_BACKEND')) {
65
                    // is backend, and controller have other namespace
66
                    $module->controllerNamespace = 'inblank\activeuser\controllers\backend';
67
                    $module->frontendUrlManager = new yii\web\UrlManager([
68
                        'baseUrl' => '/',
69
                        'enablePrettyUrl' => true,
70
                        'showScriptName' => false,
71
                    ]);
72
                    $configUrlRule['rules'] = $module->urlRulesFrontend;
73
                    $module->frontendUrlManager->addRules([new GroupUrlRule($configUrlRule)], false);
74
                }
75
            }
76
            if (!isset($app->get('i18n')->translations['activeuser*'])) {
77
                $app->get('i18n')->translations['activeuser*'] = [
78
                    'class' => PhpMessageSource::className(),
0 ignored issues
show
Deprecated Code introduced by
The method yii\base\BaseObject::className() has been deprecated with message: since 2.0.14. On PHP >=5.5, use `::class` instead.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
79
                    'basePath' => __DIR__ . '/messages',
80
                ];
81
            }
82
        }
83
    }
84
}
85