Passed
Push — 6.x ( 798bcf...2c9878 )
by Torben
02:44
created

RegistrationRepository   A

Complexity

Total Complexity 30

Size/Duplication

Total Lines 200
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 1
Metric Value
eloc 95
c 3
b 0
f 1
dl 0
loc 200
rs 10
wmc 30

9 Methods

Rating   Name   Duplication   Size   Complexity  
A initializeObject() 0 4 1
A setDisplayModeConstraint() 0 21 4
A setUserConstraint() 0 7 2
C findNotificationRegistrations() 0 58 13
A setStoragePageConstraint() 0 8 2
A findRegistrationsByUserRegistrationDemand() 0 13 2
A findByEventAndWaitlist() 0 8 1
A findWaitlistMoveUpRegistrations() 0 11 1
A setOrderingsFromDemand() 0 8 4
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\Domain\Repository;
13
14
use DERHANSEN\SfEventMgt\Domain\Model\Dto\CustomNotification;
15
use DERHANSEN\SfEventMgt\Domain\Model\Dto\UserRegistrationDemand;
16
use DERHANSEN\SfEventMgt\Domain\Model\Event;
17
use TYPO3\CMS\Core\Utility\GeneralUtility;
18
use TYPO3\CMS\Extbase\Persistence\Generic\Typo3QuerySettings;
19
use TYPO3\CMS\Extbase\Persistence\QueryInterface;
20
use TYPO3\CMS\Extbase\Persistence\QueryResultInterface;
21
use TYPO3\CMS\Extbase\Persistence\Repository;
22
23
/**
24
 * The repository for registrations
25
 */
