CommonTrait::di()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 3
nc 2
nop 1
1
<?php
2
3
namespace inblank\activeuser\traits;
4
5
use inblank\activeuser\Module;
6
use yii;
7
use yii\base\InvalidConfigException;
8
9
/**
10
 * Class CommonTrait
11
 * @property Module $module current module
12
 *
13
 * @package inblank\activeuser\traits
14
 */
15
trait CommonTrait
16
{
17
    /**
18
     * Get module
19
     * @return \inblank\activeuser\Module
20
     * @throws InvalidConfigException
21
     */
22
    static function getModule()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
23
    {
24
        /** @var \inblank\activeuser\Module $module */
25
        if ($module = Yii::$app->getModule('activeuser')) {
26
            return $module;
27
        }
28
        throw new InvalidConfigException('You must configure module as `activeuser`');
29
    }
30
31
    /**
32
     * Models dependency injection resolver
33
     * @param string $name class name for resolve
34
     * @return mixed
35
     * @throws InvalidConfigException
36
     */
37
    public static function di($name)
38
    {
39
        $class = 'inblank\activeuser\models\\' . $name;
40
        return empty(self::getModule()->modelMap[$name]) ? $class : self::getModule()->modelMap[$name];
41
    }
42
43
    /**
44
     * Models table name with dependency injection resolver
45
     * @param string $name class name for get table name
46
     * @return string
47
     * @throws InvalidConfigException
48
     */
49
    public static function tn($name)
50
    {
51
        $name = self::di($name);
52
        return $name::tableName();
53
    }
54
}
55