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