Passed
Push — develop ( 8d2151...a44e37 )
by Torben
42:00
created

AbstractController   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 131
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 44
c 2
b 0
f 0
dl 0
loc 131
rs 10
wmc 22

16 Methods

Rating   Name   Duplication   Size   Complexity  
A injectFieldRepository() 0 3 1
A getPagination() 0 14 4
A injectEventRepository() 0 3 1
A injectOrganisatorRepository() 0 3 1
A getTypoScriptFrontendController() 0 3 1
A overwriteEventDemandObject() 0 14 4
A injectSpeakerRepository() 0 3 1
A injectLocationRepository() 0 3 1
A injectCalendarService() 0 3 1
A getControllerArguments() 0 3 1
A injectPaymentService() 0 3 1
A injectNotificationService() 0 3 1
A injectIcalendarService() 0 3 1
A injectRegistrationService() 0 3 1
A injectRegistrationRepository() 0 3 1
A injectCategoryRepository() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Extension "sf_event_mgt" for TYPO3 CMS.
7
 *
8
 * For the full copyright and license information, please read the
9
 * LICENSE.txt file that was distributed with this source code.
10
 */
11
12
namespace DERHANSEN\SfEventMgt\Controller;
13
14
use DERHANSEN\SfEventMgt\Domain\Model\Dto\EventDemand;
15
use DERHANSEN\SfEventMgt\Domain\Repository\CategoryRepository;
16
use DERHANSEN\SfEventMgt\Domain\Repository\EventRepository;
17
use DERHANSEN\SfEventMgt\Domain\Repository\LocationRepository;
18
use DERHANSEN\SfEventMgt\Domain\Repository\OrganisatorRepository;
19
use DERHANSEN\SfEventMgt\Domain\Repository\Registration\FieldRepository;
20
use DERHANSEN\SfEventMgt\Domain\Repository\RegistrationRepository;
21
use DERHANSEN\SfEventMgt\Domain\Repository\SpeakerRepository;
22
use DERHANSEN\SfEventMgt\Pagination\NumberedPagination;
23
use DERHANSEN\SfEventMgt\Service\CalendarService;
24
use DERHANSEN\SfEventMgt\Service\ICalendarService;
25
use DERHANSEN\SfEventMgt\Service\NotificationService;
26
use DERHANSEN\SfEventMgt\Service\PaymentService;
27
use DERHANSEN\SfEventMgt\Service\RegistrationService;
28
use TYPO3\CMS\Extbase\Mvc\Controller\ActionController;
29
use TYPO3\CMS\Extbase\Mvc\Controller\Arguments;
30
use TYPO3\CMS\Extbase\Pagination\QueryResultPaginator;
31
use TYPO3\CMS\Extbase\Persistence\QueryResultInterface;
32
use TYPO3\CMS\Extbase\Reflection\ObjectAccess;
33
use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController;
34
35
abstract class AbstractController extends ActionController
36
{
37
    protected array $ignoredSettingsForOverwriteDemand = ['storagepage', 'orderfieldallowed'];
38
39
    protected EventRepository $eventRepository;
40
    protected RegistrationRepository $registrationRepository;
41
    protected CategoryRepository $categoryRepository;
42
    protected LocationRepository $locationRepository;
43
    protected OrganisatorRepository $organisatorRepository;
44
    protected SpeakerRepository $speakerRepository;
45
    protected NotificationService $notificationService;
46
    protected ICalendarService $icalendarService;
47
    protected RegistrationService $registrationService;
48
    protected CalendarService $calendarService;
49
    protected PaymentService $paymentService;
50
    protected FieldRepository $fieldRepository;
51
52
    public function injectCalendarService(CalendarService $calendarService): void
53
    {
54
        $this->calendarService = $calendarService;
55
    }
56
57
    public function injectCategoryRepository(CategoryRepository $categoryRepository): void
58
    {
59
        $this->categoryRepository = $categoryRepository;
60
    }
61
62
    public function injectEventRepository(EventRepository $eventRepository): void
63
    {
64
        $this->eventRepository = $eventRepository;
65
    }
66
67
    public function injectIcalendarService(ICalendarService $icalendarService): void
68
    {
69
        $this->icalendarService = $icalendarService;
70
    }
71
72
    public function injectLocationRepository(LocationRepository $locationRepository): void
73
    {
74
        $this->locationRepository = $locationRepository;
75
    }
76
77
    public function injectNotificationService(NotificationService $notificationService): void
78
    {
79
        $this->notificationService = $notificationService;
80
    }
81
82
    public function injectOrganisatorRepository(OrganisatorRepository $organisatorRepository): void
83
    {
84
        $this->organisatorRepository = $organisatorRepository;
85
    }
86
87
    public function injectSpeakerRepository(SpeakerRepository $speakerRepository): void
88
    {
89
        $this->speakerRepository = $speakerRepository;
90
    }
91
92
    public function injectPaymentService(PaymentService $paymentService): void
93
    {
94
        $this->paymentService = $paymentService;
95
    }
96
97
    public function injectRegistrationRepository(RegistrationRepository $registrationRepository): void
98
    {
99
        $this->registrationRepository = $registrationRepository;
100
    }
101
102
    public function injectRegistrationService(RegistrationService $registrationService): void
103
    {
104
        $this->registrationService = $registrationService;
105
    }
106
107
    public function injectFieldRepository(FieldRepository $fieldRepository): void
108
    {
109
        $this->fieldRepository = $fieldRepository;
110
    }
111
112
    /**
113
     * Public getter for extbase arguments. Can be used by extending extensions in e.g. event listeners to
114
     * retrieve the current controller arguments.
115
     */
116
    public function getControllerArguments(): Arguments
117
    {
118
        return $this->arguments;
119
    }
120
121
    /**
122
     * Returns an array with variables for the pagination. An array with pagination settings should be passed.
123
     * Applies default values if settings are not available:
124
     * - pagination disabled
125
     * - itemsPerPage = 10
126
     * - maxNumPages = 10
127
     */
128
    protected function getPagination(QueryResultInterface $events, array $settings): array
129
    {
130
        $pagination = [];
131
        $currentPage = $this->request->hasArgument('currentPage') ? (int)$this->request->getArgument('currentPage') : 1;
132
        if (($settings['enablePagination'] ?? false) && (int)$settings['itemsPerPage'] > 0) {
133
            $paginator = new QueryResultPaginator($events, $currentPage, (int)($settings['itemsPerPage'] ?? 10));
134
            $pagination = new NumberedPagination($paginator, (int)($settings['maxNumPages'] ?? 10));
135
            $pagination = [
136
                'paginator' => $paginator,
137
                'pagination' => $pagination,
138
            ];
139
        }
140
141
        return $pagination;
142
    }
143
144
    /**
145
     * Overwrites a given demand object by an propertyName =>  $propertyValue array
146
     */
147
    protected function overwriteEventDemandObject(EventDemand $demand, array $overwriteDemand): EventDemand
148
    {
149
        foreach ($this->ignoredSettingsForOverwriteDemand as $property) {
150
            unset($overwriteDemand[$property]);
151
        }
152
153
        foreach ($overwriteDemand as $propertyName => $propertyValue) {
154
            if (in_array(strtolower($propertyName), $this->ignoredSettingsForOverwriteDemand, true)) {
155
                continue;
156
            }
157
            ObjectAccess::setProperty($demand, $propertyName, $propertyValue);
158
        }
159
160
        return $demand;
161
    }
162
163
    protected function getTypoScriptFrontendController(): ?TypoScriptFrontendController
164
    {
165
        return $GLOBALS['TSFE'] ?? null;
166
    }
167
}
168