Completed
Push — master ( 4cd89f...617684 )
by Torben
07:17
created

findRegistrationsByUserRegistrationDemand()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 2

Importance

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