Completed
Push — payment ( a2694a )
by Torben
42:06
created

EventRepository   B

Complexity

Total Complexity 45

Size/Duplication

Total Lines 280
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 7
Bugs 1 Features 2
Metric Value
wmc 45
c 7
b 1
f 2
lcom 1
cbo 3
dl 0
loc 280
rs 8.3673

13 Methods

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

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
    public function initializeObject()
46
    {
47
        $this->defaultQuerySettings = $this->objectManager->get('TYPO3\\CMS\\Extbase\\Persistence\\Generic\\Typo3QuerySettings');
48
        $this->defaultQuerySettings->setRespectStoragePage(false);
49
    }
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
    public function findDemanded(EventDemand $eventDemand)
59
    {
60
        $constraints = [];
61
        $query = $this->createQuery();
62
        $this->setStoragePageConstraint($query, $eventDemand, $constraints);
63
        $this->setDisplayModeConstraint($query, $eventDemand, $constraints);
64
        $this->setCategoryConstraint($query, $eventDemand, $constraints);
65
        $this->setLocationConstraint($query, $eventDemand, $constraints);
66
        $this->setLocationCityConstraint($query, $eventDemand, $constraints);
67
        $this->setLocationCountryConstraint($query, $eventDemand, $constraints);
68
        $this->setStartEndDateConstraint($query, $eventDemand, $constraints);
69
        $this->setSearchConstraint($query, $eventDemand, $constraints);
70
        $this->setTopEventConstraint($query, $eventDemand, $constraints);
71
        $this->setOrderingsFromDemand($query, $eventDemand);
72
73
        if (count($constraints) > 0) {
74
            $query->matching($query->logicalAnd($constraints));
75
        }
76
77
        $this->setQueryLimitFromDemand($query, $eventDemand);
78
        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
    protected function setQueryLimitFromDemand($query, EventDemand $eventDemand)
90
    {
91
        if ($eventDemand->getQueryLimit() != null &&
92
            MathUtility::canBeInterpretedAsInteger($eventDemand->getQueryLimit()) &&
93
            (int)$eventDemand->getQueryLimit() > 0
94
        ) {
95
            $query->setLimit((int)$eventDemand->getQueryLimit());
96
        }
97
    }
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
    protected function setOrderingsFromDemand($query, EventDemand $eventDemand)
108
    {
109
        $orderings = [];
110
        if ($eventDemand->getOrderField() != '' && $eventDemand->getOrderDirection() != '') {
111
            $orderings[$eventDemand->getOrderField()] = ((strtolower($eventDemand->getOrderDirection()) == 'desc') ?
112
                \TYPO3\CMS\Extbase\Persistence\QueryInterface::ORDER_DESCENDING :
113
                \TYPO3\CMS\Extbase\Persistence\QueryInterface::ORDER_ASCENDING);
114
            $query->setOrderings($orderings);
115
        }
116
    }
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
    protected function setStoragePageConstraint($query, $eventDemand, &$constraints)
128
    {
129
        if ($eventDemand->getStoragePage() != '') {
130
            $pidList = GeneralUtility::intExplode(',', $eventDemand->getStoragePage(), true);
131
            $constraints[] = $query->in('pid', $pidList);
132
        }
133
    }
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
    protected function setDisplayModeConstraint($query, $eventDemand, &$constraints)
145
    {
146
        switch ($eventDemand->getDisplayMode()) {
147
            case 'future':
148
                $constraints[] = $query->greaterThan('startdate', $eventDemand->getCurrentDateTime());
149
                break;
150
            case 'past':
151
                $constraints[] = $query->lessThanOrEqual('enddate', $eventDemand->getCurrentDateTime());
152
                break;
153
            default:
154
        }
155
    }
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
    protected function setCategoryConstraint($query, $eventDemand, &$constraints)
167
    {
168
        if ($eventDemand->getCategory() != '') {
169
            $categoryConstraints = [];
170
            if ($eventDemand->getIncludeSubcategories()) {
171
                $categoryList = CategoryService::getCategoryListWithChilds($eventDemand->getCategory());
172
                $categories = GeneralUtility::intExplode(',', $categoryList, true);
173
            } else {
174
                $categories = GeneralUtility::intExplode(',', $eventDemand->getCategory(), true);
175
            }
176
            foreach ($categories as $category) {
177
                $categoryConstraints[] = $query->contains('category', $category);
178
            }
179
            if (count($categoryConstraints) > 0) {
180
                $constraints[] = $query->logicalOr($categoryConstraints);
181
            }
182
        }
183
    }
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
    protected function setLocationConstraint($query, $eventDemand, &$constraints)
195
    {
196
        if ($eventDemand->getLocation() !== null && $eventDemand->getLocation() != '') {
197
            $constraints[] = $query->equals('location', $eventDemand->getLocation());
198
        }
199
    }
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
    protected function setLocationCityConstraint($query, $eventDemand, &$constraints)
211
    {
212
        if ($eventDemand->getLocationCity() !== null && $eventDemand->getLocationCity() != '') {
213
            $constraints[] = $query->equals('location.city', $eventDemand->getLocationCity());
214
        }
215
    }
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
    protected function setLocationCountryConstraint($query, $eventDemand, &$constraints)
227
    {
228
        if ($eventDemand->getLocationCountry() !== null && $eventDemand->getLocationCountry() != '') {
229
            $constraints[] = $query->equals('location.country', $eventDemand->getLocationCountry());
230
        }
231
    }
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
    protected function setStartEndDateConstraint($query, $eventDemand, &$constraints)
243
    {
244
        /* StartDate */
245
        if ($eventDemand->getSearchDemand() && $eventDemand->getSearchDemand()->getStartDate() !== null) {
246
            $constraints[] = $query->greaterThanOrEqual('startdate', $eventDemand->getSearchDemand()->getStartDate());
247
        }
248
249
        /* EndDate */
250
        if ($eventDemand->getSearchDemand() && $eventDemand->getSearchDemand()->getEndDate() !== null) {
251
            $constraints[] = $query->lessThanOrEqual('enddate', $eventDemand->getSearchDemand()->getEndDate());
252
        }
253
    }
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
    protected function setSearchConstraint($query, $eventDemand, &$constraints)
265
    {
266
        if ($eventDemand->getSearchDemand() &&
267
            $eventDemand->getSearchDemand()->getSearch() !== null &&
268
            $eventDemand->getSearchDemand()->getSearch() !== ''
269
        ) {
270
            $searchFields = GeneralUtility::trimExplode(',', $eventDemand->getSearchDemand()->getFields(), true);
271
            $searchConstraints = [];
272
273
            if (count($searchFields) === 0) {
274
                throw new \UnexpectedValueException('No search fields defined', 1318497755);
275
            }
276
277
            $searchSubject = $eventDemand->getSearchDemand()->getSearch();
278
            foreach ($searchFields as $field) {
279
                if (!empty($searchSubject)) {
280
                    $searchConstraints[] = $query->like($field, '%' . $searchSubject . '%', false);
281
                }
282
            }
283
284
            if (count($searchConstraints)) {
285
                $constraints[] = $query->logicalOr($searchConstraints);
286
            }
287
        }
288
    }
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
    protected function setTopEventConstraint($query, $eventDemand, &$constraints)
300
    {
301
        if ($eventDemand->getTopEventRestriction() > 0) {
302
            $constraints[] = $query->equals('topEvent', (bool)($eventDemand->getTopEventRestriction() - 1));
303
        }
304
    }
305
306
}