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

InstallSchema   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 4
c 3
b 0
f 0
lcom 0
cbo 3
dl 0
loc 53
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A install() 0 14 2
A addUseTwoFactorColumn() 0 13 1
A addTwoFactorSecretColumn() 0 12 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\Setup;
23
24
use Magento\Framework\Setup\InstallSchemaInterface;
25
use Magento\Framework\Setup\ModuleContextInterface;
26
use Magento\Framework\Setup\SchemaSetupInterface;
27
use Rossmitchell\Twofactor\Model\Admin\Attribute\IsUsingTwoFactor;
28
use Rossmitchell\Twofactor\Model\Admin\Attribute\TwoFactorSecret;
29
30
class InstallSchema implements InstallSchemaInterface
31
{
32
33
    /**
34
     * Installs DB schema for a module
35
     *
36
     * @param SchemaSetupInterface   $setup
37
     * @param ModuleContextInterface $context
38
     *
39
     * @return void
40
     */
41
    public function install(SchemaSetupInterface $setup, ModuleContextInterface $context)
42
    {
43
        $installer = $setup;
44
        $installer->startSetup();
45
        $version = $context->getVersion();
46
47
        if (version_compare($version, '0.0.1') < 0) {
48
            $adminTable = $installer->getTable('admin_user');
49
            $this->addUseTwoFactorColumn($installer, $adminTable);
50
            $this->addTwoFactorSecretColumn($installer, $adminTable);
51
        }
52
53
        $installer->endSetup();
54
    }
55
56
    private function addUseTwoFactorColumn(SchemaSetupInterface $installer, $table)
57
    {
58
        $installer->getConnection()->addColumn(
59
            $table,
60
            IsUsingTwoFactor::ATTRIBUTE_CODE,
61
            [
62
                'type' => \Magento\Framework\DB\Ddl\Table::TYPE_SMALLINT,
63
                'nullable' => true,
64
                'default' => 0,
65
                'comment' => 'Use Two Factor Authentication',
66
            ]
67
        );
68
    }
69
70
    private function addTwoFactorSecretColumn(SchemaSetupInterface $installer, $table)
71
    {
72
        $installer->getConnection()->addColumn(
73
            $table,
74
            TwoFactorSecret::ATTRIBUTE_CODE,
75
            [
76
                'type' => \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
77
                'nullable' => true,
78
                'comment' => 'Two Factor Secret',
79
            ]
80
        );
81
    }
82
}
83