We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.
| Total Complexity | 99 |
| Total Lines | 607 |
| Duplicated Lines | 0 % |
| Changes | 5 | ||
| Bugs | 0 | Features | 0 |
Complex classes like BasketController 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 BasketController, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 34 | class BasketController extends AbstractController |
||
| 35 | { |
||
| 36 | /** |
||
| 37 | * @var BasketRepository |
||
| 38 | */ |
||
| 39 | protected $basketRepository; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @param BasketRepository $basketRepository |
||
| 43 | */ |
||
| 44 | public function injectBasketRepository(BasketRepository $basketRepository) |
||
| 47 | } |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @var MailRepository |
||
| 51 | */ |
||
| 52 | protected $mailRepository; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * @param MailRepository $mailRepository |
||
| 56 | */ |
||
| 57 | public function injectMailRepository(MailRepository $mailRepository) |
||
| 58 | { |
||
| 59 | $this->mailRepository = $mailRepository; |
||
| 60 | } |
||
| 61 | |||
| 62 | /** |
||
| 63 | * @var PrinterRepository |
||
| 64 | */ |
||
| 65 | protected $printerRepository; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * @param PrinterRepository $printerRepository |
||
| 69 | */ |
||
| 70 | public function injectPrinterRepository(PrinterRepository $printerRepository) |
||
| 73 | } |
||
| 74 | |||
| 75 | /** |
||
| 76 | * @var ActionLogRepository |
||
| 77 | */ |
||
| 78 | protected $actionLogRepository; |
||
| 79 | |||
| 80 | /** |
||
| 81 | * @param ActionLogRepository $actionLogRepository |
||
| 82 | */ |
||
| 83 | public function injectActionLogRepository(ActionLogRepository $actionLogRepository) |
||
| 86 | } |
||
| 87 | |||
| 88 | /** |
||
| 89 | * Different actions which depends on the choosen action (form) |
||
| 90 | * |
||
| 91 | * @return void |
||
| 92 | */ |
||
| 93 | public function basketAction() |
||
| 94 | { |
||
| 95 | $basket = $this->getBasketData(); |
||
| 96 | |||
| 97 | // action remove from basket |
||
| 98 | if ($this->requestData['basket_action'] === 'remove') { |
||
| 99 | // remove entry from list |
||
| 100 | if (isset($this->requestData['selected'])) { |
||
| 101 | $basket = $this->removeFromBasket($this->requestData, $basket); |
||
| 102 | } |
||
| 103 | } |
||
| 104 | // action remove from basket |
||
| 105 | if ($this->requestData['basket_action'] == 'download') { |
||
| 106 | // open selected documents |
||
| 107 | if (isset($this->requestData['selected'])) { |
||
| 108 | $pdfUrl = $this->settings['pdfgenerate']; |
||
| 109 | foreach ($this->requestData['selected'] as $docValue) { |
||
| 110 | if ($docValue['id']) { |
||
| 111 | $docData = $this->getDocumentData($docValue['id'], $docValue); |
||
| 112 | $pdfUrl .= $docData['urlParams'] . $this->settings['pdfparamseparator']; |
||
| 113 | $this->redirectToUri($pdfUrl); |
||
| 114 | } |
||
| 115 | } |
||
| 116 | } |
||
| 117 | } |
||
| 118 | // action print from basket |
||
| 119 | if ($this->requestData['print_action']) { |
||
| 120 | // open selected documents |
||
| 121 | if (isset($this->requestData['selected'])) { |
||
| 122 | $this->printDocument($basket); |
||
| 123 | } |
||
| 124 | } |
||
| 125 | // action send mail |
||
| 126 | if ($this->requestData['mail_action']) { |
||
| 127 | if (isset($this->requestData['selected'])) { |
||
| 128 | $this->sendMail(); |
||
| 129 | } |
||
| 130 | } |
||
| 131 | |||
| 132 | $this->redirect('main'); |
||
| 133 | } |
||
| 134 | |||
| 135 | /** |
||
| 136 | * Add documents to the basket |
||
| 137 | * |
||
| 138 | * @return void |
||
| 139 | */ |
||
| 140 | public function addAction() |
||
| 141 | { |
||
| 142 | $basket = $this->getBasketData(); |
||
| 143 | |||
| 144 | if ( |
||
| 145 | !empty($this->requestData['id']) |
||
| 146 | && $this->requestData['addToBasket'] |
||
| 147 | ) { |
||
| 148 | $basket = $this->addToBasket($this->requestData, $basket); |
||
|
|
|||
| 149 | } |
||
| 150 | |||
| 151 | $this->redirect('main'); |
||
| 152 | } |
||
| 153 | |||
| 154 | /** |
||
| 155 | * The main method of the plugin |
||
| 156 | * |
||
| 157 | * @return void |
||
| 158 | */ |
||
| 159 | public function mainAction() |
||
| 160 | { |
||
| 161 | $basket = $this->getBasketData(); |
||
| 162 | |||
| 163 | $countDocs = 0; |
||
| 164 | if ($basket->getDocIds()) { |
||
| 165 | $countDocs = count(json_decode($basket->getDocIds(), true)); |
||
| 166 | } |
||
| 167 | $this->view->assign('countDocs', $countDocs); |
||
| 168 | |||
| 169 | $allMails = $this->mailRepository->findAllWithPid($this->settings['storagePid']); |
||
| 170 | |||
| 171 | $mailSelect = []; |
||
| 172 | if ($allMails->count() > 0) { |
||
| 173 | $mailSelect[0] = htmlspecialchars(LocalizationUtility::translate('basket.chooseMail', 'dlf')); |
||
| 174 | foreach ($allMails as $mail) { |
||
| 175 | $mailSelect[$mail->getUid()] = htmlspecialchars($mail->getName()) . ' (' . htmlspecialchars($mail->getMail()) . ')'; |
||
| 176 | } |
||
| 177 | $this->view->assign('mailSelect', $mailSelect); |
||
| 178 | } |
||
| 179 | |||
| 180 | $allPrinter = $this->printerRepository->findAll(); |
||
| 181 | |||
| 182 | $printSelect = []; |
||
| 183 | if ($allPrinter->count() > 0) { |
||
| 184 | $printSelect[0] = htmlspecialchars(LocalizationUtility::translate('basket.choosePrinter', 'dlf')); |
||
| 185 | foreach ($allPrinter as $printer) { |
||
| 186 | $printSelect[$printer->getUid()] = htmlspecialchars($printer->getLabel()); |
||
| 187 | } |
||
| 188 | $this->view->assign('printSelect', $printSelect); |
||
| 189 | } |
||
| 190 | |||
| 191 | $entries = []; |
||
| 192 | if ($basket->getDocIds()) { |
||
| 193 | // get each entry |
||
| 194 | foreach (json_decode($basket->getDocIds()) as $value) { |
||
| 195 | $entries[] = $this->getEntry($value); |
||
| 196 | } |
||
| 197 | $this->view->assign('entries', $entries); |
||
| 198 | } |
||
| 199 | } |
||
| 200 | |||
| 201 | /** |
||
| 202 | * The basket data from user session. |
||
| 203 | * |
||
| 204 | * @return Basket The found data from user session. |
||
| 205 | */ |
||
| 206 | protected function getBasketData() |
||
| 207 | { |
||
| 208 | // get user session |
||
| 209 | $sessionId = $GLOBALS['TSFE']->fe_user->id; |
||
| 210 | |||
| 211 | if ($GLOBALS['TSFE']->loginUser) { |
||
| 212 | $basket = $this->basketRepository->findOneByFeUserId((int) $GLOBALS['TSFE']->fe_user->user['uid']); |
||
| 213 | } else { |
||
| 214 | $GLOBALS['TSFE']->fe_user->setKey('ses', 'tx_dlf_basket', ''); |
||
| 215 | $GLOBALS['TSFE']->fe_user->sesData_change = true; |
||
| 216 | $GLOBALS['TSFE']->fe_user->storeSessionData(); |
||
| 217 | |||
| 218 | $basket = $this->basketRepository->findOneBySessionId($sessionId); |
||
| 219 | } |
||
| 220 | // session doesnt exists |
||
| 221 | if ($basket === null) { |
||
| 222 | // create new basket in db |
||
| 223 | $basket = GeneralUtility::makeInstance(Basket::class); |
||
| 224 | $basket->setSessionId($sessionId); |
||
| 225 | $basket->setFeUserId($GLOBALS['TSFE']->loginUser ? $GLOBALS['TSFE']->fe_user->user['uid'] : 0); |
||
| 226 | } |
||
| 227 | |||
| 228 | return $basket; |
||
| 229 | } |
||
| 230 | |||
| 231 | /** |
||
| 232 | * Return one basket entry |
||
| 233 | * |
||
| 234 | * @access protected |
||
| 235 | * |
||
| 236 | * @param array $data: DocumentData |
||
| 237 | * @param array $template: Template information |
||
| 238 | * |
||
| 239 | * @return string One basket entry |
||
| 240 | */ |
||
| 241 | protected function getEntry($data) |
||
| 242 | { |
||
| 243 | if (is_object($data)) { |
||
| 244 | $data = get_object_vars($data); |
||
| 245 | } |
||
| 246 | $id = $data['id']; |
||
| 247 | $startpage = $data['startpage']; |
||
| 248 | $endpage = $data['endpage']; |
||
| 249 | $startX = $data['startX']; |
||
| 250 | $startY = $data['startY']; |
||
| 251 | $endX = $data['endX']; |
||
| 252 | $endY = $data['endY']; |
||
| 253 | $rotation = $data['rotation']; |
||
| 254 | |||
| 255 | $docData = $this->getDocumentData($id, $data); |
||
| 256 | |||
| 257 | $entryArray['BASKETDATA'] = $docData; |
||
| 258 | |||
| 259 | $entryKey = $id . '_' . $startpage; |
||
| 260 | if (!empty($startX)) { |
||
| 261 | $entryKey .= '_' . $startX; |
||
| 262 | } |
||
| 263 | if (!empty($endX)) { |
||
| 264 | $entryKey .= '_' . $endX; |
||
| 265 | } |
||
| 266 | |||
| 267 | $entryArray['id'] = $id; |
||
| 268 | $entryArray['CONTROLS'] = [ |
||
| 269 | 'startpage' => $startpage, |
||
| 270 | 'endpage' => $endpage, |
||
| 271 | 'startX' => $startX, |
||
| 272 | 'startY' => $startY, |
||
| 273 | 'endX' => $endX, |
||
| 274 | 'endY' => $endY, |
||
| 275 | 'rotation' => $rotation, |
||
| 276 | ]; |
||
| 277 | |||
| 278 | $entryArray['NUMBER'] = $docData['record_id']; |
||
| 279 | $entryArray['key'] = $entryKey; |
||
| 280 | |||
| 281 | // return one entry |
||
| 282 | return $entryArray; |
||
| 283 | } |
||
| 284 | |||
| 285 | /** |
||
| 286 | * Returns the downloadurl configured in the basket |
||
| 287 | * |
||
| 288 | * @access protected |
||
| 289 | * |
||
| 290 | * @param int $id: Document id |
||
| 291 | * |
||
| 292 | * @return mixed download url or false |
||
| 293 | */ |
||
| 294 | protected function getDocumentData($id, $data) |
||
| 295 | { |
||
| 296 | // get document instance to load further information |
||
| 297 | $this->loadDocument(['id' => $id]); |
||
| 298 | if ($this->document) { |
||
| 299 | // replace url param placeholder |
||
| 300 | $urlParams = str_replace("##page##", (int) $data['page'], $this->settings['pdfparams']); |
||
| 301 | $urlParams = str_replace("##docId##", $this->document->getRecordId(), $urlParams); |
||
| 302 | $urlParams = str_replace("##startpage##", (int) $data['startpage'], $urlParams); |
||
| 303 | if ($data['startpage'] != $data['endpage']) { |
||
| 304 | $urlParams = str_replace("##endpage##", $data['endpage'] === "" ? "" : (int) $data['endpage'], $urlParams); |
||
| 305 | } else { |
||
| 306 | // remove parameter endpage |
||
| 307 | $urlParams = str_replace(",##endpage##", '', $urlParams); |
||
| 308 | } |
||
| 309 | $urlParams = str_replace("##startx##", $data['startX'] === "" ? "" : (int) $data['startX'], $urlParams); |
||
| 310 | $urlParams = str_replace("##starty##", $data['startY'] === "" ? "" : (int) $data['startY'], $urlParams); |
||
| 311 | $urlParams = str_replace("##endx##", $data['endX'] === "" ? "" : (int) $data['endX'], $urlParams); |
||
| 312 | $urlParams = str_replace("##endy##", $data['endY'] === "" ? "" : (int) $data['endY'], $urlParams); |
||
| 313 | $urlParams = str_replace("##rotation##", $data['rotation'] === "" ? "" : (int) $data['rotation'], $urlParams); |
||
| 314 | |||
| 315 | $downloadUrl = $this->settings['pdfgenerate'] . $urlParams; |
||
| 316 | |||
| 317 | $title = $this->document->getTitle(); |
||
| 318 | if (empty($title)) { |
||
| 319 | $title = LocalizationUtility::translate('basket.noTitle', 'dlf') ? : ''; |
||
| 320 | } |
||
| 321 | |||
| 322 | // Set page and cutout information |
||
| 323 | $info = ''; |
||
| 324 | if ($data['startX'] != '' && $data['endX'] != '') { |
||
| 325 | // cutout |
||
| 326 | $info .= htmlspecialchars(LocalizationUtility::translate('basket.cutout', 'dlf')) . ' '; |
||
| 327 | } |
||
| 328 | if ($data['startpage'] == $data['endpage']) { |
||
| 329 | // One page |
||
| 330 | $info .= htmlspecialchars(LocalizationUtility::translate('page', 'dlf')) . ' ' . $data['startpage']; |
||
| 331 | } else { |
||
| 332 | $info .= htmlspecialchars(LocalizationUtility::translate('page', 'dlf')) . ' ' . $data['startpage'] . '-' . $data['endpage']; |
||
| 333 | } |
||
| 334 | $downloadLink = '<a href="' . $downloadUrl . '" target="_blank">' . htmlspecialchars($title) . '</a> (' . $info . ')'; |
||
| 335 | if ($data['startpage'] == $data['endpage']) { |
||
| 336 | $pageNums = 1; |
||
| 337 | } else { |
||
| 338 | $pageNums = $data['endpage'] - $data['startpage']; |
||
| 339 | } |
||
| 340 | return [ |
||
| 341 | 'downloadUrl' => $downloadUrl, |
||
| 342 | 'title' => $title, |
||
| 343 | 'info' => $info, |
||
| 344 | 'downloadLink' => $downloadLink, |
||
| 345 | 'pageNums' => $pageNums, |
||
| 346 | 'urlParams' => $urlParams, |
||
| 347 | 'record_id' => $this->document->getRecordId(), |
||
| 348 | ]; |
||
| 349 | } |
||
| 350 | return false; |
||
| 351 | } |
||
| 352 | |||
| 353 | /** |
||
| 354 | * Adds documents to the basket |
||
| 355 | * |
||
| 356 | * @access protected |
||
| 357 | * |
||
| 358 | * @param array $_piVars: piVars |
||
| 359 | * @param Basket $basket: basket object |
||
| 360 | * |
||
| 361 | * @return array Basket data and Javascript output |
||
| 362 | */ |
||
| 363 | protected function addToBasket($_piVars, $basket) |
||
| 364 | { |
||
| 365 | $output = ''; |
||
| 366 | |||
| 367 | if (!$_piVars['startpage']) { |
||
| 368 | $page = 0; |
||
| 369 | } else { |
||
| 370 | $page = (int) $_piVars['startpage']; |
||
| 371 | } |
||
| 372 | if ($page != null || $_piVars['addToBasket'] == 'list') { |
||
| 373 | $documentItem = [ |
||
| 374 | 'id' => (int) $_piVars['id'], |
||
| 375 | 'startpage' => (int) $_piVars['startpage'], |
||
| 376 | 'endpage' => !isset($_piVars['endpage']) || $_piVars['endpage'] === "" ? "" : (int) $_piVars['endpage'], |
||
| 377 | 'startX' => !isset($_piVars['startX']) || $_piVars['startX'] === "" ? "" : (int) $_piVars['startX'], |
||
| 378 | 'startY' => !isset($_piVars['startY']) || $_piVars['startY'] === "" ? "" : (int) $_piVars['startY'], |
||
| 379 | 'endX' => !isset($_piVars['endX']) || $_piVars['endX'] === "" ? "" : (int) $_piVars['endX'], |
||
| 380 | 'endY' => !isset($_piVars['endY']) || $_piVars['endY'] === "" ? "" : (int) $_piVars['endY'], |
||
| 381 | 'rotation' => !isset($_piVars['rotation']) || $_piVars['rotation'] === "" ? "" : (int) $_piVars['rotation'] |
||
| 382 | ]; |
||
| 383 | |||
| 384 | // update basket |
||
| 385 | if (!empty(json_decode($basket->getDocIds()))) { |
||
| 386 | $items = json_decode($basket->getDocIds()); |
||
| 387 | $items = get_object_vars($items); |
||
| 388 | } else { |
||
| 389 | $items = []; |
||
| 390 | } |
||
| 391 | // get document instance to load further information |
||
| 392 | $this->loadDocument(['id' => $documentItem['id']]); |
||
| 393 | if ( |
||
| 394 | $this->document === null |
||
| 395 | || $this->document->getDoc() === null |
||
| 396 | ) { |
||
| 397 | // Quit without doing anything if required variables are not set. |
||
| 398 | return; |
||
| 399 | } |
||
| 400 | // set endpage for toc and subentry based on logid |
||
| 401 | if (($_piVars['addToBasket'] == 'subentry') or ($_piVars['addToBasket'] == 'toc')) { |
||
| 402 | $smLinks = $this->document->getDoc()->smLinks; |
||
| 403 | $pageCounter = sizeof($smLinks['l2p'][$_piVars['logId']]); |
||
| 404 | $documentItem['endpage'] = ($documentItem['startpage'] + $pageCounter) - 1; |
||
| 405 | } |
||
| 406 | // add whole document |
||
| 407 | if ($_piVars['addToBasket'] == 'list') { |
||
| 408 | $documentItem['endpage'] = $this->document->getDoc()->numPages; |
||
| 409 | } |
||
| 410 | $arrayKey = $documentItem['id'] . '_' . $page; |
||
| 411 | if (!empty($documentItem['startX'])) { |
||
| 412 | $arrayKey .= '_' . $documentItem['startX']; |
||
| 413 | } |
||
| 414 | if (!empty($documentItem['endX'])) { |
||
| 415 | $arrayKey .= '_' . $documentItem['endX']; |
||
| 416 | } |
||
| 417 | // do not add more than one identical object |
||
| 418 | if (!in_array($arrayKey, $items)) { |
||
| 419 | $items[$arrayKey] = $documentItem; |
||
| 420 | // replace url param placeholder |
||
| 421 | $pdfParams = str_replace("##startpage##", $documentItem['startpage'], $this->settings['pdfparams']); |
||
| 422 | $pdfParams = str_replace("##docId##", $this->document->getRecordId(), $pdfParams); |
||
| 423 | $pdfParams = str_replace("##startx##", $documentItem['startX'], $pdfParams); |
||
| 424 | $pdfParams = str_replace("##starty##", $documentItem['startY'], $pdfParams); |
||
| 425 | $pdfParams = str_replace("##endx##", $documentItem['endX'], $pdfParams); |
||
| 426 | $pdfParams = str_replace("##endy##", $documentItem['endY'], $pdfParams); |
||
| 427 | $pdfParams = str_replace("##rotation##", $documentItem['rotation'], $pdfParams); |
||
| 428 | if ($documentItem['startpage'] != $documentItem['endpage']) { |
||
| 429 | $pdfParams = str_replace("##endpage##", $documentItem['endpage'], $pdfParams); |
||
| 430 | } else { |
||
| 431 | // remove parameter endpage |
||
| 432 | $pdfParams = str_replace(",##endpage##", '', $pdfParams); |
||
| 433 | } |
||
| 434 | $pdfGenerateUrl = $this->settings['pdfgenerate'] . $pdfParams; |
||
| 435 | if ($this->settings['pregeneration']) { |
||
| 436 | // send ajax request to webapp |
||
| 437 | $output .= ' |
||
| 438 | <script> |
||
| 439 | $(document).ready(function(){ |
||
| 440 | $.ajax({ |
||
| 441 | url: "' . $pdfGenerateUrl . '", |
||
| 442 | }).done(function() { |
||
| 443 | }); |
||
| 444 | }); |
||
| 445 | </script>'; |
||
| 446 | } |
||
| 447 | } |
||
| 448 | |||
| 449 | $basket->setDocIds(json_encode($items)); |
||
| 450 | if ($basket->getUid() === null) { |
||
| 451 | $this->basketRepository->add($basket); |
||
| 452 | } else { |
||
| 453 | $this->basketRepository->update($basket); |
||
| 454 | } |
||
| 455 | } |
||
| 456 | $this->view->assign('pregenerateJs', $output); |
||
| 457 | |||
| 458 | return $basket; |
||
| 459 | } |
||
| 460 | |||
| 461 | /** |
||
| 462 | * Removes selected documents from basket |
||
| 463 | * |
||
| 464 | * @access protected |
||
| 465 | * |
||
| 466 | * @param array $_piVars: plugin variables |
||
| 467 | * @param Basket $basket: basket object |
||
| 468 | * |
||
| 469 | * @return Basket basket |
||
| 470 | */ |
||
| 471 | protected function removeFromBasket($_piVars, $basket) |
||
| 472 | { |
||
| 473 | if (!empty($basket->getDocIds())) { |
||
| 474 | $items = json_decode($basket->getDocIds()); |
||
| 475 | $items = get_object_vars($items); |
||
| 476 | } |
||
| 477 | foreach ($_piVars['selected'] as $value) { |
||
| 478 | if (isset($value['id'])) { |
||
| 479 | $arrayKey = $value['id'] . '_' . $value['startpage']; |
||
| 480 | if (!empty($value['startX'])) { |
||
| 481 | $arrayKey .= '_' . $value['startX']; |
||
| 482 | } |
||
| 483 | if (!empty($value['endX'])) { |
||
| 484 | $arrayKey .= '_' . $value['endX']; |
||
| 485 | } |
||
| 486 | if (isset($items[$arrayKey])) { |
||
| 487 | unset($items[$arrayKey]); |
||
| 488 | } |
||
| 489 | } |
||
| 490 | } |
||
| 491 | if (empty($items)) { |
||
| 492 | $update = ''; |
||
| 493 | } else { |
||
| 494 | $update = json_encode($items); |
||
| 495 | } |
||
| 496 | |||
| 497 | $basket->setDocIds($update); |
||
| 498 | $this->basketRepository->update($basket); |
||
| 499 | |||
| 500 | return $basket; |
||
| 501 | } |
||
| 502 | |||
| 503 | /** |
||
| 504 | * Send mail with pdf download url |
||
| 505 | * |
||
| 506 | * @access protected |
||
| 507 | * |
||
| 508 | * @return void |
||
| 509 | */ |
||
| 510 | protected function sendMail() |
||
| 578 | } |
||
| 579 | |||
| 580 | /** |
||
| 581 | * Sends document information to an external printer (url) |
||
| 582 | * |
||
| 583 | * @access protected |
||
| 584 | * @param Basket basket object |
||
| 585 | * |
||
| 586 | * @return void |
||
| 587 | */ |
||
| 588 | protected function printDocument($basket) |
||
| 641 | } |
||
| 642 | } |
||
| 643 |