Passed
Push — master ( 1d4eef...487e06 )
by Ross
03:32
created

InstallSchema::install()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 12
rs 9.4285
cc 2
eloc 7
nc 2
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\Setup;
23
24
25
use Magento\Framework\Setup\InstallSchemaInterface;
26
use Magento\Framework\Setup\ModuleContextInterface;
27
use Magento\Framework\Setup\SchemaSetupInterface;
28
29
class InstallSchema implements InstallSchemaInterface
30
{
31
32
    /**
33
     * Installs DB schema for a module
34
     *
35
     * @param SchemaSetupInterface   $setup
36
     * @param ModuleContextInterface $context
37
     *
38
     * @return void
39
     */
40
    public function install(SchemaSetupInterface $setup, ModuleContextInterface $context)
41
    {
42
        $installer = $setup;
43
        $installer->startSetup();
44
        $version = $context->getVersion();
45
46
        if (version_compare($version, '0.0.1') < 0) {
47
            $this->addColumnsToAdminTable($installer);
48
        }
49
50
        $installer->endSetup();
51
    }
52
53
    private function addColumnsToAdminTable(SchemaSetupInterface $installer)
54
    {
55
        $tableAdmins = $installer->getTable('admin_user');
56
57
        $installer->getConnection()->addColumn(
58
            $tableAdmins,
59
            'use_two_factor',
60
            [
61
                'type' => \Magento\Framework\DB\Ddl\Table::TYPE_SMALLINT,
62
                'nullable' => true,
63
                'default' => 0,
64
                'comment' => 'Use Two Factor Authentication',
65
            ]
66
        );
67
68
        $installer->getConnection()->addColumn(
69
            $tableAdmins,
70
            'two_factor_secret',
71
            [
72
                'type' => \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
73
                'nullable' => true,
74
                'comment' => 'Two Factor Secret',
75
            ]
76
        );
77
    }
78
}
79