Passed
Push — master ( a63648...bd508d )
by Julito
10:03
created

Version20211005153900::up()   D

Complexity

Conditions 10
Paths 512

Size

Total Lines 60
Code Lines 33

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 10
eloc 33
c 2
b 0
f 0
nc 512
nop 1
dl 0
loc 60
rs 4.1777

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
declare(strict_types=1);
4
5
namespace Chamilo\CoreBundle\Migrations\Schema\V200;
6
7
use Chamilo\CoreBundle\Migrations\AbstractMigrationChamilo;
8
use Doctrine\DBAL\Schema\Schema;
9
10
class Version20211005153900 extends AbstractMigrationChamilo
11
{
12
    public function getDescription(): string
13
    {
14
        return 'ticket changes';
15
    }
16
17
    public function up(Schema $schema): void
18
    {
19
        // Assigned users
20
        $table = $schema->getTable('ticket_ticket');
21
        $this->addSql(
22
            'UPDATE ticket_ticket SET assigned_last_user = NULL WHERE assigned_last_user NOT IN (SELECT id FROM user)'
23
        );
24
25
        $this->addSql('UPDATE ticket_ticket SET session_id = NULL WHERE session_id = 0');
26
        $this->addSql('UPDATE ticket_ticket SET course_id = NULL WHERE course_id = 0');
27
28
        $this->addSql('DELETE FROM ticket_ticket WHERE session_id IS NOT NULL AND session_id NOT IN (SELECT id FROM session)');
29
        $this->addSql('DELETE FROM ticket_ticket WHERE course_id IS NOT NULL AND course_id NOT IN (SELECT id FROM course)');
30
31
        if (!$table->hasForeignKey('FK_EDE2C7686219A7B7')) {
32
            $this->addSql(
33
                'ALTER TABLE ticket_ticket ADD CONSTRAINT FK_EDE2C7686219A7B7 FOREIGN KEY (assigned_last_user) REFERENCES user (id) ON DELETE CASCADE'
34
            );
35
        }
36
37
        if (!$table->hasForeignKey('FK_EDE2C768591CC992')) {
38
            $this->addSql('ALTER TABLE ticket_ticket ADD CONSTRAINT FK_EDE2C768591CC992 FOREIGN KEY (course_id) REFERENCES course (id) ON DELETE CASCADE;');
39
        }
40
41
        if (!$table->hasForeignKey('FK_EDE2C768613FECDF')) {
42
            $this->addSql('ALTER TABLE ticket_ticket ADD CONSTRAINT FK_EDE2C768613FECDF FOREIGN KEY (session_id) REFERENCES session (id) ON DELETE CASCADE;');
43
        }
44
45
        if (false === $table->hasIndex('IDX_EDE2C7686219A7B7')) {
46
            $this->addSql('CREATE INDEX IDX_EDE2C7686219A7B7 ON ticket_ticket (assigned_last_user)');
47
        }
48
49
        $table = $schema->getTable('ticket_assigned_log');
50
        if (!$table->hasForeignKey('FK_54B65868700047D2')) {
51
            $this->addSql('ALTER TABLE ticket_assigned_log ADD CONSTRAINT FK_54B65868700047D2 FOREIGN KEY (ticket_id) REFERENCES ticket_ticket (id) ON DELETE CASCADE;');
52
        }
53
54
        $table = $schema->getTable('ticket_message');
55
        if (!$table->hasForeignKey('FK_BA71692D700047D2')) {
56
            $this->addSql('ALTER TABLE ticket_message ADD CONSTRAINT FK_BA71692D700047D2 FOREIGN KEY (ticket_id) REFERENCES ticket_ticket (id) ON DELETE CASCADE;');
57
        }
58
59
        // Attachments
60
        $table = $schema->getTable('ticket_message_attachments');
61
62
        if (!$table->hasForeignKey('FK_70BF9E26537A1329')) {
63
            $this->addSql('ALTER TABLE ticket_message_attachments ADD CONSTRAINT FK_70BF9E26537A1329 FOREIGN KEY (message_id) REFERENCES ticket_message (id) ON DELETE CASCADE;');
64
        }
65
66
        if (!$table->hasForeignKey('FK_70BF9E26700047D2')) {
67
            $this->addSql('ALTER TABLE ticket_message_attachments ADD CONSTRAINT FK_70BF9E26700047D2 FOREIGN KEY (ticket_id) REFERENCES ticket_ticket (id) ON DELETE CASCADE;');
68
        }
69
70
        if (!$table->hasColumn('resource_node_id')) {
71
            $this->addSql('ALTER TABLE ticket_message_attachments ADD resource_node_id BIGINT DEFAULT NULL;');
72
            $this->addSql(
73
                'ALTER TABLE ticket_message_attachments ADD CONSTRAINT FK_70BF9E261BAD783F FOREIGN KEY (resource_node_id) REFERENCES resource_node (id) ON DELETE CASCADE;'
74
            );
75
            $this->addSql(
76
                'CREATE UNIQUE INDEX UNIQ_70BF9E261BAD783F ON ticket_message_attachments (resource_node_id);'
77
            );
78
        }
79
    }
80
}
81