Scrutinizer GitHub App not installed

We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.

Install GitHub App

GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Passed
Pull Request — master (#715)
by Alexander
03:15
created

BasketController   F

Complexity

Total Complexity 99

Size/Duplication

Total Lines 607
Duplicated Lines 0 %

Importance

Changes 5
Bugs 0 Features 0
Metric Value
eloc 292
c 5
b 0
f 0
dl 0
loc 607
rs 2
wmc 99

14 Methods

Rating   Name   Duplication   Size   Complexity  
A injectBasketRepository() 0 3 1
A injectPrinterRepository() 0 3 1
A injectActionLogRepository() 0 3 1
A injectMailRepository() 0 3 1
B sendMail() 0 68 7
B printDocument() 0 53 7
B removeFromBasket() 0 30 8
C getDocumentData() 0 57 15
A getEntry() 0 42 4
B mainAction() 0 39 8
B basketAction() 0 40 11
A getBasketData() 0 23 4
F addToBasket() 0 96 28
A addAction() 0 12 3

How to fix   Complexity   

Complex Class

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
2
3
/**
4
 * (c) Kitodo. Key to digital objects e.V. <[email protected]>
5
 *
6
 * This file is part of the Kitodo and TYPO3 projects.
7
 *
8
 * @license GNU General Public License version 3 or later.
9
 * For the full copyright and license information, please read the
10
 * LICENSE.txt file that was distributed with this source code.
11
 */
12
13
namespace Kitodo\Dlf\Controller;
14
15
use Kitodo\Dlf\Common\Doc;
16
use Kitodo\Dlf\Common\Helper;
17
use Kitodo\Dlf\Domain\Model\ActionLog;
18
use Kitodo\Dlf\Domain\Model\Basket;
19
use Kitodo\Dlf\Domain\Repository\ActionLogRepository;
20
use Kitodo\Dlf\Domain\Repository\MailRepository;
21
use Kitodo\Dlf\Domain\Repository\BasketRepository;
22
use Kitodo\Dlf\Domain\Repository\PrinterRepository;
23
use TYPO3\CMS\Core\Utility\GeneralUtility;
24
use TYPO3\CMS\Extbase\Utility\LocalizationUtility;
25
26
/**
27
 * Controller class for the plugin 'Basket'.
28
 *
29
 * @author Christopher Timm <[email protected]>
30
 * @package TYPO3
31
 * @subpackage dlf
32
 * @access public
33
 */
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)
45
    {
46
        $this->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)
71
    {
72
        $this->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)
84
    {
85
        $this->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);
0 ignored issues
show
Unused Code introduced by
The assignment to $basket is dead and can be removed.
Loading history...
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'));
0 ignored issues
show
Bug introduced by
It seems like TYPO3\CMS\Extbase\Utilit...ket.chooseMail', 'dlf') can also be of type null; however, parameter $string of htmlspecialchars() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

173
            $mailSelect[0] = htmlspecialchars(/** @scrutinizer ignore-type */ LocalizationUtility::translate('basket.chooseMail', 'dlf'));
Loading history...
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']);
0 ignored issues
show
Bug introduced by
The method findOneByFeUserId() does not exist on Kitodo\Dlf\Domain\Repository\BasketRepository. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

212
            /** @scrutinizer ignore-call */ 
213
            $basket = $this->basketRepository->findOneByFeUserId((int) $GLOBALS['TSFE']->fe_user->user['uid']);
Loading history...
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);
0 ignored issues
show
Bug introduced by
The method findOneBySessionId() does not exist on Kitodo\Dlf\Domain\Repository\BasketRepository. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

218
            /** @scrutinizer ignore-call */ 
219
            $basket = $this->basketRepository->findOneBySessionId($sessionId);
Loading history...
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;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $basket also could return the type TYPO3\CMS\Extbase\Persis...y<mixed,object>|integer which is incompatible with the documented return type Kitodo\Dlf\Domain\Model\Basket.
Loading history...
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')) . ' ';
0 ignored issues
show
Bug introduced by
It seems like TYPO3\CMS\Extbase\Utilit...'basket.cutout', 'dlf') can also be of type null; however, parameter $string of htmlspecialchars() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

326
                $info .= htmlspecialchars(/** @scrutinizer ignore-type */ LocalizationUtility::translate('basket.cutout', 'dlf')) . ' ';
Loading history...
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()
511
    {
512
        // send mail
513
        $mailId = $this->requestData['mail_action'];
514
515
        $mailObject = $this->mailRepository->findByUid(intval($mailId))->getFirst();
516
517
        $mailText = htmlspecialchars(LocalizationUtility::translate('basket.mailBody', 'dlf')) . "\n";
0 ignored issues
show
Bug introduced by
It seems like TYPO3\CMS\Extbase\Utilit...asket.mailBody', 'dlf') can also be of type null; however, parameter $string of htmlspecialchars() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

517
        $mailText = htmlspecialchars(/** @scrutinizer ignore-type */ LocalizationUtility::translate('basket.mailBody', 'dlf')) . "\n";
Loading history...
518
        $numberOfPages = 0;
519
        $pdfUrl = $this->settings['pdfdownload'];
520
        // prepare links
521
        foreach ($this->requestData['selected'] as $docValue) {
522
            if ($docValue['id']) {
523
                $explodeId = explode("_", $docValue['id']);
524
                $docData = $this->getDocumentData($explodeId[0], $docValue);
525
                $pdfUrl .= $docData['urlParams'] . $this->settings['pdfparamseparator'];
526
                $pages = (abs(intval($docValue['startpage']) - intval($docValue['endpage'])));
527
                if ($pages === 0) {
528
                    $numberOfPages = $numberOfPages + 1;
529
                } else {
530
                    $numberOfPages = $numberOfPages + $pages;
531
                }
532
            }
533
        }
534
        // Remove leading/tailing pdfparamseperator
535
        $pdfUrl = trim($pdfUrl, $this->settings['pdfparamseparator']);
536
        $mailBody = $mailText . $pdfUrl;
537
        // Get hook objects.
538
        $hookObjects = Helper::getHookObjects($this->scriptRelPath);
0 ignored issues
show
Bug Best Practice introduced by
The property scriptRelPath does not exist on Kitodo\Dlf\Controller\BasketController. Did you maybe forget to declare it?
Loading history...
539
        // Hook for getting a customized mail body.
540
        foreach ($hookObjects as $hookObj) {
541
            if (method_exists($hookObj, 'customizeMailBody')) {
542
                $mailBody = $hookObj->customizeMailBody($mailText, $pdfUrl);
543
            }
544
        }
545
        $from = \TYPO3\CMS\Core\Utility\MailUtility::getSystemFrom();
546
        // send mail
547
        $mail = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Mail\MailMessage::class);
