Passed
Pull Request — master (#18)
by Aske
02:50
created

HistoryController::indexAction()   F

Complexity

Conditions 13
Paths 576

Size

Total Lines 102
Code Lines 64

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 13
eloc 64
nc 576
nop 5
dl 0
loc 102
rs 3.2165
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\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
     * @Flow\Inject
27
     * @var AccountRepository
28
     */
29
    protected $accountRepository;
30
31
    /**
32
     * @var string
33
     */
34
    protected $defaultViewObjectName = FusionView::class;
35
36
    /**
37
     * @Flow\Inject
38
     * @var DomainRepository
39
     */
40
    protected $domainRepository;
41
42
    /**
43
     * @Flow\Inject
44
     * @var NodeEventRepository
45
     */
46
    protected $nodeEventRepository;
47
48
    /**
49
     * @Flow\Inject
50
     * @var Context
51
     */
52
    protected $securityContext;
53
54
    /**
55
     * @Flow\Inject
56
     * @var SiteRepository
57
     */
58
    protected $siteRepository;
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 ($accountIdentifier === '') {
79
            $accountIdentifier = null;
80
        }
81
        if ($nodeIdentifier === '') {
82
            $nodeIdentifier = null;
83
        }
84
        if ($siteIdentifier === '') {
85
            $siteIdentifier = null;
86
        }
87
88
        $numberOfSites = 0;
89
        // In case a user can only access a single site, but more sites exists
90
        $this->securityContext->withoutAuthorizationChecks(function () use (&$numberOfSites) {
91
            $numberOfSites = $this->siteRepository->countAll();
92
        });
93
        $sites = $this->siteRepository->findOnline();
94
        if ($numberOfSites > 1 && $siteIdentifier === null) {
95
            $domain = $this->domainRepository->findOneByActiveRequest();
96
            if ($domain !== null) {
97
                $siteIdentifier = $this->persistenceManager->getIdentifierByObject($domain->getSite());
98
            }
99
        }
100
101
        /** @var Account[] $accounts */
102
        $accounts = $this->accountRepository->findByAuthenticationProviderName('Neos.Neos:Backend')->toArray();
103
104
        /** @var NodeEvent[] $events */
105
        $events = $this->nodeEventRepository
106
            ->findRelevantEventsByWorkspace(
107
                $offset,
108
                $limit + 1,
109
                'live',
110
                $siteIdentifier,
111
                $nodeIdentifier,
112
                $accountIdentifier
113
            )
114
            ->toArray()
115
        ;
116
117
        $nextPage = null;
118
        if (count($events) > $limit) {
119
            $events = array_slice($events, 0, $limit);
120
121
            $nextPage = $this->controllerContext
122
                ->getUriBuilder()
123
                ->setCreateAbsoluteUri(true)
124
                ->uriFor(
125
                    'Index',
126
                    ['offset' => $offset + $limit, 'siteIdentifier' => $siteIdentifier],
127
                    'History',
128
                    'Neos.Neos'
129
                )
130
            ;
131
        }
132
133
        /** @var EventsOnDate[] $eventsByDate */
134
        $eventsByDate = [];
135
        foreach ($events as $event) {
136
            if ($event->getChildEvents()->count() === 0) {
137
                continue;
138
            }
139
            $timestamp = $event->getTimestamp();
140
            $day = $timestamp->format('Y-m-d');
141
            if (!isset($eventsByDate[$day])) {
142
                $eventsByDate[$day] = new EventsOnDate($timestamp);
143
            }
144
145
            $eventsOnThisDay = $eventsByDate[$day];
146
            $eventsOnThisDay->add($event);
147
        }
148
149
        $firstEvent = current($events);
150
        if ($firstEvent === false) {
151
            $node = $this->createContentContext('live')->getNodeByIdentifier($nodeIdentifier);
152
            if ($node !== null) {
153
                $firstEvent = [
154
                    'data' => [
155
                        'documentNodeLabel' => $node->getLabel(),
156
                        'documentNodeType' => $node->getNodeType()->getName(),
157
                    ],
158
                    'node' => $node,
159
                    'nodeIdentifier' => $nodeIdentifier,
160
                ];
161
            }
162
        }
163
164
        $this->view->assignMultiple([
165
            'accountIdentifier' => $accountIdentifier,
166
            'accounts' => $accounts,
167
            'eventsByDate' => $eventsByDate,
168
            'firstEvent' => $firstEvent,
169
            'nextPage' => $nextPage,
170
            'nodeIdentifier' => $nodeIdentifier,
171
            'siteIdentifier' => $siteIdentifier,
172
            'sites' => $sites,
173
        ]);
174
    }
175
176
    /**
177
     * Simply sets the Fusion path pattern on the view.
178
     *
179
     * @param ViewInterface $view
180
     *
181
     * @return void
182
     */
183
    protected function initializeView(ViewInterface $view)
184
    {
185
        parent::initializeView($view);
186
        $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

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