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