Completed
Pull Request — master (#248)
by Pascale
03:56
created

NewIncludeExcludeStructureUpdate   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 113
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 0
Metric Value
wmc 3
lcom 0
cbo 3
dl 0
loc 113
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A checkForUpdate() 0 25 2
B performUpdate() 0 62 1
1
<?php
2
3
/**
4
 * NewIncludeExcludeStructureUpdate.
5
 */
6
declare(strict_types=1);
7
8
namespace HDNET\Calendarize\Updates;
9
10
use HDNET\Calendarize\Domain\Model\ConfigurationInterface;
11
use HDNET\Calendarize\Utility\HelperUtility;
12
use TYPO3\CMS\Install\Updates\AbstractUpdate;
13
14
/**
15
 * NewIncludeExcludeStructureUpdate.
16
 */
17
class NewIncludeExcludeStructureUpdate extends AbstractUpdate
18
{
19
    /**
20
     * The human-readable title of the upgrade wizard.
21
     *
22
     * @var string
23
     */
24
    protected $title = 'Migrate the calendarize configurations to the new include/exclude/override/cutout structure';
25
26
    /**
27
     * Checks whether updates are required.
28
     *
29
     * @param string &$description The description for the update
30
     *
31
     * @return bool Whether an update is required (TRUE) or not (FALSE)
32
     */
33
    public function checkForUpdate(&$description)
34
    {
35
        $table = 'tx_calendarize_domain_model_configuration';
36
37
        $q = HelperUtility::getDatabaseConnection($table)->createQueryBuilder();
38
39
        $q->count('*')
40
            ->from($table)
41
            ->where(
42
                $q->expr()->orX(
43
                    $q->expr()->eq('handling', ''),
44
                    $q->expr()->isNull('handling')
45
                )
46
            );
47
48
        $count = $q->execute();
49
50
        if ($count > 0) {
51
            $description = 'We will update ' . $count . ' calendarize configurations';
52
53
            return true;
54
        }
55
56
        return false;
57
    }
58
59
    /**
60
     * Performs the accordant updates.
61
     *
62
     * @param array &$dbQueries      Queries done in this update
63
     * @param mixed &$customMessages Custom messages
64
     *
65
     * @return bool Whether everything went smoothly or not
66
     */
67
    public function performUpdate(array &$dbQueries, &$customMessages)
68
    {
69
        $table = 'tx_calendarize_domain_model_configuration';
70
71
        $q = HelperUtility::getDatabaseConnection($table)->createQueryBuilder();
72
        $q->update($table)
73
            ->where(
74
                $q->expr()->eq('type', 'timeExclude')
75
            )
76
            ->values([
77
                'type' => ConfigurationInterface::TYPE_TIME,
78
                'handling' => ConfigurationInterface::HANDLING_INCLUDE,
79
            ]);
80
81
        $dbQueries[] = $q->getSQL();
82
        $q->execute();
83
        $q->resetQueryParts();
84
85
        $q->update('tx_calendarize_domain_model_configuration')
86
            ->where(
87
                $q->expr()->eq('type', 'include')
88
            )
89
            ->values([
90
                'type' => ConfigurationInterface::TYPE_GROUP,
91
                'handling' => ConfigurationInterface::HANDLING_INCLUDE,
92
            ]);
93
94
        $dbQueries[] = $q->getSQL();
95
        $q->execute();
96
        $q->resetQueryParts();
97
98
        $q->update('tx_calendarize_domain_model_configuration')
99
            ->where(
100
                $q->expr()->eq('type', 'exclude')
101
            )
102
            ->values([
103
                'type' => ConfigurationInterface::TYPE_GROUP,
104
                'handling' => ConfigurationInterface::HANDLING_EXCLUDE,
105
            ]);
106
107
        $dbQueries[] = $q->getSQL();
108
        $q->execute();
109
        $q->resetQueryParts();
110
111
        $q->update('tx_calendarize_domain_model_configuration')
112
            ->where(
113
                $q->expr()->orX(
114
                    $q->expr()->eq('handling', ''),
115
                    $q->expr()->isNull('handling')
116
                )
117
            )
118
            ->values([
119
                'handling' => ConfigurationInterface::HANDLING_INCLUDE,
120
            ]);
121
122
        $dbQueries[] = $q->getSQL();
123
        $q->execute();
124
125
        $customMessages = 'All queries are done! :)';
126
127
        return true;
128
    }
129
}
130