Passed
Push — master ( 0c3c7b...7eea2b )
by Julito
07:39
created

Version20170626122900::up()   F

Complexity

Conditions 23
Paths > 20000

Size

Total Lines 122
Code Lines 74

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 23
eloc 74
c 1
b 1
f 0
nc 2097152
nop 1
dl 0
loc 122
rs 0

How to fix   Long Method    Complexity   

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
/* For licensing terms, see /license.txt */
4
5
namespace Chamilo\CoreBundle\Migrations\Schema\V200;
6
7
use Chamilo\CoreBundle\Migrations\AbstractMigrationChamilo;
8
use Doctrine\DBAL\Schema\Schema;
9
use Symfony\Component\Uid\Uuid;
10
11
/**
12
 * User.
13
 */
14
class Version20170626122900 extends AbstractMigrationChamilo
15
{
16
    public function up(Schema $schema): void
17
    {
18
        $table = $schema->getTable('user');
19
        if (false === $table->hasIndex('idx_user_uid')) {
20
            $this->addSql('DROP INDEX idx_user_uid ON user;');
21
        }
22
        if ($table->hasIndex('UNIQ_8D93D649C05FB297')) {
23
            $this->addSql('DROP INDEX UNIQ_8D93D649C05FB297 ON user;');
24
        }
25
        if ($table->hasIndex('idx_user_uid')) {
26
            $this->addSql('DROP INDEX idx_user_uid ON user');
27
        }
28
29
        if ($table->hasColumn('user_id')) {
30
            $this->addSql('ALTER TABLE user DROP user_id');
31
        }
32
33
        if (false === $table->hasColumn('resource_node_id')) {
34
            $this->addSql('ALTER TABLE user ADD resource_node_id INT DEFAULT NULL;');
35
            $this->addSql(
36
                'ALTER TABLE user ADD CONSTRAINT FK_8D93D6491BAD783F FOREIGN KEY (resource_node_id) REFERENCES resource_node (id) ON DELETE CASCADE;'
37
            );
38
            $this->addSql('CREATE UNIQUE INDEX UNIQ_8D93D6491BAD783F ON user (resource_node_id);');
39
        }
40
41
42
        $this->addSql(
43
            'ALTER TABLE user CHANGE salt salt VARCHAR(255) NOT NULL, CHANGE created_at created_at DATETIME NOT NULL, CHANGE updated_at updated_at DATETIME NOT NULL'
44
        );
45
        $this->addSql(
46
            'ALTER TABLE user CHANGE confirmation_token confirmation_token VARCHAR(255) DEFAULT NULL, CHANGE website website VARCHAR(255) DEFAULT NULL'
47
        );
48
49
        if (false === $table->hasColumn('user_id')) {
50
51
        }
52
        if (false === $table->hasColumn('user_id')) {
53
54
        }
55
56
57
        $em = $this->getEntityManager();
58
59
        if (false === $table->hasColumn('uuid')) {
60
            $this->addSql("ALTER TABLE user ADD uuid BINARY(16) NOT NULL COMMENT '(DC2Type:uuid)'");
61
            $sql = 'SELECT * FROM user';
62
            $result = $em->getConnection()->executeQuery($sql);
63
            $data = $result->fetchAllAssociative();
64
            foreach ($data as $item) {
65
                $uuid = Uuid::v4()->toBinary();
66
                $userId = $item['id'];
67
                $sql = "UPDATE user SET uuid = '$uuid' WHERE id = $userId";
68
                $this->addSql($sql);
69
            }
70
        }
71
72
        if (false === $table->hasIndex('UNIQ_8D93D649D17F50A6')) {
73
            $this->addSql('CREATE UNIQUE INDEX UNIQ_8D93D649D17F50A6 ON user (uuid);');
74
        }
75
76
        if (false === $table->hasColumn('api_token')) {
77
            $this->addSql('ALTER TABLE user ADD api_token VARCHAR(255) DEFAULT NULL');
78
            $this->addSql('CREATE UNIQUE INDEX UNIQ_8D93D6497BA2F5EB ON user (api_token);');
79
        }
80
81
        if (false === $table->hasColumn('date_of_birth')) {
82
            $this->addSql('ALTER TABLE user ADD date_of_birth DATETIME DEFAULT NULL');
83
        }
84
        if (false === $table->hasColumn('website')) {
85
            $this->addSql('ALTER TABLE user ADD website VARCHAR(255) DEFAULT NULL');
86
        }
87
        if (false === $table->hasColumn('biography')) {
88
            $this->addSql('ALTER TABLE user ADD biography LONGTEXT DEFAULT NULL');
89
        }
90
        if (false === $table->hasColumn('gender')) {
91
            $this->addSql('ALTER TABLE user ADD gender VARCHAR(1) DEFAULT NULL');
92
        }
93
        if (false === $table->hasColumn('locale')) {
94
            $this->addSql('ALTER TABLE user ADD locale VARCHAR(8) DEFAULT NULL');
95
        }
96
        if (false === $table->hasColumn('timezone')) {
97
            $this->addSql('ALTER TABLE user ADD timezone VARCHAR(64) NOT NULL');
98
        }
99
100
        if (false === $table->hasColumn('confirmation_token')) {
101
            $this->addSql('ALTER TABLE user ADD confirmation_token VARCHAR(255) DEFAULT NULL');
102
            $this->addSql('CREATE UNIQUE INDEX UNIQ_8D93D649C05FB297 ON user (confirmation_token)');
103
        } else {
104
            $this->addSql('ALTER TABLE user CHANGE confirmation_token confirmation_token VARCHAR(180) DEFAULT NULL');
105
        }
106
107
        $this->addSql('ALTER TABLE user CHANGE username_canonical username_canonical VARCHAR(180) NOT NULL');
108
        $this->addSql('ALTER TABLE user CHANGE lastname lastname VARCHAR(64) DEFAULT NULL');
109
        $this->addSql('ALTER TABLE user CHANGE firstname firstname VARCHAR(64) DEFAULT NULL');
110
        $this->addSql('ALTER TABLE user CHANGE phone phone VARCHAR(64) DEFAULT NULL');
111
        $this->addSql('ALTER TABLE user CHANGE salt salt VARCHAR(255) DEFAULT NULL');
112
        $this->addSql(
113
            'UPDATE user SET created_at = registration_date WHERE CAST(created_at AS CHAR(20)) = "0000-00-00 00:00:00"'
114
        );
115
        $this->addSql(
116
            'UPDATE user SET updated_at = registration_date WHERE CAST(updated_at AS CHAR(20)) = "0000-00-00 00:00:00"'
117
        );
118
119
        $table = $schema->getTable('admin');
120
        $this->addSql('ALTER TABLE admin CHANGE user_id user_id INT DEFAULT NULL');
121
        if (false === $table->hasForeignKey('FK_880E0D76A76ED395')) {
122
            $this->addSql(
123
                'ALTER TABLE admin ADD CONSTRAINT FK_880E0D76A76ED395 FOREIGN KEY (user_id) REFERENCES user (id) ON DELETE CASCADE'
124
            );
125
        }
126
127
        if (false === $table->hasIndex('user_id')) {
128
            $this->addSql('DROP INDEX user_id ON admin');
129
        }
130
131
        if (false === $table->hasIndex('UNIQ_880E0D76A76ED395')) {
132
            $this->addSql('CREATE UNIQUE INDEX UNIQ_880E0D76A76ED395 ON admin (user_id)');
133
        }
134
135
        $table = $schema->getTable('user_course_category');
136
        if (!$table->hasColumn('collapsed')) {
137
            $this->addSql('ALTER TABLE user_course_category ADD collapsed TINYINT(1) DEFAULT NULL');
138
        }
139
    }
140
141
    public function down(Schema $schema): void
142
    {
143
    }
144
145
    public function getDescription(): string
146
    {
147
        return 'User changes';
148
    }
149
}
150