Completed
Push — master ( 4ec14e...eda794 )
by Ross
24:33
created

InstallData::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 9
rs 9.6666
cc 1
eloc 7
nc 1
nop 3
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\Setup;
23
24
use Magento\Customer\Setup\CustomerSetup;
25
use Magento\Customer\Setup\CustomerSetupFactory;
26
use Magento\Eav\Api\AttributeRepositoryInterface;
27
use Magento\Eav\Model\Entity\Attribute\SetFactory as AttributeSetFactory;
28
use Magento\Framework\Setup\InstallDataInterface;
29
use Magento\Framework\Setup\ModuleContextInterface;
30
use Magento\Framework\Setup\ModuleDataSetupInterface;
31
use Rossmitchell\Twofactor\Model\Customer\Attribute\IsUsingTwoFactor;
32
use Rossmitchell\Twofactor\Model\Customer\Attribute\TwoFactorSecret;
33
34
class InstallData implements InstallDataInterface
35
{
36
37
    private $customerSetupFactory;
38
    /**
39
     * @var AttributeSetFactory
40
     */
41
    private $attributeSetFactory;
42
    /**
43
     * @var AttributeRepositoryInterface
44
     */
45
    private $attributeRepository;
46
47
    /**
48
     * Constructor
49
     *
50
     * @param CustomerSetupFactory         $customerSetupFactory
51
     * @param AttributeSetFactory          $attributeSetFactory
52
     * @param AttributeRepositoryInterface $attributeRepository
53
     */
54
    public function __construct(
55
        CustomerSetupFactory $customerSetupFactory,
56
        AttributeSetFactory $attributeSetFactory,
57
        AttributeRepositoryInterface $attributeRepository
58
    ) {
59
        $this->customerSetupFactory = $customerSetupFactory;
60
        $this->attributeSetFactory  = $attributeSetFactory;
61
        $this->attributeRepository  = $attributeRepository;
62
    }
63
64
    /**
65
     * {@inheritdoc}
66
     */
67
    public function install(
0 ignored issues
show
Unused Code introduced by
The method parameter $context is never used
Loading history...
68
        ModuleDataSetupInterface $setup,
69
        ModuleContextInterface $context
70
    ) {
71
        /** @var CustomerSetup $customerSetup */
72
        $customerSetup  = $this->customerSetupFactory->create(['setup' => $setup]);
73
        $customerEntity = $customerSetup->getEavConfig()->getEntityType('customer');
74
        $attributeSetId = $customerEntity->getDefaultAttributeSetId();
75
76
        /** @var $attributeSet AttributeSet */
77
        $attributeSet     = $this->attributeSetFactory->create();
78
        $attributeGroupId = $attributeSet->getDefaultGroupId($attributeSetId);
79
80
        $useTwoFactorCode = IsUsingTwoFactor::ATTRIBUTE_CODE;
81
82
        $customerSetup->addAttribute(
83
            'customer',
84
            $useTwoFactorCode,
85
            [
86
                'type' => 'int',
87
                'label' => $useTwoFactorCode,
88
                'input' => 'boolean',
89
                'source' => '',
90
                'required' => true,
91
                'visible' => true,
92
                'position' => 333,
93
                'system' => false,
94
                'backend' => '',
95
            ]
96
        );
97
98
        $attribute = $customerSetup->getEavConfig()->getAttribute('customer', $useTwoFactorCode)->addData(
99
            [
100
                'attribute_set_id' => $attributeSetId,
101
                'attribute_group_id' => $attributeGroupId,
102
                'used_in_forms' => [
103
                    'adminhtml_customer',
104
                    'customer_account_create',
105
                    'customer_account_edit',
106
                ],
107
            ]
108
        );
109
        $attribute->save();
110
111
        $secretCode = TwoFactorSecret::ATTRIBUTE_CODE;
112
113
        $customerSetup->addAttribute(
114
            'customer',
115
            $secretCode,
116
            [
117
                'type' => 'varchar',
118
                'label' => $secretCode,
119
                'input' => 'text',
120
                'source' => '',
121
                'required' => false,
122
                'visible' => false,
123
                'position' => 334,
124
                'system' => false,
125
                'backend' => '',
126
            ]
127
        );
128
    }
129
}
130