Passed
Push — typo3_11 ( 39c917...394e41 )
by Torben
04:40
created

AbstractController::overwriteEventDemandObject()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 7
c 1
b 0
f 0
nc 6
nop 2
dl 0
loc 14
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)
55
    {
56
        $this->calendarService = $calendarService;
57
    }
58
59
    public function injectCategoryRepository(CategoryRepository $categoryRepository)
60
    {
61
        $this->categoryRepository = $categoryRepository;
62
    }
63
64
    public function injectEventRepository(EventRepository $eventRepository)
65
    {
66
        $this->eventRepository = $eventRepository;
67
    }
68
69
    public function injectIcalendarService(ICalendarService $icalendarService)
70
    {
71
        $this->icalendarService = $icalendarService;
72
    }
73
74
    public function injectLocationRepository(LocationRepository $locationRepository)
75
    {
76
        $this->locationRepository = $locationRepository;
77
    }
78
79
    public function injectNotificationService(NotificationService $notificationService)
80
    {
81
        $this->notificationService = $notificationService;
82
    }
83
84
    public function injectOrganisatorRepository(OrganisatorRepository $organisatorRepository)
85
    {
86
        $this->organisatorRepository = $organisatorRepository;
87
    }
88
89
    public function injectSpeakerRepository(SpeakerRepository $speakerRepository)
90
    {
91
        $this->speakerRepository = $speakerRepository;
92
    }
93
94
    public function injectPaymentService(PaymentService $paymentService)
95
    {
96
        $this->paymentService = $paymentService;
97
    }
98
99
    public function injectRegistrationRepository(RegistrationRepository $registrationRepository)
100
    {
101
        $this->registrationRepository = $registrationRepository;
102
    }
103
104
    public function injectRegistrationService(RegistrationService $registrationService)
105
    {
106
        $this->registrationService = $registrationService;
107
    }
108
109
    public function injectFieldRepository(FieldRepository $fieldRepository)
110
    {
111
        $this->fieldRepository = $fieldRepository;
112
    }
113
114
    /**
115
     * Returns an array with variables for the pagination. An array with pagination settings should be passed.
116
     * Applies default values if settings are not available:
117
     * - pagination disabled
118
     * - itemsPerPage = 10
119
     * - maxNumPages = 10
120
     *
121
     * @param QueryResultInterface $events
122
     * @param array $settings
123
     * @return array
124
     */
125
    protected function getPagination(QueryResultInterface $events, array $settings): array
126
    {
127
        $pagination = [];
128
        $currentPage = $this->request->hasArgument('currentPage') ? (int)$this->request->getArgument('currentPage') : 1;
129
        if (($settings['enablePagination'] ?? false) && (int)$settings['itemsPerPage'] > 0) {
130
            $paginator = new QueryResultPaginator($events, $currentPage, (int)($settings['itemsPerPage'] ?? 10));
131
            $pagination = new NumberedPagination($paginator, (int)($settings['maxNumPages'] ?? 10));
132
            $pagination = [
133
                'paginator' => $paginator,
134
                'pagination' => $pagination,
135
            ];
136
        }
137
138
        return $pagination;
139
    }
140
141
    /**
142
     * Overwrites a given demand object by an propertyName =>  $propertyValue array
143
     *
144
     * @param EventDemand $demand
145
     * @param array $overwriteDemand
146
     *
147
     * @return EventDemand
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