Completed
Push — master ( b8dae5...46c150 )
by Gabriel
01:13
created

UpgradeSchema::upgrade()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 63
Code Lines 50

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 50
c 1
b 0
f 0
nc 3
nop 2
dl 0
loc 63
rs 9.4347

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
namespace Stockbase\Integration\Setup;
4
5
use Magento\Framework\Setup\UpgradeSchemaInterface;
6
use Magento\Framework\Setup\ModuleContextInterface;
7
use Magento\Framework\Setup\SchemaSetupInterface;
8
use Magento\Framework\DB\Adapter\AdapterInterface;
9
use Magento\Framework\DB\Ddl\Table;
10
11
/**
12
 * Class UpgradeSchema
13
 */
14
class UpgradeSchema implements UpgradeSchemaInterface
15
{
16
17
    /**
18
     * @param SchemaSetupInterface $setup
19
     * @param ModuleContextInterface $context
20
     */
21
    public function upgrade(SchemaSetupInterface $setup, ModuleContextInterface $context)
22
    {
23
        if(version_compare($context->getVersion(), '1.1.0') < 0) {
24
            $tableName = $setup->getTable('stockbase_product_images');
25
            if (!$setup->getConnection()->isTableExists($tableName)) {
26
                $table = $setup->getConnection()
27
                    ->newTable($tableName)
28
                    ->setComment('Stockbase product images')
29
                    ->addColumn(
30
                        'id',
31
                        Table::TYPE_BIGINT,
32
                        null,
33
                        ['identity' => true, 'unsigned' => true, 'nullable' => false, 'primary' => true],
34
                        'Image ID'
35
                    )
36
                    ->addColumn(
37
                        'ean',
38
                        Table::TYPE_BIGINT,
39
                        null,
40
                        ['identity' => false, 'unsigned' => true, 'nullable' => false],
41
                        'International/European Article Number'
42
                    )
43
                    ->addColumn(
44
                        'product_id',
45
                        Table::TYPE_BIGINT,
46
                        null,
47
                        ['identity' => false, 'unsigned' => true, 'nullable' => false],
48
                        'Magento Product ID'
49
                    )
50
                    ->addColumn(
51
                        'image',
52
                        Table::TYPE_TEXT,
53
                        null,
54
                        ['nullable' => false],
55
                        'Image'
56
                    )
57
                    ->addColumn(
58
                        'timestamp',
59
                        Table::TYPE_DATETIME,
60
                        null,
61
                        ['nullable' => false],
62
                        'Latest Sync'
63
                    )
64
                    ->addIndex(
65
                        $setup->getIdxName($tableName, ['product_id']),
66
                        ['product_id'],
67
                        ['type' => AdapterInterface::INDEX_TYPE_INDEX]
68
                    )
69
                    ->addIndex(
70
                        $setup->getIdxName($tableName, ['ean']),
71
                        ['ean'],
72
                        ['type' => AdapterInterface::INDEX_TYPE_INDEX]
73
                    )
74
                    ->addIndex(
75
                        $setup->getIdxName($tableName, ['timestamp']),
76
                        ['timestamp'],
77
                        ['type' => AdapterInterface::INDEX_TYPE_INDEX]
78
                    );
79
                $setup->getConnection()->createTable($table);
80
            }
81
        }
82
83
    }
84
85
}
86