Completed
Push — master ( 68129f...3bc187 )
by Tim
02:15 queued 10s
created

NewIncludeExcludeStructureUpdate::getIdentifier()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
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\DatabaseUpdatedPrerequisite;
13
use TYPO3\CMS\Install\Updates\UpgradeWizardInterface;
14
15
/**
16
 * NewIncludeExcludeStructureUpdate.
17
 */
18
class NewIncludeExcludeStructureUpdate extends AbstractUpdate
19
{
20
    /**
21
     * The human-readable title of the upgrade wizard.
22
     *
23
     * @var string
24
     */
25
    protected $title = 'Migrate the calendarize configurations to the new include/exclude/override/cutout structure';
26
27
    public function getIdentifier(): string
28
    {
29
        return 'calendarize_newIncludeExcludeStructure';
30
    }
31
32
33
    /**
34
     * Checks whether updates are required.
35
     *
36
     * @param string &$description The description for the update
37
     *
38
     * @return bool Whether an update is required (TRUE) or not (FALSE)
39
     */
40
    public function checkForUpdate(&$description)
41
    {
42
        $table = 'tx_calendarize_domain_model_configuration';
43
44
        $q = HelperUtility::getDatabaseConnection($table)->createQueryBuilder();
45
46
        $q->count('handling')
47
            ->from($table)
48
            ->where(
49
                $q->expr()->orX(
50
                    $q->expr()->eq('handling', $q->quote('')),
51
                    $q->expr()->isNull('handling')
52
                )
53
            );
54
55
        $count = $q->execute()->fetchColumn(0);
56
57
        if ($count > 0) {
58
            $description = 'We will update ' . $count . ' calendarize configurations';
59
60
            return true;
61
        }
62
63
        return false;
64
    }
65
66
    /**
67
     * Performs the accordant updates.
68
     *
69
     * @param array &$dbQueries      Queries done in this update
70
     * @param mixed &$customMessages Custom messages
71
     *
72
     * @return bool Whether everything went smoothly or not
73
     */
74
    public function executeUpdate(): bool
75
    {
76
        $table = 'tx_calendarize_domain_model_configuration';
77
78
        $q = HelperUtility::getDatabaseConnection($table)->createQueryBuilder();
79
        $q->update($table)
80
            ->where(
81
                $q->expr()->eq('type', $q->quote('timeExclude'))
82
            )
83
            ->set('type', ConfigurationInterface::TYPE_TIME)
84
            ->set('handling', ConfigurationInterface::HANDLING_INCLUDE);
85
86
        $dbQueries[] = $q->getSQL();
0 ignored issues
show
Coding Style Comprehensibility introduced by
$dbQueries was never initialized. Although not strictly required by PHP, it is generally a good practice to add $dbQueries = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
87
        $q->execute();
88
89
        $q->resetQueryParts();
90
91
        $q->update($table)
92
            ->where(
93
                $q->expr()->eq('type', $q->quote('include'))
94
            )
95
            ->set('type', ConfigurationInterface::TYPE_GROUP)
96
            ->set('handling', ConfigurationInterface::HANDLING_INCLUDE);
97
98
        $dbQueries[] = $q->getSQL();
99
        $q->execute();
100
101
        $q->resetQueryParts();
102
103
        $q->update($table)
104
            ->where(
105
                $q->expr()->eq('type', $q->quote('exclude'))
106
            )
107
            ->set('type', ConfigurationInterface::TYPE_GROUP)
108
            ->set('handling', ConfigurationInterface::HANDLING_EXCLUDE);
109
110
        $dbQueries[] = $q->getSQL();
111
        $q->execute();
112
113
        $q->resetQueryParts();
114
115
        $q->update($table)
116
            ->where(
117
                $q->expr()->orX(
118
                    $q->expr()->eq('handling', $q->quote('')),
119
                    $q->expr()->isNull('handling')
120
                )
121
            )
122
            ->set('handling', ConfigurationInterface::HANDLING_INCLUDE);
123
124
        $dbQueries[] = $q->getSQL();
125
        $q->execute();
126
127
        $customMessages = 'All queries are done! :)';
0 ignored issues
show
Unused Code introduced by
$customMessages is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
128
129
        return true;
130
    }
131
132
    public function updateNecessary(): bool
133
    {
134
        return false;
135
    }
136
137
    public function getPrerequisites(): array
138
    {
139
        return [
140
            DatabaseUpdatedPrerequisite::class,
141
        ];
142
    }
143
}
144