Completed
Push — task/821-remove-lang ( 1a66a2 )
by Torben
43:13
created

RegistrationRepository::findByUid()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
/*
4
 * This file is part of the Extension "sf_event_mgt" for TYPO3 CMS.
5
 *
6
 * For the full copyright and license information, please read the
7
 * LICENSE.txt file that was distributed with this source code.
8
 */
9
10
namespace DERHANSEN\SfEventMgt\Domain\Repository;
11
12
use DERHANSEN\SfEventMgt\Domain\Model\Dto\CustomNotification;
13
use DERHANSEN\SfEventMgt\Domain\Model\Event;
14
use TYPO3\CMS\Core\Utility\GeneralUtility;
15
use TYPO3\CMS\Extbase\Persistence\Generic\Typo3QuerySettings;
16
use TYPO3\CMS\Extbase\Persistence\QueryInterface;
17
18
/**
19
 * The repository for registrations
20
 *
21
 * @author Torben Hansen <[email protected]>
22
 */
23
class RegistrationRepository extends \TYPO3\CMS\Extbase\Persistence\Repository
24
{
25
    /**
26
     * Disable the use of storage records, because the StoragePage can be set
27
     * in the plugin
28
     */
29
    public function initializeObject()
30
    {
31
        $this->defaultQuerySettings = $this->objectManager->get(Typo3QuerySettings::class);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->objectManager->ge...o3QuerySettings::class) of type object<TYPO3\CMS\Extbase\Object\object> is incompatible with the declared type object<TYPO3\CMS\Extbase...QuerySettingsInterface> of property $defaultQuerySettings.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
Deprecated Code introduced by
The method TYPO3\CMS\Extbase\Object...ManagerInterface::get() has been deprecated with message: since TYPO3 10.4, will be removed in version 12.0

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
32
        $this->defaultQuerySettings->setRespectStoragePage(false);
33
    }
34
35
    /**
36
     * Returns all registrations, where the confirmation date is less than the
37
     * given date
38
     *
39
     * @param \Datetime $dateNow Date
40
     *
41
     * @return \TYPO3\CMS\Extbase\Persistence\QueryResultInterface|array
42
     */
43
    public function findExpiredRegistrations($dateNow)
44
    {
45
        $constraints = [];
46
        $query = $this->createQuery();
47
        $constraints[] = $query->lessThanOrEqual('confirmationUntil', $dateNow);
48
        $constraints[] = $query->equals('confirmed', false);
49
50
        return $query->matching($query->logicalAnd($constraints))->execute();
51
    }
52
53
    /**
54
     * Returns all registrations for the given event with the given constraints
55
     * Constraints are combined with a logical AND
56
     *
57
     * @param Event $event Event
58
     * @param CustomNotification $customNotification
59
     * @param array $findConstraints FindConstraints
60
     *
61
     * @return array|\TYPO3\CMS\Extbase\Persistence\QueryResultInterface
62
     */
63
    public function findNotificationRegistrations(
64
        Event $event,
65
        CustomNotification $customNotification,
66
        array $findConstraints = []
67
    ) {
68
        $constraints = [];
69
        $query = $this->createQuery();
70
        $constraints[] = $query->equals('event', $event);
71
        $constraints[] = $query->equals('ignoreNotifications', false);
72
73
        if ($customNotification->getRecipients() === CustomNotification::RECIPIENTS_CONFIRMED) {
74
            $constraints[] = $query->equals('confirmed', true);
75
        } elseif ($customNotification->getRecipients() === CustomNotification::RECIPIENTS_UNCONFIRMED) {
76
            $constraints[] = $query->equals('confirmed', false);
77
        }
78
79
        if (!is_array($findConstraints) || count($findConstraints) == 0) {
80
            return $query->matching($query->logicalAnd($constraints))->execute();
81
        }
82
83
        foreach ($findConstraints as $findConstraint => $value) {
84
            $condition = key($value);
85
            switch ($condition) {
86
                case 'equals':
87
                    $constraints[] = $query->equals($findConstraint, $value[$condition]);
88
                    break;
89
                case 'lessThan':
90
                    $constraints[] = $query->lessThan($findConstraint, $value[$condition]);
91
                    break;
92
                case 'lessThanOrEqual':
93
                    $constraints[] = $query->lessThanOrEqual($findConstraint, $value[$condition]);
94
                    break;
95
                case 'greaterThan':
96
                    $constraints[] = $query->greaterThan($findConstraint, $value[$condition]);
97
                    break;
98
                case 'greaterThanOrEqual':
99
                    $constraints[] = $query->greaterThanOrEqual($findConstraint, $value[$condition]);
100
                    break;
101
                default:
102
                    throw new \InvalidArgumentException('An error occured - Unknown condition: ' . $condition);
103
            }
104
        }
105
106
        return $query->matching($query->logicalAnd($constraints))->execute();
107
    }
