Completed
Push — develop ( a256c6...c8d690 )
by Mario
12:04
created

Version0_13_0   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 269
Duplicated Lines 6.69 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 8
dl 18
loc 269
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
B up() 18 220 7
A down() 0 33 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace AppBundle\Migrations;
4
5
use Doctrine\DBAL\Migrations\AbstractMigration;
6
use Doctrine\DBAL\Schema\Schema;
7
use Ds\Component\Container\Attribute;
8
use Ramsey\Uuid\Uuid;
9
use stdClass;
10
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
11
use Symfony\Component\Yaml\Yaml;
12
13
/**
14
 * Class Version0_13_0
15
 */
16
class Version0_13_0 extends AbstractMigration implements ContainerAwareInterface
17
{
18
    use Attribute\Container;
19
20
    /**
21
     * Up
22
     *
23
     * @param \Doctrine\DBAL\Schema\Schema $schema
24
     */
25
    public function up(Schema $schema)
26
    {
27
        $platform = $this->connection->getDatabasePlatform()->getName();
28
        $cipherService = $this->container->get('ds_encryption.service.cipher');
29
30
        switch ($platform) {
31
            case 'postgresql':
32
                // Schema
33
                $this->addSql('CREATE SEQUENCE ds_config_id_seq INCREMENT BY 1 MINVALUE 1 START 8');
34
                $this->addSql('CREATE SEQUENCE ds_parameter_id_seq INCREMENT BY 1 MINVALUE 1 START 4');
35
                $this->addSql('CREATE SEQUENCE ds_metadata_id_seq INCREMENT BY 1 MINVALUE 1 START 1');
36
                $this->addSql('CREATE SEQUENCE ds_metadata_trans_id_seq INCREMENT BY 1 MINVALUE 1 START 1');
37
                $this->addSql('CREATE SEQUENCE ds_access_id_seq INCREMENT BY 1 MINVALUE 1 START 3');
38
                $this->addSql('CREATE SEQUENCE ds_access_permission_id_seq INCREMENT BY 1 MINVALUE 1 START 7');
39
                $this->addSql('CREATE SEQUENCE ds_tenant_id_seq INCREMENT BY 1 MINVALUE 1 START 2');
40
                $this->addSql('CREATE SEQUENCE app_form_id_seq INCREMENT BY 1 MINVALUE 1 START 1');
41
                $this->addSql('CREATE TABLE ds_config (id INT NOT NULL, uuid UUID NOT NULL, "owner" VARCHAR(255) DEFAULT NULL, owner_uuid UUID DEFAULT NULL, "key" VARCHAR(255) NOT NULL, value TEXT DEFAULT NULL, version INT DEFAULT 1 NOT NULL, tenant UUID NOT NULL, created_at TIMESTAMP(0) WITHOUT TIME ZONE DEFAULT NULL, updated_at TIMESTAMP(0) WITHOUT TIME ZONE DEFAULT NULL, PRIMARY KEY(id))');
42
                $this->addSql('CREATE UNIQUE INDEX UNIQ_758C45F4D17F50A6 ON ds_config (uuid)');
43
                $this->addSql('CREATE UNIQUE INDEX UNIQ_758C45F48A90ABA94E59C462 ON ds_config (key, tenant)');
44
                $this->addSql('CREATE TABLE ds_parameter (id INT NOT NULL, "key" VARCHAR(255) NOT NULL, value TEXT DEFAULT NULL, PRIMARY KEY(id))');
45
                $this->addSql('CREATE UNIQUE INDEX UNIQ_B3C0FD91F48571EB ON ds_parameter ("key")');
46
                $this->addSql('CREATE TABLE ds_metadata (id INT NOT NULL, uuid UUID NOT NULL, "owner" VARCHAR(255) DEFAULT NULL, owner_uuid UUID DEFAULT NULL, slug VARCHAR(255) NOT NULL, type VARCHAR(255) DEFAULT NULL, data JSON NOT NULL, version INT DEFAULT 1 NOT NULL, tenant UUID NOT NULL, deleted_at TIMESTAMP(0) WITHOUT TIME ZONE DEFAULT NULL, created_at TIMESTAMP(0) WITHOUT TIME ZONE DEFAULT NULL, updated_at TIMESTAMP(0) WITHOUT TIME ZONE DEFAULT NULL, PRIMARY KEY(id))');
47
                $this->addSql('CREATE UNIQUE INDEX UNIQ_11290F17D17F50A6 ON ds_metadata (uuid)');
48
                $this->addSql('CREATE UNIQUE INDEX UNIQ_11290F17989D9B624E59C462 ON ds_metadata (slug, tenant)');
49
                $this->addSql('CREATE TABLE ds_metadata_trans (id INT NOT NULL, translatable_id INT DEFAULT NULL, title VARCHAR(255) NOT NULL, locale VARCHAR(255) NOT NULL, PRIMARY KEY(id))');
50
                $this->addSql('CREATE INDEX IDX_A6447E202C2AC5D3 ON ds_metadata_trans (translatable_id)');
51
                $this->addSql('CREATE UNIQUE INDEX ds_metadata_trans_unique_translation ON ds_metadata_trans (translatable_id, locale)');
52
                $this->addSql('CREATE TABLE ds_access (id INT NOT NULL, uuid UUID NOT NULL, "owner" VARCHAR(255) DEFAULT NULL, owner_uuid UUID DEFAULT NULL, assignee VARCHAR(255) DEFAULT NULL, assignee_uuid UUID DEFAULT NULL, version INT DEFAULT 1 NOT NULL, tenant UUID NOT NULL, created_at TIMESTAMP(0) WITHOUT TIME ZONE DEFAULT NULL, updated_at TIMESTAMP(0) WITHOUT TIME ZONE DEFAULT NULL, PRIMARY KEY(id))');
53
                $this->addSql('CREATE UNIQUE INDEX UNIQ_A76F41DCD17F50A6 ON ds_access (uuid)');
54
                $this->addSql('CREATE TABLE ds_access_permission (id INT NOT NULL, access_id INT DEFAULT NULL, scope VARCHAR(255) DEFAULT NULL, entity VARCHAR(255) DEFAULT NULL, entity_uuid UUID DEFAULT NULL, "key" VARCHAR(255) NOT NULL, attributes JSON NOT NULL, tenant UUID NOT NULL, PRIMARY KEY(id))');
55
                $this->addSql('CREATE INDEX IDX_D46DD4D04FEA67CF ON ds_access_permission (access_id)');
56
                $this->addSql('CREATE TABLE ds_tenant (id INT NOT NULL, uuid UUID NOT NULL, data JSON NOT NULL, version INT DEFAULT 1 NOT NULL, created_at TIMESTAMP(0) WITHOUT TIME ZONE DEFAULT NULL, updated_at TIMESTAMP(0) WITHOUT TIME ZONE DEFAULT NULL, PRIMARY KEY(id))');
57
                $this->addSql('CREATE UNIQUE INDEX UNIQ_EF5FAEEAD17F50A6 ON ds_tenant (uuid)');
58
                $this->addSql('CREATE TABLE app_form (id INT NOT NULL, uuid UUID NOT NULL, "owner" VARCHAR(255) DEFAULT NULL, owner_uuid UUID DEFAULT NULL, type VARCHAR(255) DEFAULT NULL, config JSON NOT NULL, version INT DEFAULT 1 NOT NULL, tenant UUID NOT NULL, deleted_at TIMESTAMP(0) WITHOUT TIME ZONE DEFAULT NULL, created_at TIMESTAMP(0) WITHOUT TIME ZONE DEFAULT NULL, updated_at TIMESTAMP(0) WITHOUT TIME ZONE DEFAULT NULL, PRIMARY KEY(id))');
59
                $this->addSql('CREATE UNIQUE INDEX UNIQ_57A6D8EFD17F50A6 ON app_form (uuid)');
60
                $this->addSql('ALTER TABLE ds_metadata_trans ADD CONSTRAINT FK_A6447E202C2AC5D3 FOREIGN KEY (translatable_id) REFERENCES ds_metadata (id) ON DELETE CASCADE NOT DEFERRABLE INITIALLY IMMEDIATE');
61
                $this->addSql('ALTER TABLE ds_access_permission ADD CONSTRAINT FK_D46DD4D04FEA67CF FOREIGN KEY (access_id) REFERENCES ds_access (id) NOT DEFERRABLE INITIALLY IMMEDIATE');
62
                $this->addSql('CREATE TABLE ds_session (id VARCHAR(128) NOT NULL PRIMARY KEY, data BYTEA NOT NULL, time INTEGER NOT NULL, lifetime INTEGER NOT NULL)');
63
64
                // Data
65
                $yml = file_get_contents('/srv/api-platform/src/AppBundle/Resources/migrations/1_0_0.yml');
66
                $data = Yaml::parse($yml);
67
                $i = 0;
68
                $parameters = [
69
                    [
70
                        'key' => 'ds_system.user.username',
71
                        'value' => serialize($data['system']['username'])
72
                    ],
73
                    [
74
                        'key' => 'ds_system.user.password',
75
                        'value' => $cipherService->encrypt($data['system']['password'])
76
                    ],
77
                    [
78
                        'key' => 'ds_tenant.tenant.default',
79
                        'value' => serialize($data['tenant']['uuid'])
80
                    ]
81
                ];
82
83 View Code Duplication
                foreach ($parameters as $parameter) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
84
                    $this->addSql(sprintf(
85
                        'INSERT INTO ds_parameter (id, key, value) VALUES (%d, %s, %s);',
86
                        ++$i,
87
                        $this->connection->quote($parameter['key']),
88
                        $this->connection->quote($parameter['value'])
89
                    ));
90
                }
91
92
                $i = 0;
93
                $tenants = [
94
                    [
95
                        'uuid' => $data['tenant']['uuid'],
96
                        'data' => '"'.$cipherService->encrypt(new stdClass).'"'
97
                    ]
98
                ];
99
100 View Code Duplication
                foreach ($tenants as $tenant) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
101
                    $this->addSql(sprintf(
102
                        'INSERT INTO ds_tenant (id, uuid, data, created_at, updated_at) VALUES (%d, %s, %s, %s, %s);',
103
                        ++$i,
104
                        $this->connection->quote($tenant['uuid']),
105
                        $this->connection->quote($tenant['data']),
106
                        'now()',
107
                        'now()'
108
                    ));
109
                }
110
111
                $i = 0;
112
                $configs = [
113
                    [
114
                        'key' => 'ds_api.user.username',
115
                        'value' => serialize($data['user']['system']['username'])
116
                    ],
117
                    [
118
                        'key' => 'ds_api.user.password',
119
                        'value' => $cipherService->encrypt($data['user']['system']['password'])
120
                    ],
121
                    [
122
                        'key' => 'ds_api.user.uuid',
123
                        'value' => serialize($data['user']['system']['uuid'])
124
                    ],
125
                    [
126
                        'key' => 'ds_api.user.roles',
127
                        'value' => serialize([])
128
                    ],
129
                    [
130
                        'key' => 'ds_api.user.identity.roles',
131
                        'value' => serialize([])
132
                    ],
133
                    [
134
                        'key' => 'ds_api.user.identity.type',
135
                        'value' => serialize('System')
136
                    ],
137
                    [
138
                        'key' => 'ds_api.user.identity.uuid',
139
                        'value' => serialize($data['identity']['system']['uuid'])
140
                    ],
141
                    [
142
                        'key' => 'ds_api.user.tenant',
143
                        'value' => serialize($data['tenant']['uuid'])
144
                    ]
145
                ];
146
147
                foreach ($configs as $config) {
148
                    $this->addSql(sprintf(
149
                        'INSERT INTO ds_config (id, uuid, owner, owner_uuid, key, value, version, tenant, created_at, updated_at) VALUES (%d, %s, %s, %s, %s, %s, %d, %s, %s, %s);',
150
                        ++$i,
151
                        $this->connection->quote(Uuid::uuid4()->toString()),
152
                        $this->connection->quote('BusinessUnit'),
153
                        $this->connection->quote($data['business_unit']['administration']['uuid']),
154
                        $this->connection->quote($config['key']),
155
                        $this->connection->quote($config['value']),
156
                        1,
157
                        $this->connection->quote($data['tenant']['uuid']),
158
                        'now()',
159
                        'now()'
160
                    ));
161
                }
162
163
                $i = 0;
164
                $j = 0;
165
                $accesses = [
166
                    [
167
                        'owner' => 'System',
168
                        'owner_uuid' => $data['identity']['system']['uuid'],
169
                        'assignee' => 'System',
170
                        'assignee_uuid' => $data['identity']['system']['uuid'],
171
                        'permissions' => [
172
                            [
173
                                'key' => 'entity',
174
                                'attributes' => '["BROWSE","READ","EDIT","ADD","DELETE"]'
175
                            ],
176
                            [
177
                                'key' => 'property',
178
                                'attributes' => '["BROWSE","READ","EDIT"]'
179
                            ],
180
                            [
181
                                'key' => 'generic',
182
                                'attributes' => '["BROWSE","READ","EDIT","ADD","DELETE","EXECUTE"]'
183
                            ]
184
                        ]
185
                    ],
186
                    [
187
                        'owner' => 'BusinessUnit',
188
                        'owner_uuid' => $data['business_unit']['administration']['uuid'],
189
                        'assignee' => 'Staff',
190
                        'assignee_uuid' => $data['identity']['admin']['uuid'],
191
                        'permissions' => [
192
                            [
193
                                'key' => 'entity',
194
                                'attributes' => '["BROWSE","READ","EDIT","ADD","DELETE"]'
195
                            ],
196
                            [
197
                                'key' => 'property',
198
                                'attributes' => '["BROWSE","READ","EDIT"]'
199
                            ],
200
                            [
201
                                'key' => 'generic',
202
                                'attributes' => '["BROWSE","READ","EDIT","ADD","DELETE","EXECUTE"]'
203
                            ]
204
                        ]
205
                    ]
206
                ];
207
208
                foreach ($accesses as $access) {
209
                    $this->addSql(sprintf(
210
                        'INSERT INTO ds_access (id, uuid, owner, owner_uuid, assignee, assignee_uuid, version, tenant, created_at, updated_at) VALUES (%d, %s, %s, %s, %s, %s, %d, %s, %s, %s);',
211
                        ++$i,
212
                        $this->connection->quote(Uuid::uuid4()->toString()),
213
                        $this->connection->quote($access['owner']),
214
                        $this->connection->quote($access['owner_uuid']),
215
                        $this->connection->quote($access['assignee']),
216
                        $this->connection->quote($access['assignee_uuid']),
217
                        1,
218
                        $this->connection->quote($data['tenant']['uuid']),
219
                        'now()',
220
                        'now()'
221
                    ));
222
223
                    foreach ($access['permissions'] as $permission) {
224
                        $this->addSql(sprintf(
225
                            'INSERT INTO ds_access_permission (id, access_id, scope, entity, entity_uuid, key, attributes, tenant) VALUES (%d, %d, %s, %s, %s, %s, %s, %s);',
226
                            ++$j,
227
                            $i,
228
                            $this->connection->quote('generic'),
229
                            'NULL',
230
                            'NULL',
231
                            $this->connection->quote($permission['key']),
232
                            $this->connection->quote($permission['attributes']),
233
                            $this->connection->quote($data['tenant']['uuid'])
234
                        ));
235
                    }
236
                }
237
238
                break;
239
240
            default:
241
                $this->abortIf(true,'Migration cannot be executed on "'.$platform.'".');
242
                break;
243
        }
