Passed
Pull Request — master (#5640)
by Yannick
07:50
created

PriorityMigrationHelper::executeUp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 5
rs 10
c 1
b 0
f 0
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 Doctrine\DBAL\Schema\Schema;
10
use Doctrine\DBAL\Connection;
11
use Psr\Log\LoggerInterface;
12
13
class PriorityMigrationHelper
14
{
15
    private Connection $connection;
16
    private LoggerInterface $logger;
17
18
    public function __construct(Connection $connection, LoggerInterface $logger)
19
    {
20
        $this->connection = $connection;
21
        $this->logger = $logger;
22
    }
23
24
    public function executeUp(Schema $schema): void
25
    {
26
        $this->logger->info('Executing PriorityMigrationHelper up method.');
27
        $this->renameSettingsTableUp();
28
        $this->addDurationFields($schema);
29
    }
30
31
    public function executeDown(Schema $schema): void
32
    {
33
        $this->logger->info('Executing PriorityMigrationHelper down method.');
34
        $this->renameSettingsTableDown();
35
        $this->removeDurationFields($schema);
36
    }
37
38
    private function addDurationFields(Schema $schema): void
39
    {
40
        $tables = [
41
            'course',
42
            'c_survey',
43
            'c_quiz',
44
            'c_quiz_question',
45
            'c_lp',
46
            'c_lp_item',
47
            'c_student_publication',
48
            'c_attendance_calendar'
49
        ];
50
51
        foreach ($tables as $tableName) {
52
            $table = $schema->getTable($tableName);
53
            if (!$table->hasColumn('duration')) {
54
                $this->connection->executeQuery("ALTER TABLE $tableName ADD duration INT DEFAULT NULL");
55
            }
56
        }
57
    }
58
59
    private function removeDurationFields(Schema $schema): void
60
    {
61
        $tables = [
62
            'course',
63
            'c_survey',
64
            'c_quiz',
65
            'c_quiz_question',
66
            'c_lp',
67
            'c_lp_item',
68
            'c_student_publication',
69
            'c_attendance_calendar'
70
        ];
71
72
        foreach ($tables as $tableName) {
73
            $table = $schema->getTable($tableName);
74
            if ($table->hasColumn('duration')) {
75
                $this->connection->executeQuery("ALTER TABLE $tableName DROP COLUMN duration");
76
            }
77
        }
78
    }
79
80
    private function renameSettingsTableUp(): void
81
    {
82
        $schemaManager = $this->connection->createSchemaManager();
83
84
        if ($schemaManager->tablesExist(['settings_current']) && !$schemaManager->tablesExist(['settings'])) {
85
            $this->connection->executeQuery('RENAME TABLE settings_current TO settings');
86
        }
87
    }
88
89
    private function renameSettingsTableDown(): void
90
    {
91
        $schemaManager = $this->connection->createSchemaManager();
92
93
        if ($schemaManager->tablesExist(['settings']) && !$schemaManager->tablesExist(['settings_current'])) {
94
            $this->connection->executeQuery('RENAME TABLE settings TO settings_current');
95
        }
96
    }
97
}
98