Completed
Push — master ( 1a67e9...ec29fe )
by Torben
42:38 queued 40:09
created

EventRepository   B

Complexity

Total Complexity 45

Size/Duplication

Total Lines 280
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 99.17%

Importance

Changes 5
Bugs 0 Features 2
Metric Value
wmc 45
c 5
b 0
f 2
lcom 1
cbo 3
dl 0
loc 280
ccs 119
cts 120
cp 0.9917
rs 8.3673

13 Methods

Rating   Name   Duplication   Size   Complexity  
A setQueryLimitFromDemand() 0 9 4
A setOrderingsFromDemand() 0 10 4
A initializeObject() 0 5 1
A setStoragePageConstraint() 0 7 2
A findDemanded() 0 22 2
A setLocationCityConstraint() 0 6 3
A setLocationCountryConstraint() 0 6 3
C setSearchConstraint() 0 25 8
A setTopEventConstraint() 0 6 2
A setDisplayModeConstraint() 0 12 3
B setCategoryConstraint() 0 18 5
A setLocationConstraint() 0 6 3
B setStartEndDateConstraint() 0 12 5

How to fix   Complexity   

Complex Class

Complex classes like EventRepository often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use EventRepository, and based on these observations, apply Extract Interface, too.

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\MathUtility;
18
use TYPO3\CMS\Core\Utility\GeneralUtility;
19
use DERHANSEN\SfEventMgt\Domain\Model\Dto\EventDemand;
20
use DERHANSEN\SfEventMgt\Service\CategoryService;
21
22
/**
23
 * The repository for Events
24
 *
25
 * @author Torben Hansen <[email protected]>
26
 */
