Passed
Push — master ( 48499d...6227bd )
by Aske
02:45
created

HistoryController::indexAction()   D

Complexity

Conditions 14
Paths 288

Size

Total Lines 106
Code Lines 66

Duplication

Lines 0
Ratio 0 %

Importance

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

197
        $view->/** @scrutinizer ignore-call */ 
198
               setFusionPathPattern('resource://AE.History/Private/Fusion');
Loading history...
198
    }
199
}
200