Passed
Push — master ( 8ad597...64a1db )
by Ross
03:04
created

Form::updateFormData()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 8
rs 9.4285
cc 1
eloc 6
nc 1
nop 2
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
30
class Form
31
{
32
    /**
33
     * @var AdminUser
34
     */
35
    private $adminUser;
36
37
    /**
38
     * Form constructor.
39
     *
40
     * @param AdminUser $adminUser
41
     */
42
    public function __construct(AdminUser $adminUser)
43
    {
44
        $this->adminUser = $adminUser;
45
    }
46
47
    public function beforeSetForm(OriginalClass $subject, OriginalForm $form)
48
    {
49
        $fieldSet = $this->getFieldSetFromForm($form);
50
        $this->addFieldToFieldSet($fieldSet);
51
        $this->updateFormData($form, $subject);
52
53
        return [$form];
54
    }
55
56
    /**
57
     * @param OriginalForm $form
58
     *
59
     * @return Fieldset
60
     * @throws \Exception
61
     */
62
    private function getFieldSetFromForm(OriginalForm $form)
63
    {
64
        $fieldSet = $form->getElement('base_fieldset');
65
        if (!($fieldSet instanceof Fieldset)) {
66
            throw new \Exception("The Fieldset has changed it's ID");
67
        }
68
69
        return $fieldSet;
70
    }
71
72
    private function addFieldToFieldSet(Fieldset $fieldSet)
73
    {
74
        $attributeCode = IsUsingTwoFactor::ATTRIBUTE_CODE;
75
        $fieldSet->addField(
76
            $attributeCode,
77
            'select',
78
            [
79
                'name' => $attributeCode,
80
                'label' => __('Use Two Factor for this account'),
81
                'title' => __('Use Two Factor for this account'),
82
                'values' => [['value' => '0', 'label' => 'No'], ['value' => '1', 'label' => 'Yes']],
83
                'class' => 'select',
84
            ]
85
        );
86
    }
87
88
    private function updateFormData(OriginalForm $form, OriginalClass $subject)
89
    {
90
        $user = $this->adminUser->getAdminUser();
91
        $user->unsetData('password');
92
        $userData = $user->getData();
93
        unset($userData[$subject::IDENTITY_VERIFICATION_PASSWORD_FIELD]);
94
        $form->setValues($userData);
95
    }
96
}
97