27
class EventRepository extends \TYPO3\CMS\Extbase\Persistence\Repository
28
{
29
30
    /**
31
     * Set default sorting
32
     *
33
     * @var array
34
     */
35
    protected $defaultOrderings = [
36
        'startdate' => \TYPO3\CMS\Extbase\Persistence\QueryInterface::ORDER_ASCENDING,
37
    ];
38
39
    /**
40
     * Disable the use of storage records, because the StoragePage can be set
41
     * in the plugin
42
     *
43
     * @return void
44
     */
45 31
    public function initializeObject()
46
    {
47 31
        $this->defaultQuerySettings = $this->objectManager->get('TYPO3\\CMS\\Extbase\\Persistence\\Generic\\Typo3QuerySettings');
48 31
        $this->defaultQuerySettings->setRespectStoragePage(false);
49 31
    }
50
51
    /**
52
     * Returns the objects of this repository matching the given demand
53
     *
54
     * @param \DERHANSEN\SfEventMgt\Domain\Model\Dto\EventDemand $eventDemand EventDemand
55
     *
56
     * @return array|\TYPO3\CMS\Extbase\Persistence\QueryResultInterface QueryResultInterface
57
     */
58 30
    public function findDemanded(EventDemand $eventDemand)
59
    {
60 30
        $constraints = [];
61 30
        $query = $this->createQuery();
62 30
        $this->setStoragePageConstraint($query, $eventDemand, $constraints);
63 30
        $this->setDisplayModeConstraint($query, $eventDemand, $constraints);
64 30
        $this->setCategoryConstraint($query, $eventDemand, $constraints);
65 30
        $this->setLocationConstraint($query, $eventDemand, $constraints);
66 30
        $this->setLocationCityConstraint($query, $eventDemand, $constraints);
67 30
        $this->setLocationCountryConstraint($query, $eventDemand, $constraints);
68 30
        $this->setStartEndDateConstraint($query, $eventDemand, $constraints);
69 30
        $this->setSearchConstraint($query, $eventDemand, $constraints);
70 30
        $this->setTopEventConstraint($query, $eventDemand, $constraints);
71 30
        $this->setOrderingsFromDemand($query, $eventDemand);
72
73 30
        if (count($constraints) > 0) {
74 30
            $query->matching($query->logicalAnd($constraints));
75 30
        }
76
77 30
        $this->setQueryLimitFromDemand($query, $eventDemand);
78 30
        return $query->execute();
79
    }
80
81
    /**
82
     * Sets a query limit to the given query for the given demand
83
     *
84
     * @param \TYPO3\CMS\Extbase\Persistence\QueryInterface $query Query
85
     * @param \DERHANSEN\SfEventMgt\Domain\Model\Dto\EventDemand $eventDemand EventDemand
86
     *
87
     * @return void
88
     */
89 30
    protected function setQueryLimitFromDemand($query, EventDemand $eventDemand)
90
    {
91 30
        if ($eventDemand->getQueryLimit() != null &&
92 30
            MathUtility::canBeInterpretedAsInteger($eventDemand->getQueryLimit()) &&
93 1
            (int)$eventDemand->getQueryLimit() > 0
94 30
        ) {
95 1
            $query->setLimit((int)$eventDemand->getQueryLimit());
96 1
        }
97 30
    }
98
99
    /**
100
     * Sets the ordering to the given query for the given demand
101
     *
102
     * @param \TYPO3\CMS\Extbase\Persistence\QueryInterface $query Query
103
     * @param \DERHANSEN\SfEventMgt\Domain\Model\Dto\EventDemand $eventDemand EventDemand
104
     *
105
     * @return void
106
     */
107 30
    protected function setOrderingsFromDemand($query, EventDemand $eventDemand)
108
    {
109 30
        $orderings = [];
110 30
        if ($eventDemand->getOrderField() != '' && $eventDemand->getOrderDirection() != '') {
111 6
            $orderings[$eventDemand->getOrderField()] = ((strtolower($eventDemand->getOrderDirection()) == 'desc') ?
112 6
                \TYPO3\CMS\Extbase\Persistence\QueryInterface::ORDER_DESCENDING :
113 3
                \TYPO3\CMS\Extbase\Persistence\QueryInterface::ORDER_ASCENDING);
114 6
            $query->setOrderings($orderings);
115 6
        }
116 30
    }
117
118
    /**
119
     * Sets the storagePage constraint to the given constraints array
120
     *
121
     * @param \TYPO3\CMS\Extbase\Persistence\QueryInterface $query Query
122
     * @param \DERHANSEN\SfEventMgt\Domain\Model\Dto\EventDemand $eventDemand EventDemand
123
     * @param array $constraints Constraints
124
     *
125
     * @return void
126
     */
127 30
    protected function setStoragePageConstraint($query, $eventDemand, &$constraints)
128
    {
129 30
        if ($eventDemand->getStoragePage() != '') {
130 30
            $pidList = GeneralUtility::intExplode(',', $eventDemand->getStoragePage(), true);
131 30
            $constraints[] = $query->in('pid', $pidList);
132 30
        }
133 30
    }
134
135
    /**
136
     * Sets the displayMode constraint to the given constraints array
137
     *
138
     * @param \TYPO3\CMS\Extbase\Persistence\QueryInterface $query Query
139
     * @param \DERHANSEN\SfEventMgt\Domain\Model\Dto\EventDemand $eventDemand EventDemand
140
     * @param array $constraints Constraints
141
     *
142
     * @return void
143
     */
144 30
    protected function setDisplayModeConstraint($query, $eventDemand, &$constraints)
145
    {
146 30
        switch ($eventDemand->getDisplayMode()) {
147 30
            case 'future':
148 1
                $constraints[] = $query->greaterThan('startdate', $eventDemand->getCurrentDateTime());
149 1
                break;
150 29
            case 'past':
151 1
                $constraints[] = $query->lessThanOrEqual('enddate', $eventDemand->getCurrentDateTime());
152 1
                break;
153 28
            default:
154 30
        }
155 30
    }
156
157
    /**
158
     * Sets the category constraint to the given constraints array
159
     *
160
     * @param \TYPO3\CMS\Extbase\Persistence\QueryInterface $query Query
161
     * @param \DERHANSEN\SfEventMgt\Domain\Model\Dto\EventDemand $eventDemand EventDemand
162
     * @param array $constraints Constraints
163
     *
164
     * @return void
165
     */
166 30
    protected function setCategoryConstraint($query, $eventDemand, &$constraints)
167
    {
168 30
        if ($eventDemand->getCategory() != '') {
169 5
            $categoryConstraints = [];
170 5
            if ($eventDemand->getIncludeSubcategories()) {
171 1
                $categoryList = CategoryService::getCategoryListWithChilds($eventDemand->getCategory());
172 1
                $categories = GeneralUtility::intExplode(',', $categoryList, true);
173 1
            } else {
174 4
                $categories = GeneralUtility::intExplode(',', $eventDemand->getCategory(), true);
175
            }
176 5
            foreach ($categories as $category) {
177 5
                $categoryConstraints[] = $query->contains('category', $category);
178 5
            }
179 5
            if (count($categoryConstraints) > 0) {
180 5
                $constraints[] = $query->logicalOr($categoryConstraints);
181 5
            }
182 5
        }
183 30
    }
184
185
    /**
186
     * Sets the location constraint to the given constraints array
187
     *
188
     * @param \TYPO3\CMS\Extbase\Persistence\QueryInterface $query Query
189
     * @param \DERHANSEN\SfEventMgt\Domain\Model\Dto\EventDemand $eventDemand EventDemand
190
     * @param array $constraints Constraints
191
     *
192
     * @return void
193
     */
194 30
    protected function setLocationConstraint($query, $eventDemand, &$constraints)
195
    {
196 30
        if ($eventDemand->getLocation() !== null && $eventDemand->getLocation() != '') {
197 3
            $constraints[] = $query->equals('location', $eventDemand->getLocation());
198 3
        }
199 30
    }
200
201
    /**
202
     * Sets the location.city constraint to the given constraints array
203
     *
204
     * @param \TYPO3\CMS\Extbase\Persistence\QueryInterface $query Query
205
     * @param \DERHANSEN\SfEventMgt\Domain\Model\Dto\EventDemand $eventDemand EventDemand
206
     * @param array $constraints Constraints
207
     *
208
     * @return void
209
     */
210 30
    protected function setLocationCityConstraint($query, $eventDemand, &$constraints)
211
    {
212 30
        if ($eventDemand->getLocationCity() !== null && $eventDemand->getLocationCity() != '') {
213 2
            $constraints[] = $query->equals('location.city', $eventDemand->getLocationCity());
214 2
        }
215 30
    }
216
217
    /**
218
     * Sets the location.country constraint to the given constraints array
219
     *
220
     * @param \TYPO3\CMS\Extbase\Persistence\QueryInterface $query Query
221
     * @param \DERHANSEN\SfEventMgt\Domain\Model\Dto\EventDemand $eventDemand EventDemand
222
     * @param array $constraints Constraints
223
     *
224
     * @return void
225
     */
226 30
    protected function setLocationCountryConstraint($query, $eventDemand, &$constraints)
227
    {
228 30
        if ($eventDemand->getLocationCountry() !== null && $eventDemand->getLocationCountry() != '') {
229 2
            $constraints[] = $query->equals('location.country', $eventDemand->getLocationCountry());
230 2
        }
231 30
    }
232
233
    /**
234
     * Sets the start- and enddate constraint to the given constraints array
235
     *
236
     * @param \TYPO3\CMS\Extbase\Persistence\QueryInterface $query Query
237
     * @param \DERHANSEN\SfEventMgt\Domain\Model\Dto\EventDemand $eventDemand EventDemand
238
     * @param array $constraints Constraints
239
     *
240
     * @return void
241
     */
242 30
    protected function setStartEndDateConstraint($query, $eventDemand, &$constraints)
243
    {
244
        /* StartDate */
245 30
        if ($eventDemand->getSearchDemand() && $eventDemand->getSearchDemand()->getStartDate() !== null) {
246 1
            $constraints[] = $query->greaterThanOrEqual('startdate', $eventDemand->getSearchDemand()->getStartDate());
247 1
        }
248
249
        /* EndDate */
250 30
        if ($eventDemand->getSearchDemand() && $eventDemand->getSearchDemand()->getEndDate() !== null) {
251 1
            $constraints[] = $query->lessThanOrEqual('enddate', $eventDemand->getSearchDemand()->getEndDate());
252 1
        }
253 30
    }
254
255
    /**
256
     * Sets the search constraint to the given constraints array
257
     *
258
     * @param \TYPO3\CMS\Extbase\Persistence\QueryInterface $query Query
259
     * @param \DERHANSEN\SfEventMgt\Domain\Model\Dto\EventDemand $eventDemand EventDemand
260
     * @param array $constraints Constraints
261
     *
262
     * @return void
263
     */
264 30
    protected function setSearchConstraint($query, $eventDemand, &$constraints)
265
    {
266 30
        if ($eventDemand->getSearchDemand() &&
267 30
            $eventDemand->getSearchDemand()->getSearch() !== null &&
268 3
            $eventDemand->getSearchDemand()->getSearch() !== ''
269 30
        ) {
270 1
            $searchFields = GeneralUtility::trimExplode(',', $eventDemand->getSearchDemand()->getFields(), true);
271 1
            $searchConstraints = [];
272
273 1
            if (count($searchFields) === 0) {
274
                throw new \UnexpectedValueException('No search fields defined', 1318497755);
275
            }
276
277 1
            $searchSubject = $eventDemand->getSearchDemand()->getSearch();
278 1
            foreach ($searchFields as $field) {
279 1
                if (!empty($searchSubject)) {
280 1
                    $searchConstraints[] = $query->like($field, '%' . $searchSubject . '%', false);
281 1
                }
282 1
            }
283
284 1
            if (count($searchConstraints)) {
285 1
                $constraints[] = $query->logicalOr($searchConstraints);
286 1
            }
287 1
        }
288 30
    }
289
290
    /**
291
     * Sets the topEvent constraint to the given constraints array
292
     *
293
     * @param \TYPO3\CMS\Extbase\Persistence\QueryInterface $query Query
294
     * @param \DERHANSEN\SfEventMgt\Domain\Model\Dto\EventDemand $eventDemand EventDemand
295
     * @param array $constraints Constraints
296
     *
297
     * @return void
298
     */
299 30
    protected function setTopEventConstraint($query, $eventDemand, &$constraints)
300
    {
301 30
        if ($eventDemand->getTopEventRestriction() > 0) {
302 2
            $constraints[] = $query->equals('topEvent', (bool)($eventDemand->getTopEventRestriction() - 1));
303 2
        }
304 30
    }
305
306
}