Completed
Push — master ( f58984...07dd6c )
by Ross
26:19
created

Form   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 80
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 65.31%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 8
c 1
b 0
f 0
lcom 1
cbo 5
dl 0
loc 80
ccs 32
cts 49
cp 0.6531
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A beforeSetForm() 0 10 2
A isTwoFactorEnabled() 0 4 1
A getFieldSetFromForm() 0 9 2
A addFieldToFieldSet() 0 15 1
A updateFormData() 0 8 1
1
<?php
2
/**
3
 * A two factor authentication module that protects both the admin and customer logins
4
 * Copyright (C) 2017  Ross Mitchell
5
 *
6
 * This file is part of Rossmitchell/Twofactor.
7
 *
8
 * Rossmitchell/Twofactor is free software: you can redistribute it and/or modify
9
 * it under the terms of the GNU General Public License as published by
10
 * the Free Software Foundation, either version 3 of the License, or
11
 * (at your option) any later version.
12
 *
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
 * GNU General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU General Public License
19
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20
 */
21
22
namespace Rossmitchell\Twofactor\Plugin\Magento\Backend\Block\System\Account\Edit;
23
24
use Magento\Backend\Block\System\Account\Edit\Form as OriginalClass;
25
use Magento\Framework\Data\Form as OriginalForm;
26
use Magento\Framework\Data\Form\Element\Fieldset;
27
use Rossmitchell\Twofactor\Model\Admin\AdminUser;
28
use Rossmitchell\Twofactor\Model\Admin\Attribute\IsUsingTwoFactor;
29
use Rossmitchell\Twofactor\Model\Config\Admin;
30
31
class Form
32
{
33
    /**
34
     * @var AdminUser
35
     */
36
    private $adminUser;
37
    /**
38
     * @var Admin
39
     */
40
    private $adminConfig;
41
42
    /**
43
     * Form constructor.
44
     *
45
     * @param AdminUser $adminUser
46
     * @param Admin     $adminConfig
47
     */
48 3
    public function __construct(AdminUser $adminUser, Admin $adminConfig)
49
    {
50 3
        $this->adminUser = $adminUser;
51 3
        $this->adminConfig = $adminConfig;
52 3
    }
53
54 3
    public function beforeSetForm(OriginalClass $subject, OriginalForm $form)
55
    {
56 3
        if ($this->isTwoFactorEnabled() === true) {
57 2
            $fieldSet = $this->getFieldSetFromForm($form);
58 2
            $this->addFieldToFieldSet($fieldSet);
59 2
            $this->updateFormData($form, $subject);
60
        }
61
62 3
        return [$form];
63
    }
64
65 3
    private function isTwoFactorEnabled()
66
    {
67 3
        return ($this->adminConfig->isTwoFactorEnabled() == true);
68
    }
69
70
    /**
71
     * @param OriginalForm $form
72
     *
73
     * @return Fieldset
74
     * @throws \Exception
75
     */
76 2
    private function getFieldSetFromForm(OriginalForm $form)
77
    {
78 2
        $fieldSet = $form->getElement('base_fieldset');
79 2
        if (!($fieldSet instanceof Fieldset)) {
80
            throw new \Exception("The Fieldset has changed it's ID");
0 ignored issues
show
introduced by
Direct throw of Exception is discouraged. Use \Magento\Framework\Exception\LocalizedException instead.
Loading history...
81
        }
82
83 2
        return $fieldSet;
84
    }
85
86 2
    private function addFieldToFieldSet(Fieldset $fieldSet)
87
    {
88 2
        $attributeCode = IsUsingTwoFactor::ATTRIBUTE_CODE;
89 2
        $fieldSet->addField(
90
            $attributeCode,
91 2
            'select',
92
            [
93 2
                'name' => $attributeCode,
94 2
                'label' => __('Use Two Factor for this account'),
95 2
                'title' => __('Use Two Factor for this account'),
96
                'values' => [['value' => '0', 'label' => 'No'], ['value' => '1', 'label' => 'Yes']],
97 2
                'class' => 'select',
98
            ]
99
        );
100 2
    }
101
102 2
    private function updateFormData(OriginalForm $form, OriginalClass $subject)
103
    {
104 2
        $user = $this->adminUser->getAdminUser();
105 2
        $user->unsetData('password');
106 2
        $userData = $user->getData();
107 2
        unset($userData[$subject::IDENTITY_VERIFICATION_PASSWORD_FIELD]);
108 2
        $form->setValues($userData);
109 2
    }
110
}
111