ensureUserBehaviorAttached()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3.0416

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 5
cts 6
cp 0.8333
rs 9.8666
c 0
b 0
f 0
cc 3
nc 3
nop 0
crap 3.0416
1
<?php
2
/**
3
 * @link https://github.com/vuongxuongminh/yii2-mfa
4
 * @copyright Copyright (c) 2019 Vuong Xuong Minh
5
 * @license [New BSD License](http://www.opensource.org/licenses/bsd-license.php)
6
 */
7
8
namespace vxm\mfa;
9
10
use yii\base\InvalidConfigException;
11
use yii\di\Instance;
12
use yii\web\User;
13
14
/**
15
 * EnsureUserBehaviorAttachedTrait to ensure an user object had attached `\vxm\mfa\Behavior`
16
 *
17
 * @author Vuong Minh <[email protected]>
18
 * @since 1.0.0
19
 */
20
trait EnsureUserBehaviorAttachedTrait
21
{
22
23
    /**
24
     * @var array|string|User an user component id or an user instance need to verify otp of identity.
25
     */
26
    public $user = 'user';
27
28
    /**
29
     * Ensure user object had been attach `vxm\mfa\Behavior`
30
     *
31
     * @throws InvalidConfigException
32
     */
33 7
    protected function ensureUserBehaviorAttached()
34
    {
35 7
        $this->user = Instance::ensure($this->user, 'yii\web\User');
36
37 7
        foreach ($this->user->getBehaviors() as $behavior) {
38 7
            if ($behavior instanceof Behavior) {
39 7
                return;
40
            }
41
        }
42
43
        throw new InvalidConfigException('An user instance must be attach `\vxm\mfa\Behavior`');
44
    }
45
46
}
47