Completed
Push — master ( 2700b4...4ccb05 )
by Torben
04:03 queued 02:37
created

performRealurlAliasMigration()   B

Complexity

Conditions 3
Paths 2

Size

Total Lines 78

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 78
rs 8.48
c 0
b 0
f 0
cc 3
nc 2
nop 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
namespace DERHANSEN\SfEventMgt\Updates;
3
4
/*
5
 * This file is part of the Extension "sf_event_mgt" for TYPO3 CMS.
6
 *
7
 * For the full copyright and license information, please read the
8
 * LICENSE.txt file that was distributed with this source code.
9
 */
10
11
use TYPO3\CMS\Core\Database\ConnectionPool;
12
use TYPO3\CMS\Core\Utility\GeneralUtility;
13
use TYPO3\CMS\Install\Updates\ConfirmableInterface;
14
use TYPO3\CMS\Install\Updates\Confirmation;
15
use TYPO3\CMS\Install\Updates\DatabaseUpdatedPrerequisite;
16
use TYPO3\CMS\Install\Updates\UpgradeWizardInterface;
17
18
/**
19
 * Class RealurlAliasEventSlugUpdater
20
 */
21
class RealurlAliasEventSlugUpdater implements UpgradeWizardInterface, ConfirmableInterface
22
{
23
    protected $table = 'tx_sfeventmgt_domain_model_event';
24
    protected $confirmation;
25
26
    /**
27
     * RealurlAliasEventSlugUpdater constructor.
28
     */
29
    public function __construct()
30
    {
31
        $this->confirmation = new Confirmation(
32
            'Are you really sure?',
33
            $this->getDescription(),
34
            false,
35
            'Yes, start migration',
36
            'Skip migration',
37
            false
38
        );
39
    }
40
41
    /**
42
     * @return string Unique identifier of this updater
43
     */
44
    public function getIdentifier(): string
45
    {
46
        return 'realurlAliasEventSlugUpdater';
47
    }
48
49
    /**
50
     * Get title
51
     *
52
     * @return string
53
     */
54
    public function getTitle(): string
55
    {
56
        return '[Optional] Migrate realurl alias to slug field of EXT:sf_event_mgt records';
57
    }
58
59
    /**
60
     * Get description
61
     *
62
     * @return string Longer description of this updater
63
     */
64
    public function getDescription(): string
65
    {
66
        return 'You can migrate EXT:realurl unique alias into event slugs, to ensure that the same alias is used '
67
                . 'if similar event titles are used. This wizard migrates only matching realurl alias for event '
68
                . 'entries, where slug field is empty. Requires database table "tx_realurl_uniqalias" from '
69
                . 'EXT:realurl, but EXT:realurl does not need to be installed. Because only empty event slugs will '
70
                . 'be filled within this migration, you may decide to empty all event slugs before. The result of '
71
                . 'this migration can still leave empty slugs fields for event entries. Therfore you should generate '
72
                . 'these slugs afterwards using the event slug populator update wizard.';
73
    }
74
75
    /**
76
     * Checks whether updates are required.
77
     *
78
     * @return bool Whether an update is required (TRUE) or not (FALSE)
79
     */
80
    public function updateNecessary(): bool
81
    {
82
        return $this->checkIfWizardIsRequired();
83
    }
84
85
    /**
86
     * @return string[] All new fields and tables must exist
87
     */
88
    public function getPrerequisites(): array
89
    {
90
        return [
91
            DatabaseUpdatedPrerequisite::class
92
        ];
93
    }
94
95
    /**
96
     * Performs the updates.
97
     *
98
     * @return bool Whether everything went smoothly or not
99
     */
100
    public function executeUpdate(): bool
101
    {
102
        return $this->performRealurlAliasMigration();
103
    }
104
105
    /**
106
     * Return a confirmation message instance
107
     *
108
     * @return \TYPO3\CMS\Install\Updates\Confirmation
109
     */
110
    public function getConfirmation(): Confirmation
111
    {
112
        return $this->confirmation;
113
    }
114
115
    /**
116
     * Count valid entries from EXT:realurl table tx_realurl_uniqalias which can be migrated
117
     * Checks also for existance of third party extension table 'tx_realurl_uniqalias'
118
     * EXT:realurl requires not to be installed
119
     *
120
     * @return bool
121
     */
122
    public function checkIfWizardIsRequired(): bool
123
    {
124
        $elementCount = 0;
125
        // Check if table 'tx_realurl_uniqalias' exists
126
        $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
127
            ->getQueryBuilderForTable('tx_realurl_uniqalias');
128
        $schemaManager = $queryBuilder->getConnection()->getSchemaManager();
129
        if ($schemaManager->tablesExist(['tx_realurl_uniqalias']) === true) {
130
            // Count valid aliases for events
131
            $queryBuilder->getRestrictions()->removeAll();
132
            $elementCount = $queryBuilder->selectLiteral('COUNT(DISTINCT tx_sfeventmgt_domain_model_event.uid)')
133
                ->from('tx_realurl_uniqalias')
134
                ->join(
135
                    'tx_realurl_uniqalias',
136
                    'tx_sfeventmgt_domain_model_event',
137
                    'tx_sfeventmgt_domain_model_event',
138
                    $queryBuilder->expr()->eq(
139
                        'tx_realurl_uniqalias.value_id',
140
                        $queryBuilder->quoteIdentifier('tx_sfeventmgt_domain_model_event.uid')
141
                    )
142
                )
143
                ->where(
144
                    $queryBuilder->expr()->andX(
145
                        $queryBuilder->expr()->orX(
146
                            $queryBuilder->expr()->eq(
147
                                'tx_sfeventmgt_domain_model_event.slug',
148
                                $queryBuilder->createNamedParameter('', \PDO::PARAM_STR)
149
                            ),
150
                            $queryBuilder->expr()->isNull('tx_sfeventmgt_domain_model_event.slug')
151
                        ),
152
                        $queryBuilder->expr()->eq(
153
                            'tx_sfeventmgt_domain_model_event.sys_language_uid',
154
                            'tx_realurl_uniqalias.lang'
155
                        ),
156
                        $queryBuilder->expr()->eq(
157
                            'tx_realurl_uniqalias.tablename',
158
                            $queryBuilder->createNamedParameter('tx_sfeventmgt_domain_model_event', \PDO::PARAM_STR)
159
                        ),
160
                        $queryBuilder->expr()->orX(
161
                            $queryBuilder->expr()->eq(
162
                                'tx_realurl_uniqalias.expire',
163
                                $queryBuilder->createNamedParameter(0, \PDO::PARAM_INT)
164
                            ),
165
                            $queryBuilder->expr()->gte(
166
                                'tx_realurl_uniqalias.expire',
167
                                $queryBuilder->createNamedParameter($GLOBALS['ACCESS_TIME'], \PDO::PARAM_INT)
168
                            )
169
                        )
170
                    )
171
                )
172
                ->execute()->fetchColumn(0);
173
        }
174
175
        return $elementCount > 0;
176
    }
177
178
    /**
179
     * Perform migration of EXT:realurl unique alias into empty event slugs
180
     *
181
     * @return bool
182
     */
183
    public function performRealurlAliasMigration(): bool
184
    {
185
        $result = true;
186
        // Check if table 'tx_realurl_uniqalias' exists
187
        $queryBuilderForRealurl = GeneralUtility::makeInstance(ConnectionPool::class)
188
            ->getQueryBuilderForTable('tx_realurl_uniqalias');
189
        $schemaManager = $queryBuilderForRealurl->getConnection()->getSchemaManager();
190
        if ($schemaManager->tablesExist(['tx_realurl_uniqalias']) === true) {
191
            $connection = GeneralUtility::makeInstance(ConnectionPool::class)
192
                ->getConnectionForTable('tx_sfeventmgt_domain_model_event');
193
            $queryBuilder = $connection->createQueryBuilder();
194
            $queryBuilder->getRestrictions()->removeAll();
195
196
            // Get entries to update
197
            $statement = $queryBuilder
198
                ->selectLiteral(
199
                    'DISTINCT tx_sfeventmgt_domain_model_event.uid, tx_realurl_uniqalias.value_alias, tx_sfeventmgt_domain_model_event.uid'
200
                )
201
                ->from('tx_sfeventmgt_domain_model_event')
202
                ->join(
203
                    'tx_sfeventmgt_domain_model_event',
204
                    'tx_realurl_uniqalias',
205
                    'tx_realurl_uniqalias',
206
                    $queryBuilder->expr()->eq(
207
                        'tx_sfeventmgt_domain_model_event.uid',
208
                        $queryBuilder->quoteIdentifier('tx_realurl_uniqalias.value_id')
209
                    )
210
                )
211
                ->where(
212
                    $queryBuilder->expr()->andX(
213
                        $queryBuilder->expr()->orX(
214
                            $queryBuilder->expr()->eq(
215
                                'tx_sfeventmgt_domain_model_event.slug',
216
                                $queryBuilder->createNamedParameter('', \PDO::PARAM_STR)
217
                            ),
218
                            $queryBuilder->expr()->isNull('tx_sfeventmgt_domain_model_event.slug')
219
                        ),
220
                        $queryBuilder->expr()->eq(
221
                            'tx_sfeventmgt_domain_model_event.sys_language_uid',
222
                            'tx_realurl_uniqalias.lang'
223
                        ),
224
                        $queryBuilder->expr()->eq(
225
                            'tx_realurl_uniqalias.tablename',
226
                            $queryBuilder->createNamedParameter('tx_sfeventmgt_domain_model_event', \PDO::PARAM_STR)
227
                        ),
228
                        $queryBuilder->expr()->orX(
229
                            $queryBuilder->expr()->eq(
230
                                'tx_realurl_uniqalias.expire',
231
                                $queryBuilder->createNamedParameter(0, \PDO::PARAM_INT)
232
                            ),
233
                            $queryBuilder->expr()->gte(
234
                                'tx_realurl_uniqalias.expire',
235
                                $queryBuilder->createNamedParameter($GLOBALS['ACCESS_TIME'], \PDO::PARAM_INT)
236
                            )
237
                        )
238
                    )
239
                )
240
                ->execute();
241
242
            // Update entries
243
            while ($record = $statement->fetch()) {
244
                $queryBuilder = $connection->createQueryBuilder();
245
                $queryBuilder->update('tx_sfeventmgt_domain_model_event')
246
                    ->where(
247
                        $queryBuilder->expr()->eq(
248
                            'uid',
249
                            $queryBuilder->createNamedParameter($record['uid'], \PDO::PARAM_INT)
250
                        )
251
                    )
252
                    ->set('slug', (string)$record['value_alias']);
253
                $queryBuilder->execute();
254
            }
255
        } else {
256
            $result = false;
257
        }
258
259
        return $result;
260
    }
261
}
262