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 |
||
| 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) |
|
| 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) |
|
| 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) |
|
| 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) |
|
| 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) |
|
| 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) |
|
| 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) |
|
| 305 | |||
| 306 | } |