Completed
Push — development ( 6b83c1...4f1043 )
by Torben
11:42
created

findNotificationRegistrations()   B

Complexity

Conditions 9
Paths 8

Size

Total Lines 36

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 26
CRAP Score 9

Importance

Changes 0
Metric Value
dl 0
loc 36
rs 8.0555
c 0
b 0
f 0
ccs 26
cts 26
cp 1
cc 9
nc 8
nop 2
crap 9
1
<?php
2
namespace DERHANSEN\SfEventMgt\Domain\Repository;
3
4
/*
5
 * This file is part of the Extension "sf_event_mgt" for TYPO3 CMS.
6
 *
7
 * For the full copyright and license information, please read the
8
 * LICENSE.txt file that was distributed with this source code.
9
 */
10
11
use TYPO3\CMS\Core\Utility\GeneralUtility;
12
use TYPO3\CMS\Extbase\Persistence\Generic\Typo3QuerySettings;
13
14
/**
15
 * The repository for registrations
16
 *
17
 * @author Torben Hansen <[email protected]>
18
 */
19
class RegistrationRepository extends \TYPO3\CMS\Extbase\Persistence\Repository
20
{
21
    /**
22
     * Disable the use of storage records, because the StoragePage can be set
23
     * in the plugin
24
     *
25
     * @return void
26
     */
27
    public function initializeObject()
28
    {
29
        $this->defaultQuerySettings = $this->objectManager->get(Typo3QuerySettings::class);
30
        $this->defaultQuerySettings->setRespectStoragePage(false);
31
        $this->defaultQuerySettings->setRespectSysLanguage(false);
32
    }
33 44
34
    /**
35 44
     * Returns all registrations, where the confirmation date is less than the
36 44
     * given date
37 44
     *
38 44
     * @param \Datetime $dateNow Date
39
     *
40
     * @return \TYPO3\CMS\Extbase\Persistence\QueryResultInterface|array
41
     */
42
    public function findExpiredRegistrations($dateNow)
43
    {
44
        $constraints = [];
45
        $query = $this->createQuery();
46
        $constraints[] = $query->lessThanOrEqual('confirmationUntil', $dateNow);
47
        $constraints[] = $query->equals('confirmed', false);
48 6
49
        return $query->matching($query->logicalAnd($constraints))->execute();
50 6
    }
51 6
52 6
    /**
53 6
     * Returns all registrations for the given event with the given constraints
54 6
     * Constraints are combined with a logical AND
55
     *
56
     * @param \DERHANSEN\SfEventMgt\Domain\Model\Event $event Event
57
     * @param array $findConstraints FindConstraints
58
     *
59
     * @return array|\TYPO3\CMS\Extbase\Persistence\QueryResultInterface
60
     */
61
    public function findNotificationRegistrations($event, $findConstraints)
62
    {
63
        $constraints = [];
64
        $query = $this->createQuery();
65
        $constraints[] = $query->equals('event', $event);
66 22
        $constraints[] = $query->equals('ignoreNotifications', false);
67
68 22
        if (!is_array($findConstraints) || count($findConstraints) == 0) {
69 22
            return $query->matching($query->logicalAnd($constraints))->execute();
70 22
        }
71 22
72
        foreach ($findConstraints as $findConstraint => $value) {
73 22
            $condition = key($value);
74 8
            switch ($condition) {
75
                case 'equals':
76
                    $constraints[] = $query->equals($findConstraint, $value[$condition]);
77 14
                    break;
78 14
                case 'lessThan':
79
                    $constraints[] = $query->lessThan($findConstraint, $value[$condition]);
80 14
                    break;
81 4
                case 'lessThanOrEqual':
82 4
                    $constraints[] = $query->lessThanOrEqual($findConstraint, $value[$condition]);
83 12
                    break;
84 4
                case 'greaterThan':
85 4
                    $constraints[] = $query->greaterThan($findConstraint, $value[$condition]);
86 8
                    break;
87 2
                case 'greaterThanOrEqual':
88 2
                    $constraints[] = $query->greaterThanOrEqual($findConstraint, $value[$condition]);
89 6
                    break;
90 2
                default:
91 2
                    throw new \InvalidArgumentException('An error occured - Unknown condition: ' . $condition);
92 4
            }
93 2
        }
94 2
95 2
        return $query->matching($query->logicalAnd($constraints))->execute();
96 2
    }
97 2
98 12
    /**
99
     * Returns all registrations for the given event matching the given e-mail address
100 12
     *
101
     * @param \DERHANSEN\SfEventMgt\Domain\Model\Event $event Event
102
     * @param string $email E-Mail
103
     * @return array|\TYPO3\CMS\Extbase\Persistence\QueryResultInterface
104
     */
105
    public function findEventRegistrationsByEmail($event, $email)
106
    {
107
        $constraints = [];
108
        $query = $this->createQuery();
109
        $constraints[] = $query->equals('event', $event);
110 2
        $constraints[] = $query->equals('email', $email);
111
112 2
        return $query->matching($query->logicalAnd($constraints))->execute();
113 2
    }
114 2
115 2
    /**
116 2
     * Returns registrations for the given UserRegistrationDemand demand
117
     *
118
     * @param \DERHANSEN\SfEventMgt\Domain\Model\Dto\UserRegistrationDemand $demand
119
     * @return array|\TYPO3\CMS\Extbase\Persistence\QueryResultInterface
120
     */
121
    public function findRegistrationsByUserRegistrationDemand($demand)
122
    {
123
        if (!$demand->getUser()) {
124
            return [];
125 12
        }
126
        $constraints = [];
127 12
        $query = $this->createQuery();
128 2
        $this->setStoragePageConstraint($query, $demand, $constraints);
129
        $this->setDisplayModeConstraint($query, $demand, $constraints);
130 10
        $this->setUserConstraint($query, $demand, $constraints);
131 10
        $this->setOrderingsFromDemand($query, $demand);
132 10
133 10
        return $query->matching($query->logicalAnd($constraints))->execute();
134 10
    }
135 10
136 10
    /**
137
     * Sets the displayMode constraint to the given constraints array
138
     *
139
     * @param \TYPO3\CMS\Extbase\Persistence\QueryInterface $query Query
140
     * @param \DERHANSEN\SfEventMgt\Domain\Model\Dto\UserRegistrationDemand $demand
141
     * @param array $constraints Constraints
142
     *
143
     * @return void
144
     */
145
    protected function setDisplayModeConstraint($query, $demand, &$constraints)
146
    {
147
        switch ($demand->getDisplayMode()) {
148 10
            case 'future':
149
                $constraints[] = $query->greaterThan('event.startdate', $demand->getCurrentDateTime());
150 10
                break;
151 10
            case 'past':
152 2
                $constraints[] = $query->lessThanOrEqual('event.enddate', $demand->getCurrentDateTime());
153 2
                break;
154 8
            default:
155
        }
156
    }
157 8
158 10
    /**
159 10
     * Sets the storagePage constraint to the given constraints array
160
     *
161
     * @param \TYPO3\CMS\Extbase\Persistence\QueryInterface $query Query
162
     * @param \DERHANSEN\SfEventMgt\Domain\Model\Dto\UserRegistrationDemand $demand
163
     * @param array $constraints Constraints
164
     *
165
     * @return void
166
     */
167
    protected function setStoragePageConstraint($query, $demand, &$constraints)
168
    {
169
        if ($demand->getStoragePage() != '') {
170 10
            $pidList = GeneralUtility::intExplode(',', $demand->getStoragePage(), true);
171
            $constraints[] = $query->in('pid', $pidList);
172 10
        }
173 10
    }
174 10
175 10
    /**
176 10
     * Sets the user constraint to the given constraints array
177
     *
178
     * @param \TYPO3\CMS\Extbase\Persistence\QueryInterface $query Query
179
     * @param \DERHANSEN\SfEventMgt\Domain\Model\Dto\UserRegistrationDemand $demand
180
     * @param array $constraints Constraints
181
     *
182
     * @return void
183
     */
184
    protected function setUserConstraint($query, $demand, &$constraints)
185
    {
186
        if ($demand->getUser()) {
187 10
            $constraints[] = $query->equals('feUser', $demand->getUser());
188
        }
189 10
    }
190 10
191 10
    /**
192 10
     * Sets the ordering to the given query for the given demand
193
     *
194
     * @param \TYPO3\CMS\Extbase\Persistence\QueryInterface $query Query
195
     * @param \DERHANSEN\SfEventMgt\Domain\Model\Dto\UserRegistrationDemand $demand
196
     *
197
     * @return void
198
     */
199
    protected function setOrderingsFromDemand($query, $demand)
200
    {
201
        $orderings = [];
202 10
        if ($demand->getOrderField() != '' && $demand->getOrderDirection() != '') {
203
            $orderings[$demand->getOrderField()] = ((strtolower($demand->getOrderDirection()) == 'desc') ?
204 10
                \TYPO3\CMS\Extbase\Persistence\QueryInterface::ORDER_DESCENDING :
205 10
                \TYPO3\CMS\Extbase\Persistence\QueryInterface::ORDER_ASCENDING);
206 4
            $query->setOrderings($orderings);
207 4
        }
208 2
    }
209
}
210