Completed
Push — payment ( a2694a )
by Torben
42:06
created

RegistrationService::checkCancelRegistration()   D

Complexity

Conditions 9
Paths 16

Size

Total Lines 43
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 1
Metric Value
c 3
b 0
f 1
dl 0
loc 43
rs 4.909
cc 9
eloc 29
nc 16
nop 2
1
<?php
2
namespace DERHANSEN\SfEventMgt\Service;
3
4
/*
5
 * This file is part of the TYPO3 CMS project.
6
 *
7
 * It is free software; you can redistribute it and/or modify it under
8
 * the terms of the GNU General Public License, either version 2
9
 * of the License, or any later version.
10
 *
11
 * For the full copyright and license information, please read the
12
 * LICENSE.txt file that was distributed with this source code.
13
 *
14
 * The TYPO3 project - inspiring people to share!
15
 */
16
17
use \TYPO3\CMS\Extbase\Reflection\ObjectAccess;
18
use DERHANSEN\SfEventMgt\Domain\Model\Registration;
19
use DERHANSEN\SfEventMgt\Utility\RegistrationResult;
20
use DERHANSEN\SfEventMgt\Domain\Model\Event;
21
22
/**
23
 * RegistrationService
24
 *
25
 * @author Torben Hansen <[email protected]>
26
 */