108
109
    /**
110
     * Returns registrations for the given UserRegistrationDemand demand
111
     *
112
     * @param \DERHANSEN\SfEventMgt\Domain\Model\Dto\UserRegistrationDemand $demand
113
     * @return array|\TYPO3\CMS\Extbase\Persistence\QueryResultInterface
114
     */
115
    public function findRegistrationsByUserRegistrationDemand($demand)
116
    {
117
        if (!$demand->getUser()) {
118
            return [];
119
        }
120
        $constraints = [];
121
        $query = $this->createQuery();
122
        $this->setStoragePageConstraint($query, $demand, $constraints);
123
        $this->setDisplayModeConstraint($query, $demand, $constraints);
124
        $this->setUserConstraint($query, $demand, $constraints);
125
        $this->setOrderingsFromDemand($query, $demand);
126
127
        return $query->matching($query->logicalAnd($constraints))->execute();
128
    }
129
130
    /**
131
     * Returns all registrations for the given event and where the waitlist flag is as given
132
     *
133
     * @param Event $event
134
     * @param bool $waitlist
135
     * @return array|\TYPO3\CMS\Extbase\Persistence\QueryResultInterface
136
     */
137
    public function findByEventAndWaitlist($event, $waitlist = false)
138
    {
139
        $constraints = [];
140
        $query = $this->createQuery();
141
        $constraints[] = $query->equals('event', $event->getUid());
142
        $constraints[] = $query->equals('waitlist', $waitlist);
143
144
        return $query->matching($query->logicalAnd($constraints))->execute();
145
    }
146
147
    /**
148
     * Returns all potential move up registrations for the given event ordered by "registration_date"
149
     *
150
     * @param Event $event
151
     * @return array|\TYPO3\CMS\Extbase\Persistence\QueryResultInterface
152
     */
153
    public function findWaitlistMoveUpRegistrations(Event $event)
154
    {
155
        $constraints = [];
156
        $query = $this->createQuery();
157
        $constraints[] = $query->equals('event', $event->getUid());
158
        $constraints[] = $query->equals('waitlist', true);
159
        $constraints[] = $query->equals('confirmed', true);
160
        $constraints[] = $query->greaterThan('registrationDate', 0);
161
        $query->setOrderings(['registration_date' => QueryInterface::ORDER_ASCENDING]);
162
163
        return $query->matching($query->logicalAnd($constraints))->execute();
164
    }
165
166
    /**
167
     * Sets the displayMode constraint to the given constraints array
168
     *
169
     * @param \TYPO3\CMS\Extbase\Persistence\QueryInterface $query Query
170
     * @param \DERHANSEN\SfEventMgt\Domain\Model\Dto\UserRegistrationDemand $demand
171
     * @param array $constraints Constraints
172
     */
173
    protected function setDisplayModeConstraint($query, $demand, &$constraints)
174
    {
175
        switch ($demand->getDisplayMode()) {
176
            case 'future':
177
                $constraints[] = $query->greaterThan('event.startdate', $demand->getCurrentDateTime());
178
                break;
179
            case 'past':
180
                $constraints[] = $query->lessThanOrEqual('event.enddate', $demand->getCurrentDateTime());
181
                break;
182
            default:
183
        }
184
    }
185
186
    /**
187
     * Sets the storagePage constraint to the given constraints array
188
     *
189
     * @param \TYPO3\CMS\Extbase\Persistence\QueryInterface $query Query
190
     * @param \DERHANSEN\SfEventMgt\Domain\Model\Dto\UserRegistrationDemand $demand
191
     * @param array $constraints Constraints
192
     */
193
    protected function setStoragePageConstraint($query, $demand, &$constraints)
194
    {
195
        if ($demand->getStoragePage() != '') {
196
            $pidList = GeneralUtility::intExplode(',', $demand->getStoragePage(), true);
197
            $constraints[] = $query->in('pid', $pidList);
198
        }
199
    }
200
201
    /**
202
     * Sets the user constraint to the given constraints array
203
     *
204
     * @param \TYPO3\CMS\Extbase\Persistence\QueryInterface $query Query
205
     * @param \DERHANSEN\SfEventMgt\Domain\Model\Dto\UserRegistrationDemand $demand
206
     * @param array $constraints Constraints
207
     */
208
    protected function setUserConstraint($query, $demand, &$constraints)
209
    {
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
     * @param \TYPO3\CMS\Extbase\Persistence\QueryInterface $query Query
219
     * @param \DERHANSEN\SfEventMgt\Domain\Model\Dto\UserRegistrationDemand $demand
220
     */
221
    protected function setOrderingsFromDemand($query, $demand)
222
    {
223
        $orderings = [];
224
        if ($demand->getOrderField() != '' && $demand->getOrderDirection() != '') {
225
            $orderings[$demand->getOrderField()] = ((strtolower($demand->getOrderDirection()) == 'desc') ?
226
                \TYPO3\CMS\Extbase\Persistence\QueryInterface::ORDER_DESCENDING :
227
                \TYPO3\CMS\Extbase\Persistence\QueryInterface::ORDER_ASCENDING);
228
            $query->setOrderings($orderings);
229
        }
230
    }
231
}
232