Passed
Push — 6.x ( f9d41c...a9ae9c )
by Torben
57:47 queued 12:48
created

AbstractController::injectCalendarService()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
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\Pagination\QueryResultPaginator;
30
use TYPO3\CMS\Extbase\Persistence\QueryResultInterface;
31
use TYPO3\CMS\Extbase\Reflection\ObjectAccess;
32
use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController;
33
34
/**
35
 * EventController
36
 */
37
abstract class AbstractController extends ActionController
38
{
39
    protected array $ignoredSettingsForOverwriteDemand = ['storagepage', 'orderfieldallowed'];
40
41
    protected EventRepository $eventRepository;
42
    protected RegistrationRepository $registrationRepository;
43
    protected CategoryRepository $categoryRepository;
44
    protected LocationRepository $locationRepository;
45
    protected OrganisatorRepository $organisatorRepository;
46
    protected SpeakerRepository $speakerRepository;
47
    protected NotificationService $notificationService;
48
    protected ICalendarService $icalendarService;
49
    protected RegistrationService $registrationService;
50
    protected CalendarService $calendarService;
51
    protected PaymentService $paymentService;
52
    protected FieldRepository $fieldRepository;
53
54
    public function injectCalendarService(CalendarService $calendarService): void
55
    {
56
        $this->calendarService = $calendarService;
57
    }
58
59
    public function injectCategoryRepository(CategoryRepository $categoryRepository): void
60
    {
61
        $this->categoryRepository = $categoryRepository;
62
    }
63
64
    public function injectEventRepository(EventRepository $eventRepository): void
65
    {
66
        $this->eventRepository = $eventRepository;
67
    }
68
69
    public function injectIcalendarService(ICalendarService $icalendarService): void
70
    {
71
        $this->icalendarService = $icalendarService;
72
    }
73
74
    public function injectLocationRepository(LocationRepository $locationRepository): void
75
    {
76
        $this->locationRepository = $locationRepository;
77
    }
78
79
    public function injectNotificationService(NotificationService $notificationService): void
80
    {
81
        $this->notificationService = $notificationService;
82
    }
83
84
    public function injectOrganisatorRepository(OrganisatorRepository $organisatorRepository): void
85
    {
86
        $this->organisatorRepository = $organisatorRepository;
87
    }
88
89
    public function injectSpeakerRepository(SpeakerRepository $speakerRepository): void
90
    {
91
        $this->speakerRepository = $speakerRepository;
92
    }
93
94
    public function injectPaymentService(PaymentService $paymentService): void
95
    {
96
        $this->paymentService = $paymentService;
97
    }
98
99
    public function injectRegistrationRepository(RegistrationRepository $registrationRepository): void
100
    {
101
        $this->registrationRepository = $registrationRepository;
102
    }
103
104
    public function injectRegistrationService(RegistrationService $registrationService): void
105
    {
106
        $this->registrationService = $registrationService;
107
    }
108
109
    public function injectFieldRepository(FieldRepository $fieldRepository): void
110
    {
111
        $this->fieldRepository = $fieldRepository;
112
    }
113
114
    /**
115
     * Public getter to retrieve extension settings. Can be used by extending extensions in e.g. event listeners to
116
     * retrieve the extension settings.
117
     */
118
    public function getSettings(): array
119
    {
120
        return $this->settings;
121
    }
122
123
    /**
124
     * Returns an array with variables for the pagination. An array with pagination settings should be passed.
125
     * Applies default values if settings are not available:
126
     * - pagination disabled
127
     * - itemsPerPage = 10
128
     * - maxNumPages = 10
129
     */
130
    protected function getPagination(QueryResultInterface $events, array $settings): array
131
    {
132
        $pagination = [];
133
        $currentPage = $this->request->hasArgument('currentPage') ? (int)$this->request->getArgument('currentPage') : 1;
134
        if (($settings['enablePagination'] ?? false) && (int)$settings['itemsPerPage'] > 0) {
135
            $paginator = new QueryResultPaginator($events, $currentPage, (int)($settings['itemsPerPage'] ?? 10));
136
            $pagination = new NumberedPagination($paginator, (int)($settings['maxNumPages'] ?? 10));
137
            $pagination = [
138
                'paginator' => $paginator,
139
                'pagination' => $pagination,
140
            ];
141
        }
142
143
        return $pagination;
144
    }
145
146
    /**
147
     * Overwrites a given demand object by an propertyName =>  $propertyValue array
148
     */
149
    protected function overwriteEventDemandObject(EventDemand $demand, array $overwriteDemand): EventDemand
150
    {
151
        foreach ($this->ignoredSettingsForOverwriteDemand as $property) {
152
            unset($overwriteDemand[$property]);
153
        }
154
155
        foreach ($overwriteDemand as $propertyName => $propertyValue) {
156
            if (in_array(strtolower($propertyName), $this->ignoredSettingsForOverwriteDemand, true)) {
157
                continue;
158
            }
159
            ObjectAccess::setProperty($demand, $propertyName, $propertyValue);
160
        }
161
162
        return $demand;
163
    }
164
165
    protected function getTypoScriptFrontendController(): ?TypoScriptFrontendController
166
    {
167
        return $GLOBALS['TSFE'] ?? null;
168
    }
169
}
170