Total Complexity | 53 |
Total Lines | 466 |
Duplicated Lines | 0 % |
Changes | 10 | ||
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 |
||
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) |
||
238 | } |
||
239 | |||
240 | /** |
||
241 | * Sanitize input variables. |
||
242 | * |
||
243 | * @access protected |
||
244 | * |
||
245 | * @return void |
||
246 | */ |
||
247 | protected function sanitizeRequestData(): void |
||
270 | } |
||
271 | |||
272 | /** |
||
273 | * Sets page value. |
||
274 | * |
||
275 | * @access protected |
||
276 | * |
||
277 | * @return void |
||
278 | */ |
||
279 | protected function setPage(): void |
||
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 (empty($this->requestData['page'])) { |
||
302 | $this->requestData['page'] = 1; |
||
303 | } elseif ((int) $this->requestData['page'] > 0) { |
||
304 | $this->requestData['page'] = MathUtility::forceIntegerInRange((int) $this->requestData['page'], 1, $this->document->getCurrentDocument()->numPages, 1); |
||
305 | } else { |
||
306 | $this->requestData['page'] = array_search($this->requestData['page'], $this->document->getCurrentDocument()->physicalStructure); |
||
307 | } |
||
308 | // reassign viewData to get correct page |
||
309 | $this->viewData['requestData'] = $this->requestData; |
||
310 | } |
||
311 | |||
312 | /** |
||
313 | * This is the constructor |
||
314 | * |
||
315 | * @access public |
||
316 | * |
||
317 | * @return void |
||
318 | */ |
||
319 | public function __construct() |
||
322 | } |
||
323 | |||
324 | /** |
||
325 | * build simple pagination |
||
326 | * |
||
327 | * @param PaginationInterface $pagination |
||
328 | * @param PaginatorInterface $paginator |
||
329 | * @return array |
||
330 | */ |
||
331 | //TODO: clean this function |
||
332 | protected function buildSimplePagination(PaginationInterface $pagination, PaginatorInterface $paginator): array |
||
442 | ]; |
||
443 | } |
||
444 | |||
445 | /** |
||
446 | * Get document from repository by uid. |
||
447 | * |
||
448 | * @access private |
||
449 | * |
||
450 | * @param int $documentId The document's UID |
||
451 | * |
||
452 | * @return AbstractDocument |
||
453 | */ |
||
454 | private function getDocumentByUid(int $documentId) |
||
466 | } |
||
467 | |||
468 | /** |
||
469 | * Get document by URL. |
||
470 | * |
||
471 | * @access private |
||
472 | * |
||
473 | * @param string $documentId The document's URL |
||
474 | * |
||
475 | * @return AbstractDocument |
||
476 | */ |
||
477 | private function getDocumentByUrl(string $documentId) |
||
506 |