26
class RegistrationRepository extends Repository
27
{
28
    /**
29
     * Disable the use of storage records, because the StoragePage can be set
30
     * in the plugin
31
     */
32
    public function initializeObject(): void
33
    {
34
        $this->defaultQuerySettings = GeneralUtility::makeInstance(Typo3QuerySettings::class);
35
        $this->defaultQuerySettings->setRespectStoragePage(false);
36
    }
37
38
    /**
39
     * Returns all registrations for the given event with the given constraints
40
     * Constraints are combined with a logical AND
41
     *
42
     * @return array|QueryResultInterface
43
     */
44
    public function findNotificationRegistrations(
45
        Event $event,
46
        CustomNotification $customNotification,
47
        array $findConstraints = []
48
    ) {
49
        $constraints = [];
50
        $query = $this->createQuery();
51
        $constraints[] = $query->equals('event', $event);
52
        $constraints[] = $query->equals('ignoreNotifications', false);
53
54
        switch ($customNotification->getRecipients()) {
55
            case CustomNotification::RECIPIENTS_CONFIRMED:
56
                $constraints[] = $query->equals('confirmed', true);
57
                $constraints[] = $query->equals('waitlist', false);
58
                break;
59
            case CustomNotification::RECIPIENTS_UNCONFIRMED:
60
                $constraints[] = $query->equals('confirmed', false);
61
                $constraints[] = $query->equals('waitlist', false);
62
                break;
63
            case CustomNotification::RECIPIENTS_WAITLIST_CONFIRMED:
64
                $constraints[] = $query->equals('confirmed', true);
65
                $constraints[] = $query->equals('waitlist', true);
66
                break;
67
            case CustomNotification::RECIPIENTS_WAITLIST_UNCONFIRMED:
68
                $constraints[] = $query->equals('confirmed', false);
69
                $constraints[] = $query->equals('waitlist', true);
70
                break;
71
            default:
72
        }
73
74
        if (!is_array($findConstraints) || count($findConstraints) === 0) {
0 ignored issues
show
introduced by
The condition is_array($findConstraints) is always true.
Loading history...
75
            return $query->matching($query->logicalAnd($constraints))->execute();
76
        }
77
78
        foreach ($findConstraints as $findConstraint => $value) {
79
            $condition = key($value);
80
            switch ($condition) {
81
                case 'equals':
82
                    $constraints[] = $query->equals($findConstraint, $value[$condition]);
83
                    break;
84
                case 'lessThan':
85
                    $constraints[] = $query->lessThan($findConstraint, $value[$condition]);
86
                    break;
87
                case 'lessThanOrEqual':
88
                    $constraints[] = $query->lessThanOrEqual($findConstraint, $value[$condition]);
89
                    break;
90
                case 'greaterThan':
91
                    $constraints[] = $query->greaterThan($findConstraint, $value[$condition]);
92
                    break;
93
                case 'greaterThanOrEqual':
94
                    $constraints[] = $query->greaterThanOrEqual($findConstraint, $value[$condition]);
95
                    break;
96
                default:
97
                    throw new \InvalidArgumentException('An error occured - Unknown condition: ' . $condition);
98
            }
99
        }
100
101
        return $query->matching($query->logicalAnd($constraints))->execute();
102
    }
103
104
    /**
105
     * Returns registrations for the given UserRegistrationDemand demand
106
     *
107
     * @return array|QueryResultInterface
108
     */
109
    public function findRegistrationsByUserRegistrationDemand(UserRegistrationDemand $demand)
110
    {
111
        if (!$demand->getUser()) {
112
            return [];
113
        }
114
        $constraints = [];
115
        $query = $this->createQuery();
116
        $this->setStoragePageConstraint($query, $demand, $constraints);
117
        $this->setDisplayModeConstraint($query, $demand, $constraints);
118
        $this->setUserConstraint($query, $demand, $constraints);
119
        $this->setOrderingsFromDemand($query, $demand);
120
121
        return $query->matching($query->logicalAnd($constraints))->execute();
122
    }
123
124
    /**
125
     * Returns all registrations for the given event and where the waitlist flag is as given
126
     *
127
     * @return array|QueryResultInterface
128
     */
129
    public function findByEventAndWaitlist(Event $event, bool $waitlist = false)
130
    {
131
        $constraints = [];
132
        $query = $this->createQuery();
133
        $constraints[] = $query->equals('event', $event->getUid());
134
        $constraints[] = $query->equals('waitlist', $waitlist);
135
136
        return $query->matching($query->logicalAnd($constraints))->execute();
137
    }
138
139
    /**
140
     * Returns all potential move up registrations for the given event ordered by "registration_date"
141
     *
142
     * @return array|QueryResultInterface
143
     */
144
    public function findWaitlistMoveUpRegistrations(Event $event)
145
    {
146
        $constraints = [];
147
        $query = $this->createQuery();
148
        $constraints[] = $query->equals('event', $event->getUid());
149
        $constraints[] = $query->equals('waitlist', true);
150
        $constraints[] = $query->equals('confirmed', true);
151
        $constraints[] = $query->greaterThan('registrationDate', 0);
152
        $query->setOrderings(['registration_date' => QueryInterface::ORDER_ASCENDING]);
153
154
        return $query->matching($query->logicalAnd($constraints))->execute();
155
    }
156
157
    /**
158
     * Sets the displayMode constraint to the given constraints array
159
     */
160
    protected function setDisplayModeConstraint(
161
        QueryInterface $query,
162
        UserRegistrationDemand $demand,
163
        array &$constraints
164
    ): void {
165
        switch ($demand->getDisplayMode()) {
166
            case 'future':
167
                $constraints[] = $query->greaterThan('event.startdate', $demand->getCurrentDateTime());
168
                break;
169
            case 'current_future':
170
                $constraints[] = $query->logicalOr([
171
                    $query->greaterThan('event.startdate', $demand->getCurrentDateTime()),
172
                    $query->logicalAnd([
173
                        $query->greaterThanOrEqual('event.enddate', $demand->getCurrentDateTime()),
174
                        $query->lessThanOrEqual('event.startdate', $demand->getCurrentDateTime()),
175
                    ]),
176
                ]);
177
                break;
178
            case 'past':
179
                $constraints[] = $query->lessThanOrEqual('event.enddate', $demand->getCurrentDateTime());
180
                break;
181
        }
182
    }
183
184
    /**
185
     * Sets the storagePage constraint to the given constraints array
186
     */
187
    protected function setStoragePageConstraint(
188
        QueryInterface $query,
189
        UserRegistrationDemand $demand,
190
        array &$constraints
191
    ): void {
192
        if ($demand->getStoragePage() !== '') {
193
            $pidList = GeneralUtility::intExplode(',', $demand->getStoragePage(), true);
194
            $constraints[] = $query->in('pid', $pidList);
195
        }
196
    }
197
198
    /**
199
     * Sets the user constraint to the given constraints array
200
     *
201
     * @param QueryInterface $query Query
202
     * @param UserRegistrationDemand $demand
203
     * @param array $constraints Constraints
204
     */
205
    protected function setUserConstraint(
206
        QueryInterface $query,
207
        UserRegistrationDemand $demand,
208
        array &$constraints
209
    ): void {
210
        if ($demand->getUser()) {
211
            $constraints[] = $query->equals('feUser', $demand->getUser());
212
        }
213
    }
214
215
    /**
216
     * Sets the ordering to the given query for the given demand
217
     */
218
    protected function setOrderingsFromDemand(QueryInterface $query, UserRegistrationDemand $demand): void
219
    {
220
        $orderings = [];
221
        if ($demand->getOrderField() !== '' && $demand->getOrderDirection() !== '') {
222
            $orderings[$demand->getOrderField()] = ((strtolower($demand->getOrderDirection()) === 'desc') ?
223
                QueryInterface::ORDER_DESCENDING :
224
                QueryInterface::ORDER_ASCENDING);
225
            $query->setOrderings($orderings);
226
        }
227
    }
228
}
229