548
        // Prepare and send the message
549
        $mail
550
            // subject
551
            ->setSubject(LocalizationUtility::translate('basket.mailSubject', 'dlf'))
552
            // Set the From address with an associative array
553
            ->setFrom($from)
554
            // Set the To addresses with an associative array
555
            ->setTo([$mailObject->getMail() => $mailObject->getName()])
556
            ->setBody($mailBody, 'text/html')
557
            ->send();
558
559
        // create entry for action log
560
        $newActionLog = GeneralUtility::makeInstance(ActionLog::class);
561
        $newActionLog->setFileName($pdfUrl);
562
        $newActionLog->setCountPages($numberOfPages);
563
        $newActionLog->setLabel('Mail: ' . $mailObject->getMail());
564
565
        if ($GLOBALS["TSFE"]->loginUser) {
566
            // internal user
567
            $newActionLog->setUserId($GLOBALS["TSFE"]->fe_user->user['uid']);
568
            $newActionLog->setName($GLOBALS["TSFE"]->fe_user->user['username']);
569
        } else {
570
            // external user
571
            $newActionLog->setUserId(0);
572
            $newActionLog->setName('n/a');
573
        }
574
575
        $this->actionLogRepository->add($newActionLog);
576
577
        $this->redirect('main');
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)
0 ignored issues
show
Unused Code introduced by
The parameter $basket is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

588
    protected function printDocument(/** @scrutinizer ignore-unused */ $basket)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
589
    {
590
        $pdfUrl = $this->settings['pdfprint'];
591
        $numberOfPages = 0;
592
        foreach ($this->requestData['selected'] as $docId => $docValue) {
593
            if ($docValue['id']) {
594
                $docData = $this->getDocumentData($docValue['id'], $docValue);
595
                $pdfUrl .= $docData['urlParams'] . $this->settings['pdfparamseparator'];
596
                $numberOfPages += $docData['pageNums'];
597
            }
598
        }
599
        // get printer data
600
        $printerId = $this->requestData['print_action'];
601
602
        // get id from db and send selected doc download link
603
        $printer = $this->printerRepository->findOneByUid($printerId);
0 ignored issues
show
Bug introduced by
The method findOneByUid() does not exist on Kitodo\Dlf\Domain\Repository\PrinterRepository. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

603
        /** @scrutinizer ignore-call */ 
604
        $printer = $this->printerRepository->findOneByUid($printerId);
Loading history...
604
605
        // printer is selected
606
        if ($printer) {
607
            $pdfUrl = $printer->getPrint();
0 ignored issues
show
Bug introduced by
The method getPrint() does not exist on TYPO3\CMS\Extbase\Persistence\QueryResultInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

607
            /** @scrutinizer ignore-call */ 
608
            $pdfUrl = $printer->getPrint();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
608
            $numberOfPages = 0;
609
            foreach ($this->requestData['selected'] as $docId => $docValue) {
610
                if ($docValue['id']) {
611
                    $explodeId = explode("_", $docId);
612
                    $docData = $this->getDocumentData($explodeId[0], $docValue);
613
                    $pdfUrl .= $docData['urlParams'] . $this->settings['pdfparamseparator'];
614
                    $numberOfPages += $docData['pageNums'];
615
                }
616
            }
617
            $pdfUrl = trim($pdfUrl, $this->settings['pdfparamseparator']);
618
        }
619
620
        $actionLog = GeneralUtility::makeInstance(ActionLog::class);
621
        // protocol
622
        $actionLog->setPid($this->settings['storagePid']);
623
        $actionLog->setFileName($pdfUrl);
624
        $actionLog->setCountPages($numberOfPages);
625
626
        if ($GLOBALS["TSFE"]->loginUser) {
627
            // internal user
628
            $actionLog->setUserId($GLOBALS["TSFE"]->fe_user->user['uid']);
629
            $actionLog->setName($GLOBALS["TSFE"]->fe_user->user['username']);
630
            $actionLog->setLabel('Print: ' . $printer->getLabel());
0 ignored issues
show
Bug introduced by
The method getLabel() does not exist on TYPO3\CMS\Extbase\Persistence\QueryResultInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

630
            $actionLog->setLabel('Print: ' . $printer->/** @scrutinizer ignore-call */ getLabel());

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
631
        } else {
632
            // external user
633
            $actionLog->setUserId(0);
634
            $actionLog->setName('n/a');
635
            $actionLog->setLabel('Print: ' . $printer->getLabel());
636
        }
637
        // add action to protocol
638
        $this->actionLogRepository->add($actionLog);
639
640
        $this->redirectToUri($pdfUrl);
641
    }
642
}
643