HistoryController::indexAction()   F
last analyzed

Complexity

Conditions 17
Paths 432

Size

Total Lines 104
Code Lines 65

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 1 Features 0
Metric Value
cc 17
eloc 65
c 4
b 1
f 0
nc 432
nop 5
dl 0
loc 104
rs 1.8388

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
namespace AE\History\Controller;
3
4
use AE\History\Domain\Repository\NodeEventRepository;
5
use Neos\Flow\Annotations as Flow;
6
use Neos\Flow\Mvc\View\ViewInterface;
7
use Neos\Flow\Security\Context;
8
use Neos\Fusion\View\FusionView;
9
use Neos\Neos\Controller\CreateContentContextTrait;
10
use Neos\Neos\Controller\Module\AbstractModuleController;
11
use Neos\Neos\Domain\Repository\DomainRepository;
12
use Neos\Neos\Domain\Repository\SiteRepository;
13
use Neos\Neos\Domain\Service\UserService;
14
use Neos\Neos\EventLog\Domain\Model\EventsOnDate;
15
use Neos\Neos\EventLog\Domain\Model\NodeEvent;
16
17
/**
18
 * Controller for the history module of Neos, displaying the timeline of changes.
19
 */
20
class HistoryController extends AbstractModuleController
21
{
22
    use CreateContentContextTrait;
23
24
    /**
25
     * @var string
26
     */
27
    protected $defaultViewObjectName = FusionView::class;
28
29
    /**
30
     * @Flow\Inject
31
     * @var DomainRepository
32
     */
33
    protected $domainRepository;
34
35
    /**
36
     * @Flow\Inject
37
     * @var NodeEventRepository
38
     */
39
    protected $nodeEventRepository;
40
41
    /**
42
     * @Flow\Inject
43
     * @var Context
44
     */
45
    protected $securityContext;
46
47
    /**
48
     * @Flow\Inject
49
     * @var SiteRepository
50
     */
51
    protected $siteRepository;
52
53
    /**
54
     * @Flow\Inject
55
     * @var UserService
56
     */
57
    protected $userService;
58
59
    /**
60
     * Show event overview.
61
     *
62
     * @param int $offset
63
     * @param int $limit
64
     * @param string|null $siteIdentifier
65
     * @param string|null $nodeIdentifier
66
     * @param string|null $accountIdentifier
67
     *
68
     * @return void
69
     */
70
    public function indexAction(
71
        int $offset = 0,
72
        int $limit = 25,
73
        string $siteIdentifier = null,
74
        string $nodeIdentifier = null,
75
        string $accountIdentifier = null
76
    ) {
77
        if ($nodeIdentifier === '') {
78
            $nodeIdentifier = null;
79
        }
80
81
        $numberOfSites = 0;
82
        // In case a user can only access a single site, but more sites exists
83
        $this->securityContext->withoutAuthorizationChecks(function () use (&$numberOfSites) {
84
            $numberOfSites = $this->siteRepository->countAll();
85
        });
86
        $sites = $this->siteRepository->findOnline();
87
        if ($numberOfSites > 1 && $siteIdentifier === null) {
88
            $domain = $this->domainRepository->findOneByActiveRequest();
89
            if ($domain !== null) {
90
                $siteIdentifier = $this->persistenceManager->getIdentifierByObject($domain->getSite());
91
            }
92
        }
93
94
        /** @var string[] $accounts */
95
        $accounts = [];
96
        $accountIdentifiers = $this->nodeEventRepository->findAccountIdentifiers('live', $siteIdentifier ?: null, $nodeIdentifier ?: null);
97
        foreach ($accountIdentifiers as $identifier) {
98
            $user = $this->userService->getUser($identifier);
99
            $accounts[$identifier] = $user ? $user->getName()->getLastName() . ', ' . $user->getName()->getFirstName() : $identifier;
100
        }
101
102
        /** @var NodeEvent[] $events */
103
        $events = $this->nodeEventRepository
104
            ->findRelevantEventsByWorkspace(
105
                $offset,
106
                $limit + 1,
107
                'live',
108
                $siteIdentifier ?: null,
109
                $nodeIdentifier,
110
                $accountIdentifier ?: null
111
            )
112
            ->toArray()
113
        ;
114
115
        $nextPage = null;
116
        if (count($events) > $limit) {
117
            $events = array_slice($events, 0, $limit);
118
119
            $nextPage = $this->controllerContext
120
                ->getUriBuilder()
121
                ->setCreateAbsoluteUri(true)
122
                ->uriFor(
123
                    'Index',
124
                    [
125
                        'accountIdentifier' => $accountIdentifier,
126
                        'nodeIdentifier' => $nodeIdentifier,
127
                        'offset' => $offset + $limit,
128
                        'siteIdentifier' => $siteIdentifier,
129
                    ]
130
                )
131
            ;
132
        }
133
134
        /** @var EventsOnDate[] $eventsByDate */
135
        $eventsByDate = [];
136
        foreach ($events as $event) {
137
            if ($event->getChildEvents()->count() === 0) {
138
                continue;
139
            }
140
            $timestamp = $event->getTimestamp();
141
            $day = $timestamp->format('Y-m-d');
142
            if (!isset($eventsByDate[$day])) {
143
                $eventsByDate[$day] = new EventsOnDate($timestamp);
144
            }
145
146
            $eventsOnThisDay = $eventsByDate[$day];
147
            $eventsOnThisDay->add($event);
148
        }
149
150
        $firstEvent = current($events);
151
        if ($firstEvent === false) {
152
            $node = $this->createContentContext('live')->getNodeByIdentifier($nodeIdentifier);
153
            if ($node !== null) {
154
                $firstEvent = [
155
                    'data' => [
156
                        'documentNodeLabel' => $node->getLabel(),
157
                        'documentNodeType' => $node->getNodeType()->getName(),
158
                    ],
159
                    'node' => $node,
160
                    'nodeIdentifier' => $nodeIdentifier,
161
                ];
162
            }
163
        }
164
165
        $this->view->assignMultiple([
166
            'accountIdentifier' => $accountIdentifier,
167
            'eventsByDate' => $eventsByDate,
168
            'firstEvent' => $firstEvent,
169
            'nextPage' => $nextPage,
170
            'nodeIdentifier' => $nodeIdentifier,
171
            'siteIdentifier' => $siteIdentifier,
172
            'sites' => $sites,
173
            'accounts' => $accounts,
174
        ]);
175
    }
176
177
    /**
178
     * Simply sets the Fusion path pattern on the view.
179
     *
180
     * @param ViewInterface $view
181
     *
182
     * @return void
183
     */
184
    protected function initializeView(ViewInterface $view)
185
    {
186
        parent::initializeView($view);
187
        $view->setFusionPathPattern('resource://AE.History/Private/Fusion');
0 ignored issues
show
Bug introduced by
The method setFusionPathPattern() does not exist on Neos\Flow\Mvc\View\ViewInterface. It seems like you code against a sub-type of Neos\Flow\Mvc\View\ViewInterface such as Neos\Fusion\View\FusionView or Neos\Flow\Tests\Function...n\Fixtures\TemplateView. ( Ignorable by Annotation )

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

187
        $view->/** @scrutinizer ignore-call */ 
188
               setFusionPathPattern('resource://AE.History/Private/Fusion');
Loading history...
188
    }
189
}
190