InstallData::install()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 68
Code Lines 47

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 68
rs 9.2447
cc 1
eloc 47
nc 1
nop 2

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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