Passed
Push — 5.x ( 443321...10bf12 )
by Torben
45:10
created

MaintenanceService::getGdprCleanupRegistrations()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 25
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 20
nc 1
nop 1
dl 0
loc 25
rs 9.6
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Extension "sf_event_mgt" for TYPO3 CMS.
7
 *
8
 * For the full copyright and license information, please read the
9
 * LICENSE.txt file that was distributed with this source code.
10
 */
11
12
namespace DERHANSEN\SfEventMgt\Service;
13
14
use TYPO3\CMS\Core\Database\ConnectionPool;
15
use TYPO3\CMS\Core\Utility\GeneralUtility;
16
17
/**
18
 * MaintenanceService
19
 */
20
class MaintenanceService
21
{
22
    /**
23
     * Handles expired registration
24
     *
25
     * @param bool $delete
26
     */
27
    public function handleExpiredRegistrations(bool $delete = false)
28
    {
29
        $eventCacheService = GeneralUtility::makeInstance(EventCacheService::class);
30
        $registrationUids = $this->getExpiredRegistrations();
31
        foreach ($registrationUids as $registration) {
32
            $this->updateRegistration($registration['uid'], $delete);
33
            $eventCacheService->flushEventCache($registration['event'], $registration['pid']);
34
        }
35
    }
36
37
    /**
38
     * Processes a GDPR cleaup by removing all registrations of expired events. Returns the amount of registrations
39
     * removed.
40
     *
41
     * @param int $days
42
     * @param bool $softDelete
43
     * @param bool $ignoreEventRestriction
44
     * @return int
45
     */
46
    public function processGdprCleanup(int $days, bool $softDelete, bool $ignoreEventRestriction): int
47
    {
48
        if (!$ignoreEventRestriction) {
49
            $registrations = $this->getGdprCleanupRegistrations($days);
50
        } else {
51
            $registrations = $this->getAllRegistrations();
52
        }
53
54
        foreach ($registrations as $registration) {
55
            if (!$softDelete) {
56
                $this->deleteRegistration($registration['uid']);
57
                $this->deleteRegistrationFieldValues($registration['uid']);
58
            } else {
59
                $this->updateRegistration($registration['uid'], true);
60
                $this->flagRegistrationFieldValuesAsDeleted($registration['uid']);
61
            }
62
        }
63
64
        return count($registrations);
65
    }
66
67
    /**
68
     * Updates the given registration
69
     *
70
     * @param int $registrationUid
71
     * @param bool $delete
72
     */
73
    protected function updateRegistration(int $registrationUid, bool $delete = false)
74
    {
75
        $connection = GeneralUtility::makeInstance(ConnectionPool::class)
76
            ->getConnectionForTable('tx_sfeventmgt_domain_model_registration');
77
78
        $field = $delete === true ? 'deleted' : 'hidden';
79
        $updateFields = [];
80
        $updateFields[$field] = 1;
81
82
        $connection->update(
83
            'tx_sfeventmgt_domain_model_registration',
84
            $updateFields,
85
            ['uid' => $registrationUid]
86
        );
87
    }
88
89
    /**
90
     * Flags all registration field values for the given registration UID as deleted
91
     *
92
     * @param int $registrationUid
93
     */
94
    protected function flagRegistrationFieldValuesAsDeleted(int $registrationUid): void
95
    {
96
        $connection = GeneralUtility::makeInstance(ConnectionPool::class)
97
            ->getConnectionForTable('tx_sfeventmgt_domain_model_registration_fieldvalue');
98
99
        $connection->update(
100
            'tx_sfeventmgt_domain_model_registration_fieldvalue',
101
            ['deleted' => 1],
102
            ['registration' => $registrationUid]
103
        );
104
    }
105
106
    /**
107
     * Deletes the registration with the given uid
108
     *
109
     * @param int $registrationUid
110
     * @return void
111
     */
112
    protected function deleteRegistration(int $registrationUid): void
113
    {
114
        $connection = GeneralUtility::makeInstance(ConnectionPool::class)
115
            ->getConnectionForTable('tx_sfeventmgt_domain_model_registration');
116
117
        $connection->delete(
118
            'tx_sfeventmgt_domain_model_registration',
119
            ['uid' => $registrationUid]
120
        );
121
    }
122
123
    /**
124
     * Deletes all registration field values for the given registrationUid
125
     *
126
     * @param int $registrationUid
127
     * @return void
128
     */
129
    protected function deleteRegistrationFieldValues(int $registrationUid): void
130
    {
131
        $connection = GeneralUtility::makeInstance(ConnectionPool::class)
132
            ->getConnectionForTable('tx_sfeventmgt_domain_model_registration_fieldvalue');
133
134
        $connection->delete(
135
            'tx_sfeventmgt_domain_model_registration_fieldvalue',
136
            ['registration' => $registrationUid]
137
        );
138
    }
139
140
    /**
141
     * Returns an array of registration uids, which are considered as expired
142
     *
143
     * @return array
144
     */
145
    protected function getExpiredRegistrations(): array
146
    {
147
        $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
148
            ->getQueryBuilderForTable('tx_sfeventmgt_domain_model_registration');
149
150
        return $queryBuilder
151
            ->select('uid', 'pid', 'event')
152
            ->from('tx_sfeventmgt_domain_model_registration')
153
            ->where(
154
                $queryBuilder->expr()->lte(
155
                    'confirmation_until',
156
                    $queryBuilder->createNamedParameter(time(), \PDO::PARAM_INT)
157
                ),
158
                $queryBuilder->expr()->eq(
159
                    'confirmed',
160
                    $queryBuilder->createNamedParameter(0, \PDO::PARAM_INT)
161
                )
162
            )
163
            ->execute()
164
            ->fetchAll();
165
    }
166
167
    /**
168
     * Returns all registrations, where the related event has expired based on the given amount of days
169
     *
170
     * @param int $days
171
     * @return array
172
     */
173
    protected function getGdprCleanupRegistrations(int $days): array
174
    {
175
        $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
176
            ->getQueryBuilderForTable('tx_sfeventmgt_domain_model_registration');
177
        $queryBuilder->getRestrictions()->removeAll();
178
        $maxEndDate = (new \DateTime())->modify('-' . $days . ' days');
179
180
        return $queryBuilder
181
            ->select('tx_sfeventmgt_domain_model_registration.uid')
182
            ->from('tx_sfeventmgt_domain_model_registration')
183
            ->join(
184
                'tx_sfeventmgt_domain_model_registration',
185
                'tx_sfeventmgt_domain_model_event',
186
                'e',
187
                $queryBuilder->expr()->eq(
188
                    'e.uid',
189
                    $queryBuilder->quoteIdentifier('tx_sfeventmgt_domain_model_registration.event')
190
                )
191
            )->where(
192
                $queryBuilder->expr()->lte(
193
                    'e.enddate',
194
                    $queryBuilder->createNamedParameter($maxEndDate->getTimestamp(), \PDO::PARAM_INT)
195
                )
196
            )->execute()
197
            ->fetchAll();
198
    }
199
200
    /**
201
     * Returns all registrations including hidden and deleted
202
     *
203
     * @return array
204
     */
205
    protected function getAllRegistrations(): array
206
    {
207
        $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
208
            ->getQueryBuilderForTable('tx_sfeventmgt_domain_model_registration');
209
        $queryBuilder->getRestrictions()->removeAll();
210
211
        return $queryBuilder
212
            ->select('uid')
213
            ->from('tx_sfeventmgt_domain_model_registration')
214
            ->execute()
215
            ->fetchAll();
216
    }
217
}
218