LoginForm   A
last analyzed

Complexity

Total Complexity 37

Size/Duplication

Total Lines 148
Duplicated Lines 0 %

Coupling/Cohesion

Components 3
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 37
lcom 3
cbo 2
dl 0
loc 148
ccs 0
cts 114
cp 0
rs 9.44
c 0
b 0
f 0

14 Methods

Rating   Name   Duplication   Size   Complexity  
A init() 0 9 2
A run() 0 7 1
A isShown() 0 4 2
A getPage() 0 4 2
A getPageBase() 0 4 2
A buildPage() 0 14 5
A getText() 0 4 2
A getDefaultText() 0 11 5
A defaultTexts() 0 8 1
A detectInputIcon() 0 14 2
A detectInputType() 0 4 2
A getTextAttributes() 0 14 4
A getBoolAttributes() 0 18 6
A getBoolAttribute() 0 4 1
1
<?php
2
/**
3
 * Pluggable themes for Yii2
4
 *
5
 * @link      https://github.com/hiqdev/yii2-thememanager
6
 * @package   yii2-thememanager
7
 * @license   BSD-3-Clause
8
 * @copyright Copyright (c) 2015-2017, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hiqdev\thememanager\widgets;
12
13
use Yii;
14
15
/**
16
 * LoginForm widget - renders login complex login form.
17
 * @author Andrii Vasyliev <[email protected]>
18
 */
19
class LoginForm extends \yii\base\Widget
20
{
21
    public $model;
22
    public $isCaptchaRequired = false;
23
    public $options = [];
24
25
    public $shows = [];
26
    public $pages = [];
27
    public $texts = [];
28
    public $disables = [];
29
30
    protected $_textAttributes;
31
    protected $_boolAttributes;
32
    protected $_defaultTexts;
33
34
    public function init()
35
    {
36
        parent::init();
37
        $defaults = ['id' => 'login-form'];
38
        if (!empty($this->options['validationUrl'])) {
39
            $defaults['enableAjaxValidation'] = true;
40
        }
41
        $this->options = array_merge($defaults, $this->options);
42
    }
43
44
    public function run()
45
    {
46
        return $this->render('LoginForm', [
47
            'model' => $this->model,
48
            'widget' => $this,
49
        ]);
50
    }
51
52
    public function isShown($action)
53
    {
54
        return empty($this->disables[$action]) && !empty($this->shows[$action]);
55
    }
56
57
    public function getPage($action)
58
    {
59
        return $this->isShown($action) ? $this->buildPage($this->getPageBase($action)) : null;
60
    }
61
62
    public function getPageBase($action)
63
    {
64
        return isset($this->pages[$action]) ? $this->pages[$action] : $action;
65
    }
66
67
    protected function buildPage($page)
68
    {
69
        if (!$page) {
70
            return null;
71
        }
72
        if (!is_array($page)) {
73
            $page = [$page];
74
        }
75
        if (!isset($page['username']) && isset($this->model->username)) {
76
            $page['username'] = $this->model->username;
77
        }
78
79
        return $page;
80
    }
81
82
    public function getText($action)
83
    {
84
        return isset($this->texts[$action]) ? $this->texts[$action] : $this->getDefaultText($action);
85
    }
86
87
    public function getDefaultText($action)
88
    {
89
        if ($action === 'header' || $action === 'button') {
90
            return Yii::$app->view->title;
91
        }
92
        if (empty($this->_defaultTexts)) {
93
            $this->_defaultTexts = $this->defaultTexts();
94
        }
95
96
        return isset($this->_defaultTexts[$action]) ? $this->_defaultTexts[$action] : null;
97
    }
98
99
    public function defaultTexts()
100
    {
101
        return [
102
            'restore-password' => Yii::t('hiqdev.thememanager', 'I forgot my password'),
103
            'signup' => Yii::t('hiqdev.thememanager', 'Register a new membership'),
104
            'login' => Yii::t('hiqdev.thememanager', 'I already have a membership'),
105
        ];
106
    }
107
108
    public function detectInputIcon($name)
109
    {
110
        static $marks = [
111
            'old_password'      => 'lock',
112
            'password'          => 'lock',
113
            'password_retype'   => 'sign-in',
114
            'username'          => 'envelope',
115
            'email'             => 'envelope',
116
            'first_name'        => 'user',
117
            'last_name'         => 'user',
118
        ];
119
120
        return isset($marks[$name]) ? $marks[$name] : '';
121
    }
122
123
    public function detectInputType($name)
124
    {
125
        return strpos($name, 'password') === false ? 'text' : 'password';
126
    }
127
128
    public function getTextAttributes()
129
    {
130
        if ($this->_textAttributes === null) {
131
            $this->_textAttributes = [];
132
            $bools = $this->getBoolAttributes();
133
            foreach ($this->model->activeAttributes() as $attribute) {
134
                if (empty($bools[$attribute])) {
135
                    $this->_textAttributes[$attribute] = $attribute;
136
                }
137
            }
138
        }
139
140
        return $this->_textAttributes;
141
    }
142
143
    public function getBoolAttributes()
144
    {
145
        if ($this->_boolAttributes === null) {
146
            $this->_boolAttributes = [];
147
            foreach ($this->model->rules() as $rule) {
148
                if ($rule[1] === 'boolean') {
149
                    $attributes = (array) $rule[0];
150
                    foreach ($attributes as $attribute) {
151
                        if ($this->model->isAttributeActive($attribute)) {
152
                            $this->_boolAttributes[$attribute] = $attribute;
153
                        }
154
                    }
155
                }
156
            }
157
        }
158
159
        return $this->_boolAttributes;
160
    }
161
162
    public function getBoolAttribute()
163
    {
164
        return reset($this->getBoolAttributes());
0 ignored issues
show
Bug introduced by
$this->getBoolAttributes() cannot be passed to reset() as the parameter $array expects a reference.
Loading history...
165
    }
166
}
167