AdditionalAccountTrait::getCanBeLogon()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2.032

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 8
ccs 4
cts 5
cp 0.8
rs 9.4285
cc 2
eloc 5
nc 2
nop 0
crap 2.032
1
<?php
2
3
/**
4
 *  _   __ __ _____ _____ ___  ____  _____
5
 * | | / // // ___//_  _//   ||  __||_   _|
6
 * | |/ // /(__  )  / / / /| || |     | |
7
 * |___//_//____/  /_/ /_/ |_||_|     |_|
8
 * @link http://vistart.name/
9
 * @copyright Copyright (c) 2016 vistart
10
 * @license http://vistart.name/license/
11
 */
12
13
namespace vistart\Models\traits;
14
15
/**
16
 * Additional account features. This trait should be used in blameable model or
17
 * its extended class.
18
 * @property boolean $canBeLogon determines whether this account could be used for logging-in.
19
 * @property-read array $enableLoginAttributeRules
20
 * @property-read array $additionalAccountRules
21
 * @version 2.0
22
 * @author vistart <[email protected]>
23
 */
24
trait AdditionalAccountTrait
25
{
26
    use PasswordTrait;
27
28
    /**
29
     * @var boolean|string The attribute of which determines whether enable to
30
     * login with current additional account. You can assign it to false ff you
31
     * want to disable this feature, this is equivolent to not allow to login
32
     * with current additional account among all the users.
33
     */
34
    public $enableLoginAttribute = false;
35
36
    /**
37
     * @var boolean|string  Determines whether login with current additional
38
     * account with an independent password or not. If you set $enableLoginAttribute
39
     * to false, this feature will be skipped.
40
     */
41
    public $independentPassword = false;
42
43
    /**
44
     * Get this additional account could be used for logging-in.
45
     * @return boolean
46
     */
47 1
    public function getCanBeLogon()
48
    {
49 1
        if (!$this->enableLoginAttribute) {
50
            return false;
51
        }
52 1
        $enableLoginAttribute = $this->enableLoginAttribute;
53 1
        return $this->$enableLoginAttribute > 0;
54
    }
55
56
    /**
57
     * Set this additional accunt could be used for logging-in.
58
     * @param boolean $can
59
     * @return integer
60
     */
61 1
    public function setCanBeLogon($can)
62
    {
63 1
        if (!$this->enableLoginAttribute) {
64
            return;
65
        }
66 1
        $enableLoginAttribute = $this->enableLoginAttribute;
67 1
        $this->$enableLoginAttribute = ($can ? 1 : 0);
68 1
    }
69
70
    /**
71
     * Get rules associated with enable login attribute.
72
     * If enable login feature by this additional account, it will return the rules
73
     * with true by default.
74
     * @return array rules.
75
     */
76 6
    public function getEnableLoginAttributeRules()
77
    {
78 6
        return $this->enableLoginAttribute && is_string($this->enableLoginAttribute) ? [
79
            [[$this->enableLoginAttribute], 'boolean'],
80
            [[$this->enableLoginAttribute], 'default', 'value' => true],
81 6
            ] : [];
82
    }
83
84
    /**
85
     * Get rules associated with additional account attributes.
86
     * @return array rules.
87
     */
88 6
    public function getAdditionalAccountRules()
89
    {
90 6
        $rules = $this->getEnableLoginAttributeRules();
91 6
        if ($this->independentPassword) {
92 1
            $rules = array_merge($rules, $this->getPasswordHashRules());
93 1
        }
94 6
        return $rules;
95
    }
96
}
97