27
class RegistrationService
28
{
29
30
    /**
31
     * The object manager
32
     *
33
     * @var \TYPO3\CMS\Extbase\Object\ObjectManager
34
     * @inject
35
     */
36
    protected $objectManager;
37
38
    /**
39
     * RegistrationRepository
40
     *
41
     * @var \DERHANSEN\SfEventMgt\Domain\Repository\RegistrationRepository
42
     * @inject
43
     */
44
    protected $registrationRepository;
45
46
    /**
47
     * FrontendUserRepository
48
     *
49
     * @var \TYPO3\CMS\Extbase\Domain\Repository\FrontendUserRepository
50
     * @inject
51
     */
52
    protected $frontendUserRepository;
53
54
    /**
55
     * Hash Service
56
     *
57
     * @var \TYPO3\CMS\Extbase\Security\Cryptography\HashService
58
     * @inject
59
     */
60
    protected $hashService;
61
62
    /**
63
     * Handles expired registrations. If the $delete parameter is set, then
64
     * registrations are deleted, else just hidden
65
     *
66
     * @param bool $delete Delete
67
     *
68
     * @return void
69
     */
70
    public function handleExpiredRegistrations($delete = false)
71
    {
72
        $registrations = $this->registrationRepository->findExpiredRegistrations(new \DateTime());
73
        if ($registrations->count() > 0) {
74
            foreach ($registrations as $registration) {
75
                /** @var \DERHANSEN\SfEventMgt\Domain\Model\Registration $registration */
76
                if ($delete) {
77
                    $this->registrationRepository->remove($registration);
78
                } else {
79
                    $registration->setHidden(true);
80
                    $this->registrationRepository->update($registration);
81
                }
82
            }
83
        }
84
    }
85
86
    /**
87
     * Duplicates (all public accessable properties) the given registration the
88
     * amount of times configured in amountOfRegistrations
89
     *
90
     * @param \DERHANSEN\SfEventMgt\Domain\Model\Registration $registration Registration
91
     *
92
     * @return void
93
     */
94
    public function createDependingRegistrations($registration)
95
    {
96
        $registrations = $registration->getAmountOfRegistrations();
97
        for ($i = 1; $i <= $registrations - 1; $i++) {
98
            /** @var \DERHANSEN\SfEventMgt\Domain\Model\Registration $newReg */
99
            $newReg = $this->objectManager->get('DERHANSEN\SfEventMgt\Domain\Model\Registration');
100
            $properties = ObjectAccess::getGettableProperties($registration);
101
            foreach ($properties as $propertyName => $propertyValue) {
102
                ObjectAccess::setProperty($newReg, $propertyName, $propertyValue);
103
            }
104
            $newReg->setMainRegistration($registration);
105
            $newReg->setAmountOfRegistrations(1);
106
            $newReg->setIgnoreNotifications(true);
107
            $this->registrationRepository->add($newReg);
108
        }
109
    }
110
111
    /**
112
     * Confirms all depending registrations based on the given main registration
113
     *
114
     * @param \DERHANSEN\SfEventMgt\Domain\Model\Registration $registration Registration
115
     *
116
     * @return void
117
     */
118
    public function confirmDependingRegistrations($registration)
119
    {
120
        $registrations = $this->registrationRepository->findByMainRegistration($registration);
121
        foreach ($registrations as $foundRegistration) {
122
            /** @var \DERHANSEN\SfEventMgt\Domain\Model\Registration $foundRegistration */
123
            $foundRegistration->setConfirmed(true);
124
            $this->registrationRepository->update($foundRegistration);
125
        }
126
    }
127
128
    /**
129
     * Checks if the registration can be confirmed and returns an array of variables
130
     *
131
     * @param int $reguid UID of registration
132
     * @param string $hmac HMAC for parameters
133
     *
134
     * @return array
135
     */
136
    public function checkConfirmRegistration($reguid, $hmac)
137
    {
138
        /* @var $registration Registration */
139
        $registration = null;
140
        $failed = false;
141
        $messageKey = 'event.message.confirmation_successful';
142
        $titleKey = 'confirmRegistration.title.successful';
143
144
        if (!$this->hashService->validateHmac('reg-' . $reguid, $hmac)) {
145
            $failed = true;
146
            $messageKey = 'event.message.confirmation_failed_wrong_hmac';
147
            $titleKey = 'confirmRegistration.title.failed';
148
        } else {
149
            $registration = $this->registrationRepository->findByUid($reguid);
150
        }
151
152
        if (!$failed && is_null($registration)) {
153
            $failed = true;
154
            $messageKey = 'event.message.confirmation_failed_registration_not_found';
155
            $titleKey = 'confirmRegistration.title.failed';
156
        }
157
158
        if (!$failed && $registration->getConfirmationUntil() < new \DateTime()) {
159
            $failed = true;
160
            $messageKey = 'event.message.confirmation_failed_confirmation_until_expired';
161
            $titleKey = 'confirmRegistration.title.failed';
162
        }
163
164
        if (!$failed && $registration->getConfirmed() === true) {
165
            $failed = true;
166
            $messageKey = 'event.message.confirmation_failed_already_confirmed';
167
            $titleKey = 'confirmRegistration.title.failed';
168
        }
169
170
        return [
171
            $failed,
172
            $registration,
173
            $messageKey,
174
            $titleKey
175
        ];
176
    }
177
178
    /**
179
     * Cancels all depending registrations based on the given main registration
180
     *
181
     * @param \DERHANSEN\SfEventMgt\Domain\Model\Registration $registration Registration
182
     *
183
     * @return void
184
     */
185
    public function cancelDependingRegistrations($registration)
186
    {
187
        $registrations = $this->registrationRepository->findByMainRegistration($registration);
188
        foreach ($registrations as $foundRegistration) {
189
            $this->registrationRepository->remove($foundRegistration);
190
        }
191
    }
192
193
    /**
194
     * Checks if the registration can be cancelled and returns an array of variables
195
     *
196
     * @param int $reguid UID of registration
197
     * @param string $hmac HMAC for parameters
198
     *
199
     * @return array
200
     */
201
    public function checkCancelRegistration($reguid, $hmac)
202
    {
203
        /* @var $registration Registration */
204
        $registration = null;
205
        $failed = false;
206
        $messageKey = 'event.message.cancel_successful';
207
        $titleKey = 'cancelRegistration.title.successful';
208
209
        if (!$this->hashService->validateHmac('reg-' . $reguid, $hmac)) {
210
            $failed = true;
211
            $messageKey = 'event.message.cancel_failed_wrong_hmac';
212
            $titleKey = 'cancelRegistration.title.failed';
213
        } else {
214
            $registration = $this->registrationRepository->findByUid($reguid);
215
        }
216
217
        if (!$failed && is_null($registration)) {
218
            $failed = true;
219
            $messageKey = 'event.message.cancel_failed_registration_not_found_or_cancelled';
220
            $titleKey = 'cancelRegistration.title.failed';
221
        }
222
223
        if (!$failed && $registration->getEvent()->getEnableCancel() === false) {
224
            $failed = true;
225
            $messageKey = 'event.message.confirmation_failed_cancel_disabled';
226
            $titleKey = 'cancelRegistration.title.failed';
227
        }
228
229
        if (!$failed && $registration->getEvent()->getCancelDeadline() > 0
230
            && $registration->getEvent()->getCancelDeadline() < new \DateTime()
231
        ) {
232
            $failed = true;
233
            $messageKey = 'event.message.cancel_failed_deadline_expired';
234
            $titleKey = 'cancelRegistration.title.failed';
235
        }
236
237
        return [
238
            $failed,
239
            $registration,
240
            $messageKey,
241
            $titleKey
242
        ];
243
    }
244
245
    /**
246
     * Returns the current frontend user object if available
247
     *
248
     * @return \TYPO3\CMS\Extbase\Domain\Model\FrontendUser|null
249
     */
250
    public function getCurrentFeUserObject()
251
    {
252
        if (isset($GLOBALS['TSFE']->fe_user->user['uid'])) {
253
            return $this->frontendUserRepository->findByUid($GLOBALS['TSFE']->fe_user->user['uid']);
254
        } else {
255
            return null;
256
        }
257
    }
258
259
    /**
260
     * Checks, if the registration can successfully be created. Note, that
261
     * $result is passed by reference!
262
     *
263
     * @param \DERHANSEN\SfEventMgt\Domain\Model\Event $event Event
264
     * @param \DERHANSEN\SfEventMgt\Domain\Model\Registration $registration Registration
265
     * @param RegistrationResult $result Result
266
     *
267
     * @return bool
268
     */
269
    public function checkRegistrationSuccess($event, $registration, &$result)
270
    {
271
        $success = true;
272
        if ($event->getEnableRegistration() === false) {
273
            $success = false;
274
            $result = RegistrationResult::REGISTRATION_NOT_ENABLED;
275
        } elseif ($event->getRegistrationDeadline() != null && $event->getRegistrationDeadline() < new \DateTime()) {
276
            $success = false;
277
            $result = RegistrationResult::REGISTRATION_FAILED_DEADLINE_EXPIRED;
278
        } elseif ($event->getStartdate() < new \DateTime()) {
279
            $success = false;
280
            $result = RegistrationResult::REGISTRATION_FAILED_EVENT_EXPIRED;
281
        } elseif ($event->getRegistration()->count() >= $event->getMaxParticipants()
282
            && $event->getMaxParticipants() > 0
283
        ) {
284
            $success = false;
285
            $result = RegistrationResult::REGISTRATION_FAILED_MAX_PARTICIPANTS;
286
        } elseif ($event->getFreePlaces() < $registration->getAmountOfRegistrations()
287
            && $event->getMaxParticipants() > 0
288
        ) {
289
            $success = false;
290
            $result = RegistrationResult::REGISTRATION_FAILED_NOT_ENOUGH_FREE_PLACES;
291
        } elseif ($event->getMaxRegistrationsPerUser() < $registration->getAmountOfRegistrations()) {
292
            $success = false;
293
            $result = RegistrationResult::REGISTRATION_FAILED_MAX_AMOUNT_REGISTRATIONS_EXCEEDED;
294
        } elseif ($event->getUniqueEmailCheck() &&
295
            $this->emailNotUnique($event, $registration->getEmail())
296
        ) {
297
            $success = false;
298
            $result = RegistrationResult::REGISTRATION_FAILED_EMAIL_NOT_UNIQUE;
299
        }
300
        return $success;
301
    }
302
303
    /**
304
     * Returns if the given e-mail is registered to the given event
305
     *
306
     * @param \DERHANSEN\SfEventMgt\Domain\Model\Event $event
307
     * @param string $email
308
     * @return bool
309
     */
310
    protected function emailNotUnique($event, $email)
311
    {
312
        $registrations = $this->registrationRepository->findEventRegistrationsByEmail($event, $email);
313
        return $registrations->count() >= 1;
314
    }
315
}