Completed
Push — master ( 479bd3...ba046f )
by Ross
24:36
created

Form   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 80
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 96.97%

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 33
cp 0.9697
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 Magento\Framework\Exception\LocalizedException;
28
use Rossmitchell\Twofactor\Model\Admin\AdminUser;
29
use Rossmitchell\Twofactor\Model\Admin\Attribute\IsUsingTwoFactor;
30
use Rossmitchell\Twofactor\Model\Config\Admin;
31
32
class Form
33
{
34
    /**
35
     * @var AdminUser
36
     */
37
    private $adminUser;
38
    /**
39
     * @var Admin
40
     */
41
    private $adminConfig;
42
43
    /**
44
     * Form constructor.
45
     *
46
     * @param AdminUser $adminUser
47
     * @param Admin     $adminConfig
48
     */
49 3
    public function __construct(AdminUser $adminUser, Admin $adminConfig)
50
    {
51 3
        $this->adminUser = $adminUser;
52 3
        $this->adminConfig = $adminConfig;
53 3
    }
54
55 3
    public function beforeSetForm(OriginalClass $subject, OriginalForm $form)
56
    {
57 3
        if ($this->isTwoFactorEnabled() === true) {
58 2
            $fieldSet = $this->getFieldSetFromForm($form);
59 2
            $this->addFieldToFieldSet($fieldSet);
60 2
            $this->updateFormData($form, $subject);
61
        }
62
63 3
        return [$form];
64
    }
65
66 3
    private function isTwoFactorEnabled()
67
    {
68 3
        return ($this->adminConfig->isTwoFactorEnabled() == true);
69
    }
70
71
    /**
72
     * @param OriginalForm $form
73
     *
74
     * @return Fieldset
75
     * @throws \Exception
76
     */
77 2
    private function getFieldSetFromForm(OriginalForm $form)
78
    {
79 2
        $fieldSet = $form->getElement('base_fieldset');
80 2
        if (!($fieldSet instanceof Fieldset)) {
81
            throw new LocalizedException(__("The Fieldset has changed it's ID"));
82
        }
83
84 2
        return $fieldSet;
85
    }
86
87 2
    private function addFieldToFieldSet(Fieldset $fieldSet)
88
    {
89 2
        $attributeCode = IsUsingTwoFactor::ATTRIBUTE_CODE;
90 2
        $fieldSet->addField(
91
            $attributeCode,
92 2
            'select',
93
            [
94 2
                'name' => $attributeCode,
95 2
                'label' => __('Use Two Factor for this account'),
96 2
                'title' => __('Use Two Factor for this account'),
97
                'values' => [['value' => '0', 'label' => 'No'], ['value' => '1', 'label' => 'Yes']],
98 2
                'class' => 'select',
99
            ]
100
        );
101 2
    }
102
103 2
    private function updateFormData(OriginalForm $form, OriginalClass $subject)
104
    {
105 2
        $user = $this->adminUser->getAdminUser();
106 2
        $user->unsetData('password');
107 2
        $userData = $user->getData();
108 2
        unset($userData[$subject::IDENTITY_VERIFICATION_PASSWORD_FIELD]);
109 2
        $form->setValues($userData);
110 2
    }
111
}
112