Complex classes like EventController 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 EventController, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
32 | class EventController extends \TYPO3\CMS\Extbase\Mvc\Controller\ActionController |
||
33 | { |
||
34 | |||
35 | /** |
||
36 | * Configuration Manager |
||
37 | * |
||
38 | * @var ConfigurationManagerInterface |
||
39 | */ |
||
40 | protected $configurationManager; |
||
41 | |||
42 | /** |
||
43 | * EventRepository |
||
44 | * |
||
45 | * @var \DERHANSEN\SfEventMgt\Domain\Repository\EventRepository |
||
46 | * @inject |
||
47 | */ |
||
48 | protected $eventRepository = null; |
||
49 | |||
50 | /** |
||
51 | * Registration repository |
||
52 | * |
||
53 | * @var \DERHANSEN\SfEventMgt\Domain\Repository\RegistrationRepository |
||
54 | * @inject |
||
55 | */ |
||
56 | protected $registrationRepository = null; |
||
57 | |||
58 | /** |
||
59 | * Category repository |
||
60 | * |
||
61 | * @var \DERHANSEN\SfEventMgt\Domain\Repository\CategoryRepository |
||
62 | * @inject |
||
63 | */ |
||
64 | protected $categoryRepository = null; |
||
65 | |||
66 | /** |
||
67 | * Location repository |
||
68 | * |
||
69 | * @var \DERHANSEN\SfEventMgt\Domain\Repository\LocationRepository |
||
70 | * @inject |
||
71 | */ |
||
72 | protected $locationRepository = null; |
||
73 | |||
74 | /** |
||
75 | * Notification Service |
||
76 | * |
||
77 | * @var \DERHANSEN\SfEventMgt\Service\NotificationService |
||
78 | * @inject |
||
79 | */ |
||
80 | protected $notificationService = null; |
||
81 | |||
82 | /** |
||
83 | * ICalendar Service |
||
84 | * |
||
85 | * @var \DERHANSEN\SfEventMgt\Service\ICalendarService |
||
86 | * @inject |
||
87 | */ |
||
88 | protected $icalendarService = null; |
||
89 | |||
90 | /** |
||
91 | * Hash Service |
||
92 | * |
||
93 | * @var \TYPO3\CMS\Extbase\Security\Cryptography\HashService |
||
94 | * @inject |
||
95 | */ |
||
96 | protected $hashService; |
||
97 | |||
98 | /** |
||
99 | * RegistrationService |
||
100 | * |
||
101 | * @var \DERHANSEN\SfEventMgt\Service\RegistrationService |
||
102 | * @inject |
||
103 | */ |
||
104 | protected $registrationService = null; |
||
105 | |||
106 | /** |
||
107 | * UtilityService |
||
108 | * |
||
109 | * @var \DERHANSEN\SfEventMgt\Service\UtilityService |
||
110 | * @inject |
||
111 | */ |
||
112 | protected $utilityService = null; |
||
113 | |||
114 | /** |
||
115 | * Properties in this array will be ignored by overwriteDemandObject() |
||
116 | * |
||
117 | * @var array |
||
118 | */ |
||
119 | protected $ignoredSettingsForOverwriteDemand = ['storagePage']; |
||
120 | |||
121 | /** |
||
122 | * Creates an event demand object with the given settings |
||
123 | * |
||
124 | * @param array $settings The settings |
||
125 | * |
||
126 | * @return \DERHANSEN\SfEventMgt\Domain\Model\Dto\EventDemand |
||
127 | */ |
||
128 | 1 | public function createEventDemandObjectFromSettings(array $settings) |
|
129 | { |
||
130 | /** @var \DERHANSEN\SfEventMgt\Domain\Model\Dto\EventDemand $demand */ |
||
131 | 1 | $demand = $this->objectManager->get('DERHANSEN\\SfEventMgt\\Domain\\Model\\Dto\\EventDemand'); |
|
132 | 1 | $demand->setDisplayMode($settings['displayMode']); |
|
133 | 1 | $demand->setStoragePage(Page::extendPidListByChildren($settings['storagePage'], $settings['recursive'])); |
|
134 | 1 | $demand->setCategory($settings['category']); |
|
135 | 1 | $demand->setTopEventRestriction((int)$settings['topEventRestriction']); |
|
136 | 1 | $demand->setOrderField($settings['orderField']); |
|
137 | 1 | $demand->setOrderDirection($settings['orderDirection']); |
|
138 | 1 | $demand->setQueryLimit($settings['queryLimit']); |
|
139 | 1 | $demand->setLocation($settings['location']); |
|
140 | 1 | return $demand; |
|
141 | } |
||
142 | |||
143 | /** |
||
144 | * Creates a foreign record demand object with the given settings |
||
145 | * |
||
146 | * @param array $settings The settings |
||
147 | * |
||
148 | * @return \DERHANSEN\SfEventMgt\Domain\Model\Dto\ForeignRecordDemand |
||
149 | */ |
||
150 | public function createForeignRecordDemandObjectFromSettings(array $settings) |
||
151 | { |
||
152 | /** @var \DERHANSEN\SfEventMgt\Domain\Model\Dto\ForeignRecordDemand $demand */ |
||
153 | $demand = $this->objectManager->get('DERHANSEN\\SfEventMgt\\Domain\\Model\\Dto\\ForeignRecordDemand'); |
||
154 | $demand->setStoragePage(Page::extendPidListByChildren($settings['storagePage'], $settings['recursive'])); |
||
155 | $demand->setRestrictForeignRecordsToStoragePage((int)$settings['restrictForeignRecordsToStoragePage']); |
||
156 | return $demand; |
||
157 | } |
||
158 | |||
159 | /** |
||
160 | * Overwrites a given demand object by an propertyName => $propertyValue array |
||
161 | * |
||
162 | * @param \DERHANSEN\SfEventMgt\Domain\Model\Dto\EventDemand $demand Demand |
||
163 | * @param array $overwriteDemand OwerwriteDemand |
||
164 | * |
||
165 | * @return \DERHANSEN\SfEventMgt\Domain\Model\Dto\EventDemand |
||
166 | */ |
||
167 | 2 | protected function overwriteEventDemandObject(EventDemand $demand, array $overwriteDemand) |
|
168 | { |
||
169 | 2 | foreach ($this->ignoredSettingsForOverwriteDemand as $property) { |
|
170 | 2 | unset($overwriteDemand[$property]); |
|
171 | 2 | } |
|
172 | |||
173 | 2 | foreach ($overwriteDemand as $propertyName => $propertyValue) { |
|
174 | 2 | \TYPO3\CMS\Extbase\Reflection\ObjectAccess::setProperty($demand, $propertyName, $propertyValue); |
|
175 | 2 | } |
|
176 | 2 | return $demand; |
|
177 | } |
||
178 | |||
179 | /** |
||
180 | * List view |
||
181 | * |
||
182 | * @param array $overwriteDemand OverwriteDemand |
||
183 | * |
||
184 | * @return void |
||
185 | */ |
||
186 | 3 | public function listAction(array $overwriteDemand = []) |
|
187 | { |
||
188 | 3 | $eventDemand = $this->createEventDemandObjectFromSettings($this->settings); |
|
189 | 3 | $foreignRecordDemand = $this->createForeignRecordDemandObjectFromSettings($this->settings); |
|
190 | 3 | if ($this->isOverwriteDemand($overwriteDemand)) { |
|
191 | 1 | $eventDemand = $this->overwriteEventDemandObject($eventDemand, $overwriteDemand); |
|
192 | 1 | } |
|
193 | 3 | $events = $this->eventRepository->findDemanded($eventDemand); |
|
194 | 3 | $categories = $this->categoryRepository->findDemanded($foreignRecordDemand); |
|
195 | 3 | $locations = $this->locationRepository->findDemanded($foreignRecordDemand); |
|
196 | 3 | $this->view->assign('events', $events); |
|
197 | 3 | $this->view->assign('categories', $categories); |
|
198 | 3 | $this->view->assign('locations', $locations); |
|
199 | 3 | $this->view->assign('overwriteDemand', $overwriteDemand); |
|
200 | 3 | } |
|
201 | |||
202 | /** |
||
203 | * Detail view for an event |
||
204 | * |
||
205 | * @param \DERHANSEN\SfEventMgt\Domain\Model\Event $event Event |
||
206 | * |
||
207 | * @return void |
||
208 | */ |
||
209 | 1 | public function detailAction(Event $event = null) |
|
213 | |||
214 | /** |
||
215 | * Initiates the iCalendar download for the given event |
||
216 | * |
||
217 | * @param Event $event The event |
||
218 | * |
||
219 | * @return bool |
||
220 | */ |
||
221 | 1 | public function icalDownloadAction(Event $event) |
|
222 | { |
||
223 | 1 | $this->icalendarService->downloadiCalendarFile($event); |
|
224 | 1 | return false; |
|
225 | } |
||
226 | |||
227 | /** |
||
228 | * Registration view for an event |
||
229 | * |
||
230 | * @param \DERHANSEN\SfEventMgt\Domain\Model\Event $event Event |
||
231 | * |
||
232 | * @return void |
||
233 | */ |
||
234 | 1 | public function registrationAction(Event $event) |
|
238 | |||
239 | /** |
||
240 | * Set date format for field dateOfBirth |
||
241 | * |
||
242 | * @return void |
||
243 | */ |
||
244 | 1 | public function initializeSaveRegistrationAction() |
|
254 | |||
255 | /** |
||
256 | * Saves the registration |
||
257 | * |
||
258 | * @param \DERHANSEN\SfEventMgt\Domain\Model\Registration $registration Registration |
||
259 | * @param \DERHANSEN\SfEventMgt\Domain\Model\Event $event Event |
||
260 | * @validate $registration \DERHANSEN\SfEventMgt\Validation\Validator\RegistrationValidator |
||
261 | * |
||
262 | * @return void |
||
263 | */ |
||
264 | 10 | public function saveRegistrationAction(Registration $registration, Event $event) |
|
339 | |||
340 | /** |
||
341 | * Shows the result of the saveRegistrationAction |
||
342 | * |
||
343 | * @param int $result Result |
||
344 | * |
||
345 | * @return void |
||
346 | */ |
||
347 | 9 | public function saveRegistrationResultAction($result) |
|
390 | |||
391 | /** |
||
392 | * Confirms the registration if possible and sends e-mails to admin and user |
||
393 | * |
||
394 | * @param int $reguid UID of registration |
||
395 | * @param string $hmac HMAC for parameters |
||
396 | * |
||
397 | * @return void |
||
398 | */ |
||
399 | 2 | public function confirmRegistrationAction($reguid, $hmac) |
|
430 | |||
431 | /** |
||
432 | * Cancels the registration if possible and sends e-mails to admin and user |
||
433 | * |
||
434 | * @param int $reguid UID of registration |
||
435 | * @param string $hmac HMAC for parameters |
||
436 | * |
||
437 | * @return void |
||
438 | */ |
||
439 | 2 | public function cancelRegistrationAction($reguid, $hmac) |
|
473 | |||
474 | /** |
||
475 | * Set date format for field startDate and endDate |
||
476 | * |
||
477 | * @return void |
||
478 | */ |
||
479 | 1 | public function initializeSearchAction() |
|
498 | |||
499 | /** |
||
500 | * Search view |
||
501 | * |
||
502 | * @param \DERHANSEN\SfEventMgt\Domain\Model\Dto\SearchDemand $searchDemand SearchDemand |
||
503 | * @param array $overwriteDemand OverwriteDemand |
||
504 | * |
||
505 | * @return void |
||
506 | */ |
||
507 | 6 | public function searchAction(SearchDemand $searchDemand = null, array $overwriteDemand = []) |
|
540 | |||
541 | /** |
||
542 | * Returns if a demand object can be overwritten with the given overwriteDemand array |
||
543 | * |
||
544 | * @param array $overwriteDemand |
||
545 | * @return bool |
||
546 | */ |
||
547 | 9 | protected function isOverwriteDemand($overwriteDemand) |
|
551 | |||
552 | } |
||
553 |