Total Complexity | 53 |
Total Lines | 467 |
Duplicated Lines | 0 % |
Changes | 9 | ||
Bugs | 0 | Features | 0 |
Complex classes like AbstractController 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.
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 AbstractController, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
38 | abstract class AbstractController extends ActionController implements LoggerAwareInterface |
||
39 | { |
||
40 | use LoggerAwareTrait; |
||
41 | |||
42 | /** |
||
43 | * @access protected |
||
44 | * @var DocumentRepository |
||
45 | */ |
||
46 | protected DocumentRepository $documentRepository; |
||
47 | |||
48 | /** |
||
49 | * @access public |
||
50 | * |
||
51 | * @param DocumentRepository $documentRepository |
||
52 | * |
||
53 | * @return void |
||
54 | */ |
||
55 | public function injectDocumentRepository(DocumentRepository $documentRepository): void |
||
58 | } |
||
59 | |||
60 | /** |
||
61 | * @access protected |
||
62 | * @var Document|null This holds the current document |
||
63 | */ |
||
64 | protected ?Document $document = null; |
||
65 | |||
66 | /** |
||
67 | * @access protected |
||
68 | * @var array |
||
69 | */ |
||
70 | protected array $extConf; |
||
71 | |||
72 | /** |
||
73 | * @access protected |
||
74 | * @var array This holds the request parameter |
||
75 | */ |
||
76 | protected array $requestData; |
||
77 | |||
78 | /** |
||
79 | * @access protected |
||
80 | * @var array This holds some common data for the fluid view |
||
81 | */ |
||
82 | protected array $viewData; |
||
83 | |||
84 | /** |
||
85 | * @access protected |
||
86 | * @var int |
||
87 | */ |
||
88 | protected int $pageUid; |
||
89 | |||
90 | /** |
||
91 | * Initialize the plugin controller |
||
92 | * |
||
93 | * @access protected |
||
94 | * |
||
95 | * @return void |
||
96 | */ |
||
97 | protected function initialize(): void |
||
98 | { |
||
99 | $this->requestData = GeneralUtility::_GPmerged('tx_dlf'); |
||
100 | $this->pageUid = (int) GeneralUtility::_GET('id'); |
||
101 | |||
102 | // Sanitize user input to prevent XSS attacks. |
||
103 | $this->sanitizeRequestData(); |
||
104 | |||
105 | // Get extension configuration. |
||
106 | $this->extConf = GeneralUtility::makeInstance(ExtensionConfiguration::class)->get('dlf'); |
||
107 | |||
108 | $this->viewData = [ |
||
109 | 'pageUid' => $this->pageUid, |
||
110 | 'uniqueId' => uniqid(), |
||
111 | 'requestData' => $this->requestData |
||
112 | ]; |
||
113 | } |
||
114 | |||
115 | /** |
||
116 | * Loads the current document into $this->document |
||
117 | * |
||
118 | * @access protected |
||
119 | * |
||
120 | * @param int $documentId The document's UID (fallback: $this->requestData[id]) |
||
121 | * |
||
122 | * @return void |
||
123 | */ |
||
124 | protected function loadDocument(int $documentId = 0): void |
||
125 | { |
||
126 | // Get document ID from request data if not passed as parameter. |
||
127 | if ($documentId === 0 && !empty($this->requestData['id'])) { |
||
128 | $documentId = $this->requestData['id']; |
||
129 | } |
||
130 | |||
131 | // Try to get document format from database |
||
132 | if (!empty($documentId)) { |
||
133 | |||
134 | $doc = null; |
||
135 | |||
136 | if (MathUtility::canBeInterpretedAsInteger($documentId)) { |
||
137 | $doc = $this->getDocumentByUid($documentId); |
||
138 | } elseif (GeneralUtility::isValidUrl($documentId)) { |
||
139 | $doc = $this->getDocumentByUrl($documentId); |
||
140 | } |
||
141 | |||
142 | if ($this->document !== null && $doc !== null) { |
||
143 | $this->document->setCurrentDocument($doc); |
||
144 | } |
||
145 | |||
146 | } elseif (!empty($this->requestData['recordId'])) { |
||
147 | |||
148 | $this->document = $this->documentRepository->findOneByRecordId($this->requestData['recordId']); |
||
|
|||
149 | |||
150 | if ($this->document !== null) { |
||
151 | $doc = AbstractDocument::getInstance($this->document->getLocation(), $this->settings, true); |
||
152 | if ($doc !== null) { |
||
153 | $this->document->setCurrentDocument($doc); |
||
154 | } else { |
||
155 | $this->logger->error('Failed to load document with record ID "' . $this->requestData['recordId'] . '"'); |
||
156 | } |
||
157 | } |
||
158 | } else { |
||
159 | $this->logger->error('Invalid ID "' . $documentId . '" or PID "' . $this->settings['storagePid'] . '" for document loading'); |
||
160 | } |
||
161 | } |
||
162 | |||
163 | /** |
||
164 | * Configure URL for proxy. |
||
165 | * |
||
166 | * @access protected |
||
167 | * |
||
168 | * @param string $url URL for proxy configuration |
||
169 | * |
||
170 | * @return void |
||
171 | */ |
||
172 | protected function configureProxyUrl(string &$url): void |
||
173 | { |
||
174 | $this->uriBuilder->reset() |
||
175 | ->setTargetPageUid($this->pageUid) |
||
176 | ->setCreateAbsoluteUri(!empty($this->extConf['general']['forceAbsoluteUrl'])) |
||
177 | ->setArguments( |
||
178 | [ |
||
179 | 'eID' => 'tx_dlf_pageview_proxy', |
||
180 | 'url' => $url, |
||
181 | 'uHash' => GeneralUtility::hmac($url, 'PageViewProxy') |
||
182 | ] |
||
183 | ) |
||
184 | ->build(); |
||
185 | } |
||
186 | |||
187 | /** |
||
188 | * Checks if doc is missing or is empty (no pages) |
||
189 | * |
||
190 | * @access protected |
||
191 | * |
||
192 | * @return bool |
||
193 | */ |
||
194 | protected function isDocMissingOrEmpty(): bool |
||
195 | { |
||
196 | return $this->isDocMissing() || $this->document->getCurrentDocument()->numPages < 1; |
||
197 | } |
||
198 | |||
199 | /** |
||
200 | * Checks if doc is missing |
||
201 | * |
||
202 | * @access protected |
||
203 | * |
||
204 | * @return bool |
||
205 | */ |
||
206 | protected function isDocMissing(): bool |
||
207 | { |
||
208 | return $this->document === null || $this->document->getCurrentDocument() === null; |
||
209 | } |
||
210 | |||
211 | /** |
||
212 | * Returns the LanguageService |
||
213 | * |
||
214 | * @access protected |
||
215 | * |
||
216 | * @return LanguageService |
||
217 | */ |
||
218 | protected function getLanguageService(): LanguageService |
||
219 | { |
||
220 | return $GLOBALS['LANG']; |
||
221 | } |
||
222 | |||
223 | /** |
||
224 | * Safely gets Parameters from request if they exist |
||
225 | * |
||
226 | * @access protected |
||
227 | * |
||
228 | * @param string $parameterName |
||
229 | * |
||
230 | * @return null|string|array |
||
231 | */ |
||
232 | protected function getParametersSafely(string $parameterName) |
||
233 | { |
||
234 | if ($this->request->hasArgument($parameterName)) { |
||
235 | return $this->request->getArgument($parameterName); |
||
236 | } |
||
237 | return null; |
||
238 | } |
||
239 | |||
240 | /** |
||
241 | * Sanitize input variables. |
||
242 | * |
||
243 | * @access protected |
||
244 | * |
||
245 | * @return void |
||
246 | */ |
||
247 | protected function sanitizeRequestData(): void |
||
248 | { |
||
249 | // tx_dlf[id] may only be an UID or URI. |
||
250 | if ( |
||
251 | !empty($this->requestData['id']) |
||
252 | && !MathUtility::canBeInterpretedAsInteger($this->requestData['id']) |
||
253 | && !GeneralUtility::isValidUrl($this->requestData['id']) |
||
254 | ) { |
||
255 | $this->logger->warning('Invalid ID or URI "' . $this->requestData['id'] . '" for document loading'); |
||
256 | unset($this->requestData['id']); |
||
257 | } |
||
258 | |||
259 | // tx_dlf[page] may only be a positive integer or valid XML ID. |
||
260 | if ( |
||
261 | !empty($this->requestData['page']) |
||
262 | && !MathUtility::canBeInterpretedAsInteger($this->requestData['page']) |
||
263 | && !Helper::isValidXmlId($this->requestData['page']) |
||
264 | ) { |
||
265 | $this->requestData['page'] = 1; |
||
266 | } |
||
267 | |||
268 | // tx_dlf[double] may only be 0 or 1. |
||
269 | $this->requestData['double'] = MathUtility::forceIntegerInRange($this->requestData['double'] ?? 0, 0, 1); |
||
270 | } |
||
271 | |||
272 | /** |
||
273 | * Sets page value. |
||
274 | * |
||
275 | * @access protected |
||
276 | * |
||
277 | * @return void |
||
278 | */ |
||
279 | protected function setPage(): void |
||
280 | { |
||
281 | if (!empty($this->requestData['logicalPage'])) { |
||
282 | $this->requestData['page'] = $this->document->getCurrentDocument()->getPhysicalPage($this->requestData['logicalPage']); |
||
283 | // The logical page parameter should not appear again |
||
284 | unset($this->requestData['logicalPage']); |
||
285 | } |
||
286 | |||
287 | $this->setDefaultPage(); |
||
288 | } |
||
289 | |||
290 | /** |
||
291 | * Sets default page value. |
||
292 | * |
||
293 | * @access protected |
||
294 | * |
||
295 | * @return void |
||
296 | */ |
||
297 | protected function setDefaultPage(): void |
||
298 | { |
||
299 | // Set default values if not set. |
||
300 | // $this->requestData['page'] may be integer or string (physical structure @ID) |
||
301 | if ( |
||
302 | (int) $this->requestData['page'] > 0 |
||
303 | || empty($this->requestData['page']) |
||
304 | ) { |
||
305 | $this->requestData['page'] = MathUtility::forceIntegerInRange((int) $this->requestData['page'], 1, $this->document->getCurrentDocument()->numPages, 1); |
||
306 | } else { |
||
307 | $this->requestData['page'] = array_search($this->requestData['page'], $this->document->getCurrentDocument()->physicalStructure); |
||
308 | } |
||
309 | // reassign viewData to get correct page |
||
310 | $this->viewData['requestData'] = $this->requestData; |
||
311 | } |
||
312 | |||
313 | /** |
||
314 | * This is the constructor |
||
315 | * |
||
316 | * @access public |
||
317 | * |
||
318 | * @return void |
||
319 | */ |
||
320 | public function __construct() |
||
323 | } |
||
324 | |||
325 | /** |
||
326 | * build simple pagination |
||
327 | * |
||
328 | * @param PaginationInterface $pagination |
||
329 | * @param PaginatorInterface $paginator |
||
330 | * @return array |
||
331 | */ |
||
332 | //TODO: clean this function |
||
333 | protected function buildSimplePagination(PaginationInterface $pagination, PaginatorInterface $paginator): array |
||
443 | ]; |
||
444 | } |
||
445 | |||
446 | /** |
||
447 | * Get document from repository by uid. |
||
448 | * |
||
449 | * @access private |
||
450 | * |
||
451 | * @param int $documentId The document's UID |
||
452 | * |
||
453 | * @return AbstractDocument |
||
454 | */ |
||
455 | private function getDocumentByUid(int $documentId) |
||
467 | } |
||
468 | |||
469 | /** |
||
470 | * Get document by URL. |
||
471 | * |
||
472 | * @access private |
||
473 | * |
||
474 | * @param string $documentId The document's URL |
||
475 | * |
||
476 | * @return AbstractDocument |
||
477 | */ |
||
478 | private function getDocumentByUrl(string $documentId) |
||
507 |