Passed
Pull Request — master (#6495)
by Angel Fernando Quiroz
08:49
created

Version20201212114910::up()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 80
Code Lines 45

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 45
nc 4
nop 1
dl 0
loc 80
rs 8.8888
c 0
b 0
f 0

How to fix   Long Method   

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
declare(strict_types=1);
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 Version20201212114910 extends AbstractMigrationChamilo
13
{
14
    public function getDescription(): string
15
    {
16
        return 'Add password_rotation_days setting and user.password_updated_at column; migrate existing extra-field data and remove old extra-field';
17
    }
18
19
    public function up(Schema $schema): void
20
    {
21
        // 1. Add new column to user table
22
        $this->addSql('ALTER TABLE `user` ADD COLUMN `password_updated_at` DATETIME DEFAULT NULL;');
23
24
        // 2. Insert or update the new setting in settings table
25
        $setting = [
26
            'variable' => 'password_rotation_days',
27
            'selected_value' => '0',
28
            'title' => 'Password rotation interval (days)',
29
            'comment' => 'Number of days before users must rotate their password (0 = disabled).',
30
            'category' => 'security',
31
        ];
32
33
        $sqlCheck = \sprintf(
34
            "SELECT COUNT(*) AS count
35
               FROM settings
36
              WHERE variable = '%s'
37
                AND subkey IS NULL
38
                AND access_url = 1",
39
            addslashes($setting['variable'])
40
        );
41
        $result = $this->connection->fetchAssociative($sqlCheck);
42
43
        if ($result && (int) $result['count'] > 0) {
44
            // UPDATE existing setting
45
            $this->addSql(\sprintf(
46
                "UPDATE settings
47
                    SET selected_value = '%s',
48
                        title          = '%s',
49
                        comment        = '%s',
50
                        category       = '%s'
51
                  WHERE variable = '%s'
52
                    AND subkey IS NULL
53
                    AND access_url = 1",
54
                addslashes($setting['selected_value']),
55
                addslashes($setting['title']),
56
                addslashes($setting['comment']),
57
                addslashes($setting['category']),
58
                addslashes($setting['variable'])
59
            ));
60
        } else {
61
            // INSERT new setting
62
            $this->addSql(\sprintf(
63
                "INSERT INTO settings
64
                    (variable, subkey, type, category, selected_value, title, comment, access_url_changeable, access_url_locked, access_url)
65
                 VALUES
66
                    ('%s', NULL, NULL, '%s', '%s', '%s', '%s', 1, 0, 1)",
67
                addslashes($setting['variable']),
68
                addslashes($setting['category']),
69
                addslashes($setting['selected_value']),
70
                addslashes($setting['title']),
71
                addslashes($setting['comment'])
72
            ));
73
        }
74
75
        // 3. Migrate existing extra-field data into the new column (if tables exist)
76
        if ($schema->hasTable('extra_field_values') && $schema->hasTable('extra_field')) {
77
            // Copy field_value into user.password_updated_at for variable 'password_updated_at'
78
            $this->addSql("
79
                UPDATE `user` AS u
80
                INNER JOIN `extra_field_values` AS efv
81
                    ON efv.item_id = u.id
82
                INNER JOIN `extra_field` AS ef
83
                    ON ef.id = efv.field_id
84
                   AND ef.variable = 'password_updated_at'
85
                SET u.password_updated_at = efv.field_value
86
            ");
87
88
            // Delete the old extra_field_values entries
89
            $this->addSql("
90
                DELETE efv
91
                  FROM `extra_field_values` AS efv
92
                INNER JOIN `extra_field` AS ef
93
                    ON ef.id = efv.field_id
94
                   AND ef.variable = 'password_updated_at'
95
            ");
96
97
            // Delete the old extra_field definition
98
            $this->addSql("
99
                DELETE ef
100
                  FROM `extra_field` AS ef
101
                 WHERE ef.variable = 'password_updated_at'
102
                   AND ef.item_type = 1
103
            ");
104
        }
105
    }
106
107
    public function down(Schema $schema): void
108
    {
109
        // 1. Remove the setting
110
        $this->addSql("
111
            DELETE FROM settings
112
             WHERE variable = 'password_rotation_days'
113
               AND subkey IS NULL
114
               AND access_url = 1
115
        ");
116
117
        // 2. Drop the column from user table
118
        $this->addSql('ALTER TABLE `user` DROP COLUMN `password_updated_at`;');
119
    }
120
}
121