Completed
Push — master ( d368a1...825b92 )
by vistart
08:35
created

AdditionalAccountTrait   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 44%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 15
c 1
b 0
f 1
lcom 1
cbo 1
dl 0
loc 76
ccs 11
cts 25
cp 0.44
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getPasswordIsSeperate() 0 4 2
A getSeperateLogin() 0 8 3
A setSeperateLogin() 0 8 4
A getEnableLoginAttributeRules() 0 7 4
A getAdditionalAccountRules() 0 8 2
1
<?php
2
3
/**
4
 *   _   __ __ _____ _____ ___  ____  _____
5
 *  | | / // // ___//_  _//   ||  __||_   _|
6
 *  | |/ // /(__  )  / / / /| || |     | |
7
 *  |___//_//____/  /_/ /_/ |_||_|     |_|
8
 * @link https://vistart.me/
9
 * @copyright Copyright (c) 2016 - 2017 vistart
10
 * @license https://vistart.me/license/
11
 */
12
13
namespace rhosocial\base\models\traits;
14
15
/**
16
 * Additional account features.
17
 * This trait should be used in blameable model or its extended class.
18
 * 
19
 * When an additional account is enabled with a separate password, this additional
20
 * account can only log in with its own unique password.
21
 * Note: The extra account password should not be the same as the master account
22
 * password, but we will not take the initiative to determine whether the two are the same.
23
 * 
24
 * @property boolean $seperateLogin determines whether this account could be used
25
 * for logging-in.
26
 * @property-read array $enableLoginAttributeRules
27
 * @property-read array $additionAccountRules
28
 * @version 1.0
29
 * @author vistart <[email protected]>
30
 */
31
trait AdditionalAccountTrait
32
{
33
    use PasswordTrait;
34
    
35
    /**
36
     * @var boolean|string The attribute of which determines whether enable to
37
     * login with current additional account. You can assign it to false if you
38
     * want to disable this feature, this is equivolent to not allow to login
39
     * with current additional account among all the users.
40
     */
41
    public $seperateLoginAttribute = false;
42
    
43 3
    /**
44
     * @var boolean  Determines whether login with current additional
45 3
     * account with a seperate password or not. If you set $enableLoginAttribute
46
     * to false, this feature will be skipped.
47
     */
48
    public function getPasswordIsSeperate()
49
    {
50
        return $this->getSeperateLogin() && !$this->getIsEmptyPassword();
51
    }
52
    
53
    /**
54
     * Get this additional account could be used for logging-in.
55
     * @return boolean
56
     */
57
    public function getSeperateLogin()
58
    {
59
        if (!$this->seperateLoginAttribute || empty($this->seperateLoginAttribute)) {
60 3
            return false;
61
        }
62 3
        $enableLoginAttribute = $this->seperateLoginAttribute;
63 3
        return $this->$enableLoginAttribute > 0;
64
    }
65
    
66
    /**
67
     * Set this additional accunt could be used for logging-in.
68
     * @param boolean $can
69
     * @return integer
70
     */
71
    public function setSeperateLogin($can)
72
    {
73
        if (!$this->seperateLoginAttribute || empty($this->seperateLoginAttribute)) {
74
            return;
75
        }
76
        $enableLoginAttribute = $this->seperateLoginAttribute;
77
        $this->$enableLoginAttribute = ($can ? 1 : 0);
78
    }
79
    
80
    /**
81
     * Get rules associated with enable login attribute.
82
     * If enable login feature by this additional account, it will return the rules
83
     * with true by default.
84
     * @return array rules.
85
     */
86
    public function getEnableLoginAttributeRules()
87
    {
88
        return $this->seperateLoginAttribute && is_string($this->seperateLoginAttribute) && !empty($this->seperateLoginAttribute) ? [
89 3
            [[$this->seperateLoginAttribute], 'boolean'],
90
            [[$this->seperateLoginAttribute], 'default', 'value' => true],
91 3
            ] : [];
92
    }
93
    
94 3
    /**
95
     * Get rules associated with additional account attributes.
96
     * @return array rules.
97
     */
98
    public function getAdditionalAccountRules()
99
    {
100
        $rules = $this->getEnableLoginAttributeRules();
101 3
        if ($this->getPasswordIsSeperate()) {
102
            $rules = array_merge($rules, $this->getPasswordHashRules());
103 3
        }
104 3
        return $rules;
105
    }
106
}