Completed
Push — master ( e65873...e40d38 )
by Tim
53:29 queued 12:29
created

BackendController::getPids()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
1
<?php
2
3
/**
4
 * BackendController.
5
 */
6
declare(strict_types=1);
7
8
namespace HDNET\Calendarize\Controller;
9
10
use HDNET\Calendarize\Domain\Model\Request\OptionRequest;
11
use TYPO3\CMS\Core\Messaging\FlashMessage;
12
13
/**
14
 * BackendController.
15
 */
16
class BackendController extends AbstractController
17
{
18
    /**
19
     * Basic backend list.
20
     */
21
    public function listAction()
22
    {
23
        $this->settings['timeFormat'] = 'H:i';
24
        $this->settings['dateFormat'] = 'd.m.Y';
25
26
        $options = $this->getOptions();
27
        $typeLocations = $this->getDifferentTypesAndLocations();
28
29
        $this->view->assignMultiple([
30
            'indices' => $this->indexRepository->findAllForBackend($options),
0 ignored issues
show
Bug introduced by
It seems like $options defined by $this->getOptions() on line 26 can be null; however, HDNET\Calendarize\Domain...ry::findAllForBackend() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
31
            'typeLocations' => $typeLocations,
32
            'pids' => $this->getPids($typeLocations),
33
            'settings' => $this->settings,
34
            'options' => $options
35
        ]);
36
    }
37
38
    protected function getPids(array $typeLocations){
39
        $pids = [];
40
        foreach ($typeLocations as $locations) {
41
            $pids = array_merge($pids, array_keys($locations));
42
        }
43
        $pids = array_unique($pids);
44
        return array_combine($pids, $pids);
45
    }
46
47
    /**
48
     * Option action
49
     *
50
     * @param \HDNET\Calendarize\Domain\Model\Request\OptionRequest $options
51
     */
52
    public function optionAction(OptionRequest $options)
53
    {
54
        $GLOBALS['BE_USER']->setAndSaveSessionData('calendarize_be', serialize($options));
55
        $this->addFlashMessage('Options saved', '', FlashMessage::OK, true);
56
        $this->forward('list');
57
    }
58
59
    /**
60
     * Get option request
61
     *
62
     * @return OptionRequest
63
     */
64
    protected function getOptions()
65
    {
66
        try {
67
            $info = $GLOBALS['BE_USER']->getSessionData('calendarize_be');
68
            $object = @unserialize((string)$info);
69
            if($object instanceof OptionRequest) {
70
                return $object;
71
            }
72
        } catch (\Exception $exception) {
73
            return new OptionRequest();
74
        }
75
    }
76
77
    /**
78
     * Get the differnet locations for new entries.
79
     *
80
     * @return array
81
     */
82
    protected function getDifferentTypesAndLocations()
83
    {
84
        $typeLocations = [];
85
        foreach ($this->indexRepository->findDifferentTypesAndLocations() as $entry) {
86
            $typeLocations[$entry['foreign_table']][$entry['pid']] = $entry['unique_register_key'];
87
        }
88
89
        return $typeLocations;
90
    }
91
}
92