Passed
Push — master ( 6227bd...8d3e4d )
by Aske
04:29
created

HistoryController::indexAction()   F

Complexity

Conditions 17
Paths 432

Size

Total Lines 106
Code Lines 67

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 17
eloc 67
nc 432
nop 5
dl 0
loc 106
rs 1.8388
c 0
b 0
f 0

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

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