244
    }
245
246
    /**
247
     * Down
248
     *
249
     * @param \Doctrine\DBAL\Schema\Schema $schema
250
     */
251
    public function down(Schema $schema)
252
    {
253
        $platform = $this->connection->getDatabasePlatform()->getName();
254
255
        switch ($platform) {
256
            case 'postgresql':
257
                // Schema
258
                $this->addSql('ALTER TABLE ds_metadata_trans DROP CONSTRAINT FK_A6447E202C2AC5D3');
259
                $this->addSql('ALTER TABLE ds_access_permission DROP CONSTRAINT FK_D46DD4D04FEA67CF');
260
                $this->addSql('DROP SEQUENCE ds_config_id_seq CASCADE');
261
                $this->addSql('DROP SEQUENCE ds_parameter_id_seq CASCADE');
262
                $this->addSql('DROP SEQUENCE ds_metadata_id_seq CASCADE');
263
                $this->addSql('DROP SEQUENCE ds_metadata_trans_id_seq CASCADE');
264
                $this->addSql('DROP SEQUENCE ds_access_id_seq CASCADE');
265
                $this->addSql('DROP SEQUENCE ds_access_permission_id_seq CASCADE');
266
                $this->addSql('DROP SEQUENCE ds_tenant_id_seq CASCADE');
267
                $this->addSql('DROP SEQUENCE app_form_id_seq CASCADE');
268
                $this->addSql('DROP TABLE ds_config');
269
                $this->addSql('DROP TABLE ds_parameter');
270
                $this->addSql('DROP TABLE ds_metadata');
271
                $this->addSql('DROP TABLE ds_metadata_trans');
272
                $this->addSql('DROP TABLE ds_access');
273
                $this->addSql('DROP TABLE ds_access_permission');
274
                $this->addSql('DROP TABLE ds_tenant');
275
                $this->addSql('DROP TABLE app_form');
276
                $this->addSql('DROP TABLE ds_session');
277
                break;
278
279
            default:
280
                $this->abortIf(true,'Migration cannot be executed on "'.$platform.'".');
281
                break;
282
        }
283
    }
284
}
285