Version20201210100001::up()   F
last analyzed

Complexity

Conditions 12
Paths 459

Size

Total Lines 59
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 12
eloc 25
nc 459
nop 1
dl 0
loc 59
rs 3.5513
c 0
b 0
f 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
declare(strict_types=1);
4
5
/* For licensing terms, see /license.txt */
6
7
namespace Chamilo\CoreBundle\Migrations\Schema\V200;
8
9
use Chamilo\CoreBundle\Migrations\AbstractMigrationChamilo;
10
use Doctrine\DBAL\Schema\Schema;
11
12
final class Version20201210100001 extends AbstractMigrationChamilo
13
{
14
    public function getDescription(): string
15
    {
16
        return 'Add new fields to session and c_lp tables for handling reinscription and session repetition logic, and insert new settings if not exist.';
17
    }
18
19
    public function up(Schema $schema): void
20
    {
21
        $schemaManager = $this->connection->createSchemaManager();
22
23
        // Add 'new_subscription_session_id' to the 'session_rel_user' table
24
        if ($schemaManager->tablesExist('session_rel_user')) {
25
            $sessionRelUserTable = $schemaManager->listTableColumns('session_rel_user');
26
27
            if (!isset($sessionRelUserTable['new_subscription_session_id'])) {
28
                $this->addSql('ALTER TABLE session_rel_user ADD new_subscription_session_id INT DEFAULT NULL');
29
            }
30
        }
31
32
        // Add fields to the 'session' table
33
        if ($schemaManager->tablesExist('session')) {
34
            $sessionTable = $schemaManager->listTableColumns('session');
35
36
            if (!isset($sessionTable['parent_id'])) {
37
                $this->addSql('ALTER TABLE session ADD parent_id INT DEFAULT NULL');
38
            }
39
            if (!isset($sessionTable['days_to_reinscription'])) {
40
                $this->addSql('ALTER TABLE session ADD days_to_reinscription INT DEFAULT NULL');
41
            }
42
            if (!isset($sessionTable['last_repetition'])) {
43
                $this->addSql('ALTER TABLE session ADD last_repetition TINYINT(1) DEFAULT 0 NOT NULL');
44
            }
45
            if (!isset($sessionTable['days_to_new_repetition'])) {
46
                $this->addSql('ALTER TABLE session ADD days_to_new_repetition INT DEFAULT NULL');
47
            }
48
        }
49
50
        // Add 'validity_in_days' to the 'session' table
51
        if ($schemaManager->tablesExist('session')) {
52
            $sessionTable = $schemaManager->listTableColumns('session');
53
54
            if (!isset($sessionTable['validity_in_days'])) {
55
                $this->addSql('ALTER TABLE session ADD validity_in_days INT DEFAULT NULL');
56
            }
57
        }
58
59
        // Remove 'validity_in_days' from the 'c_lp' table
60
        if ($schemaManager->tablesExist('c_lp')) {
61
            $clpTable = $schemaManager->listTableColumns('c_lp');
62
63
            if (isset($clpTable['validity_in_days'])) {
64
                $this->addSql('ALTER TABLE c_lp DROP COLUMN validity_in_days');
65
            }
66
        }
67
68
        // Insert new settings if not exist
69
        $this->addSql("
70
            INSERT INTO settings (variable, subkey, type, category, selected_value, title, comment, scope, subkeytext, access_url, access_url_changeable, access_url_locked)
71
            SELECT 'enable_auto_reinscription', NULL, NULL, 'session', '0', 'Enable Auto Reinscription', 'Allow users to be automatically reinscribed in new sessions.', '', NULL, 1, 1, 1
72
            WHERE NOT EXISTS (
73
                SELECT 1 FROM settings WHERE variable = 'enable_auto_reinscription'
74
            )
75
        ");
76
77
        $this->addSql("
78
            INSERT INTO settings (variable, subkey, type, category, selected_value, title, comment, scope, subkeytext, access_url, access_url_changeable, access_url_locked)
79
            SELECT 'enable_session_replication', NULL, NULL, 'session', '0', 'Enable Session Replication', 'Allow replication of session data across instances.', '', NULL, 1, 1, 1
80
            WHERE NOT EXISTS (
81
                SELECT 1 FROM settings WHERE variable = 'enable_session_replication'
82
            )
83
        ");
84
    }
85
86
    public function down(Schema $schema): void
87
    {
88
        $schemaManager = $this->connection->createSchemaManager();
89
90
        // Revert 'new_subscription_session_id' in the 'session_rel_user' table
91
        if ($schemaManager->tablesExist('session_rel_user')) {
92
            $sessionRelUserTable = $schemaManager->listTableColumns('session_rel_user');
93
94
            if (isset($sessionRelUserTable['new_subscription_session_id'])) {
95
                $this->addSql('ALTER TABLE session_rel_user DROP COLUMN new_subscription_session_id');
96
            }
97
        }
98
99
        // Revert changes in the 'session' table
100
        if ($schemaManager->tablesExist('session')) {
101
            $sessionTable = $schemaManager->listTableColumns('session');
102
103
            if (isset($sessionTable['parent_id'])) {
104
                $this->addSql('ALTER TABLE session DROP COLUMN parent_id');
105
            }
106
            if (isset($sessionTable['days_to_reinscription'])) {
107
                $this->addSql('ALTER TABLE session DROP COLUMN days_to_reinscription');
108
            }
109
            if (isset($sessionTable['last_repetition'])) {
110
                $this->addSql('ALTER TABLE session DROP COLUMN last_repetition');
111
            }
112
            if (isset($sessionTable['days_to_new_repetition'])) {
113
                $this->addSql('ALTER TABLE session DROP COLUMN days_to_new_repetition');
114
            }
115
        }
116
117
        // Revert 'validity_in_days' in the 'session' table
118
        if ($schemaManager->tablesExist('session')) {
119
            $sessionTable = $schemaManager->listTableColumns('session');
120
121
            if (isset($sessionTable['validity_in_days'])) {
122
                $this->addSql('ALTER TABLE session DROP COLUMN validity_in_days');
123
            }
124
        }
125
126
        // Re-add 'validity_in_days' to the 'c_lp' table
127
        if ($schemaManager->tablesExist('c_lp')) {
128
            $clpTable = $schemaManager->listTableColumns('c_lp');
129
130
            if (!isset($clpTable['validity_in_days'])) {
131
                $this->addSql('ALTER TABLE c_lp ADD validity_in_days INT DEFAULT NULL');
132
            }
133
        }
134
135
        // Remove settings
136
        $this->addSql("DELETE FROM settings WHERE variable = 'enable_auto_reinscription'");
137
        $this->addSql("DELETE FROM settings WHERE variable = 'enable_session_replication'");
138
    }
139
}
140