InstallSchema   A
last analyzed

Complexity

Total Complexity 1

Size/Duplication

Total Lines 148
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 0
loc 148
c 0
b 0
f 0
wmc 1
lcom 0
cbo 0
ccs 0
cts 128
cp 0
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
B install() 0 141 1
1
<?php
2
/**
3
 * File: InstallSchema.php
4
 *
5
 * @author      Maciej Sławik <[email protected]>
6
 * Github:      https://github.com/maciejslawik
7
 */
8
9
namespace MSlwk\ICatalogue\Setup;
10
11
use Magento\Framework\DB\Ddl\Table;
12
use Magento\Framework\Setup\InstallSchemaInterface;
13
use Magento\Framework\Setup\SchemaSetupInterface;
14
use Magento\Framework\Setup\ModuleContextInterface;
15
use MSlwk\ICatalogue\Model\ResourceModel\Catalogue;
16
use MSlwk\ICatalogue\Api\CatalogueInterface;
17
use MSlwk\ICatalogue\Api\Catalogue\ImageInterface;
18
use MSlwk\ICatalogue\Api\Catalogue\StoreInterface;
19
20
/**
21
 * Class InstallSchema
22
 *
23
 * @package MSlwk\ICatalogue\Setup
24
 */
25
class InstallSchema implements InstallSchemaInterface
26
{
27
    /**
28
     * @param SchemaSetupInterface $setup
29
     * @param ModuleContextInterface $context
30
     */
31
    public function install(SchemaSetupInterface $setup, ModuleContextInterface $context)
32
    {
33
        $setup->startSetup();
34
35
        /**
36
         * Create catalogue table
37
         */
38
        $catalogueTable = $setup
39
            ->getConnection()
40
            ->newTable($setup->getTable(Catalogue::CATALOGUE_TABLE))
41
            ->addColumn(
42
                CatalogueInterface::CATALOGUE_ID,
43
                Table::TYPE_INTEGER,
44
                null,
45
                ['identity' => true, 'unsigned' => true, 'nullable' => false, 'primary' => true],
46
                'Catalogue ID'
47
            )->addColumn(
48
                CatalogueInterface::TITLE,
49
                Table::TYPE_TEXT,
50
                255,
51
                [],
52
                'Title'
53
            )->addColumn(
54
                CatalogueInterface::CREATED_AT,
55
                Table::TYPE_TIMESTAMP,
56
                null,
57
                ['nullable' => false, 'default' => Table::TIMESTAMP_INIT],
58
                'Created At'
59
            )->addColumn(
60
                CatalogueInterface::UPDATED_AT,
61
                Table::TYPE_TIMESTAMP,
62
                null,
63
                ['nullable' => false, 'default' => Table::TIMESTAMP_INIT_UPDATE],
64
                'Updated At'
65
            )->setComment(
66
                'Table for storing data about interactive catalogues'
67
            );
68
        $setup->getConnection()->createTable($catalogueTable);
69
70
        /**
71
         * Create store table
72
         */
73
        $catalogueStoreTable = $setup
74
            ->getConnection()
75
            ->newTable($setup->getTable(Catalogue::CATALOGUE_STORE_TABLE))
76
            ->addColumn(
77
                CatalogueInterface::CATALOGUE_ID,
78
                Table::TYPE_INTEGER,
79
                null,
80
                ['unsigned' => true, 'nullable' => false],
81
                'Catalogue ID'
82
            )->addColumn(
83
                StoreInterface::STORE_ID,
84
                Table::TYPE_SMALLINT,
85
                5,
86
                ['unsigned' => true, 'nullable' => false],
87
                'Store ID'
88
            )->addForeignKey(
89
                $setup->getFkName(
90
                    Catalogue::CATALOGUE_STORE_TABLE,
91
                    CatalogueInterface::CATALOGUE_ID,
92
                    Catalogue::CATALOGUE_TABLE,
93
                    CatalogueInterface::CATALOGUE_ID
94
                ),
95
                CatalogueInterface::CATALOGUE_ID,
96
                Catalogue::CATALOGUE_TABLE,
97
                CatalogueInterface::CATALOGUE_ID,
98
                Table::ACTION_CASCADE,
99
                Table::ACTION_CASCADE
100
            )->addForeignKey(
101
                $setup->getFkName(
102
                    Catalogue::CATALOGUE_STORE_TABLE,
103
                    StoreInterface::STORE_ID,
104
                    'store',
105
                    'store_id'
106
                ),
107
                StoreInterface::STORE_ID,
108
                'store',
109
                'store_id',
110
                Table::ACTION_CASCADE,
111
                Table::ACTION_CASCADE
112
            )->setComment(
113
                'Table for assigning catalogues to website stores'
114
            );
115
        $setup->getConnection()->createTable($catalogueStoreTable);
116
117
        /**
118
         * Create image table
119
         */
120
        $catalogueImageTable = $setup
121
            ->getConnection()
122
            ->newTable($setup->getTable(Catalogue::CATALOGUE_IMAGE_TABLE))
123
            ->addColumn(
124
                ImageInterface::IMAGE_ID,
125
                Table::TYPE_INTEGER,
126
                null,
127
                ['identity' => true, 'unsigned' => true, 'nullable' => false, 'primary' => true],
128
                'Image ID'
129
            )->addColumn(
130
                CatalogueInterface::CATALOGUE_ID,
131
                Table::TYPE_INTEGER,
132
                null,
133
                ['unsigned' => true, 'nullable' => false],
134
                'Catalogue ID'
135
            )->addColumn(
136
                ImageInterface::IMAGE_URI,
137
                Table::TYPE_TEXT,
138
                0,
139
                [],
140
                'Image URI'
141
            )->addColumn(
142
                ImageInterface::SORT_ORDER,
143
                Table::TYPE_INTEGER,
144
                null,
145
                ['default' => 0, 'unsigned' => true, 'nullable' => false],
146
                'Sort order'
147
            )->addColumn(
148
                ImageInterface::CREATED_AT,
149
                Table::TYPE_TIMESTAMP,
150
                null,
151
                ['nullable' => false, 'default' => Table::TIMESTAMP_INIT],
152
                'Created At'
153
            )->addForeignKey(
154
                $setup->getFkName(
155
                    Catalogue::CATALOGUE_IMAGE_TABLE,
156
                    CatalogueInterface::CATALOGUE_ID,
157
                    Catalogue::CATALOGUE_TABLE,
158
                    CatalogueInterface::CATALOGUE_ID
159
                ),
160
                CatalogueInterface::CATALOGUE_ID,
161
                Catalogue::CATALOGUE_TABLE,
162
                CatalogueInterface::CATALOGUE_ID,
163
                Table::ACTION_CASCADE,
164
                Table::ACTION_CASCADE
165
            )->setComment(
166
                'Table for storing images assigned to catalogues'
167
            );
168
        $setup->getConnection()->createTable($catalogueImageTable);
169
170
        $setup->endSetup();
171
    }
172
}
173