Completed
Push — master ( b61081...bf01ff )
by Pavel
04:03
created

ApplicationMock::changeModule()   C

Complexity

Conditions 13
Paths 24

Size

Total Lines 34
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 34
c 0
b 0
f 0
rs 5.1234
cc 13
eloc 19
nc 24
nop 3

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
namespace app\components;
3
4
use Yii;
5
use yii\web\Application;
6
7
/**
8
 * Yii2 application mock class
9
 * @package app\components
10
 */
11
class ApplicationMock extends Application
12
{
13
    /** @var array currently used application configuration */
14
    public static $config;
15
16
    /** @inheritdoc */
17
    public function __construct($config = [])
18
    {
19
        if (self::$config === null) {
20
            // if first call, store original configuration
21
            self::$config = $config;
22
        }
23
        parent::__construct(self::$config);
24
    }
25
26
    /**
27
     * Change module configuration
28
     * @param string $moduleId module id
29
     * @param array $params new params
30
     * @param bool $merge whether to merge new and old parameters
31
     */
32
    public static function changeModule($moduleId, $params = [], $merge = true)
33
    {
34
        if (empty(self::$config['modules'])) {
35
            // create module field
36
            self::$config['modules'] = [];
37
        }
38
        if (!empty(self::$config['modules'][$moduleId]) && $merge) {
39
            // if we need merge configuration data
40
            $currentParams = self::$config['modules'][$moduleId];
41
            if (is_string($currentParams)) {
42
                $currentParams = [
43
                    'class' => $currentParams,
44
                ];
45
            }
46
            foreach ($params as $name => $value) {
47
                $currentParams[$name] = $value;
48
            }
49
            $params = $currentParams;
50
        }
51
        self::$config['modules'][$moduleId] = $params;
52
        // apply for current $app instance
53
        $module = Yii::$app->getModule($moduleId);
54
        if (!$merge || $module === null || (!empty($params['class']) && $module::className() != $params['class'])) {
55
            // new or changed
56
            Yii::$app->setModule($moduleId, $params);
57
        } else {
58
            // change the attributes
59
            foreach ($params as $name => $value) {
60
                if ($name !== 'class' && $module->canSetProperty($name)) {
61
                    $module->$name = $value;
62
                }
63
            }
64
        }
65
    }
66
}
67