Passed
Push — master ( 0d18d8...40235e )
by Torben
28:04 queued 24:57
created

MaintenanceService::processGdprCleanup()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 19
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 12
nc 6
nop 3
dl 0
loc 19
rs 9.8666
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): void
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): void
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
     */
111
    protected function deleteRegistration(int $registrationUid): void
112
    {
113
        $connection = GeneralUtility::makeInstance(ConnectionPool::class)
114
            ->getConnectionForTable('tx_sfeventmgt_domain_model_registration');
115
116
        $connection->delete(
117
            'tx_sfeventmgt_domain_model_registration',
118
            ['uid' => $registrationUid]
119
        );
120
    }
121
122
    /**
123
     * Deletes all registration field values for the given registrationUid
124
     *
125
     * @param int $registrationUid
126
     */
127
    protected function deleteRegistrationFieldValues(int $registrationUid): void
128
    {
129
        $connection = GeneralUtility::makeInstance(ConnectionPool::class)
130
            ->getConnectionForTable('tx_sfeventmgt_domain_model_registration_fieldvalue');
131
132
        $connection->delete(
133
            'tx_sfeventmgt_domain_model_registration_fieldvalue',
134
            ['registration' => $registrationUid]
135
        );
136
    }
137
138
    /**
139
     * Returns an array of registration uids, which are considered as expired
140
     *
141
     * @return array
142
     */
143
    protected function getExpiredRegistrations(): array
144
    {
145
        $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
146
            ->getQueryBuilderForTable('tx_sfeventmgt_domain_model_registration');
147
148
        return $queryBuilder
149
            ->select('uid', 'pid', 'event')
150
            ->from('tx_sfeventmgt_domain_model_registration')
151
            ->where(
152
                $queryBuilder->expr()->lte(
153
                    'confirmation_until',
154
                    $queryBuilder->createNamedParameter(time(), \PDO::PARAM_INT)
155
                ),
156
                $queryBuilder->expr()->eq(
157
                    'confirmed',
158
                    $queryBuilder->createNamedParameter(0, \PDO::PARAM_INT)
159
                )
160
            )
161
            ->execute()
162
            ->fetchAll();
163
    }
164
165
    /**
166
     * Returns all registrations, where the related event has expired based on the given amount of days
167
     *
168
     * @param int $days
169
     * @return array
170
     */
171
    protected function getGdprCleanupRegistrations(int $days): array
172
    {
173
        $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
174
            ->getQueryBuilderForTable('tx_sfeventmgt_domain_model_registration');
175
        $queryBuilder->getRestrictions()->removeAll();
176
        $maxEndDate = (new \DateTime())->modify('-' . $days . ' days');
177
178
        return $queryBuilder
179
            ->select('tx_sfeventmgt_domain_model_registration.uid')
180
            ->from('tx_sfeventmgt_domain_model_registration')
181
            ->join(
182
                'tx_sfeventmgt_domain_model_registration',
183
                'tx_sfeventmgt_domain_model_event',
184
                'e',
185
                $queryBuilder->expr()->eq(
186
                    'e.uid',
187
                    $queryBuilder->quoteIdentifier('tx_sfeventmgt_domain_model_registration.event')
188
                )
189
            )->where(
190
                $queryBuilder->expr()->lte(
191
                    'e.enddate',
192
                    $queryBuilder->createNamedParameter($maxEndDate->getTimestamp(), \PDO::PARAM_INT)
193
                )
194
            )->execute()
195
            ->fetchAll();
196
    }
197
198
    /**
199
     * Returns all registrations including hidden and deleted
200
     *
201
     * @return array
202
     */
203
    protected function getAllRegistrations(): array
204
    {
205
        $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
206
            ->getQueryBuilderForTable('tx_sfeventmgt_domain_model_registration');
207
        $queryBuilder->getRestrictions()->removeAll();
208
209
        return $queryBuilder
210
            ->select('uid')
211
            ->from('tx_sfeventmgt_domain_model_registration')
212
            ->execute()
213
            ->fetchAll();
214
    }
215
}
216