processDatamap_postProcessFieldArray()   C
last analyzed

Complexity

Conditions 14
Paths 9

Size

Total Lines 97
Code Lines 74

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
cc 14
eloc 74
c 4
b 0
f 0
nc 9
nop 5
dl 0
loc 97
rs 5.5805

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
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\Hooks;
13
14
use DERHANSEN\SfEventMgt\Service\EventCacheService;
15
use TYPO3\CMS\Core\Configuration\FlexForm\FlexFormTools;
16
use TYPO3\CMS\Core\Database\Connection;
17
use TYPO3\CMS\Core\Database\ConnectionPool;
18
use TYPO3\CMS\Core\DataHandling\DataHandler;
19
use TYPO3\CMS\Core\Utility\GeneralUtility;
20
21
/**
22
 * Hooks for DataHandler
23
 */
24
class DataHandlerHooks
25
{
26
    public const EVENT_TABLE = 'tx_sfeventmgt_domain_model_event';
27
    public const CUSTOMNOTIFICATIONLOG_TABLE = 'tx_sfeventmgt_domain_model_customnotificationlog';
28
29
    /**
30
     * Flushes the cache if a event record was edited.
31
     * This happens on two levels: by UID and by PID.
32
     */
33
    public function clearCachePostProc(array $params): void
34
    {
35
        if (isset($params['table']) && $params['table'] === self::EVENT_TABLE) {
36
            $eventUid = $params['uid'] ?? 0;
37
            $pageUid = $params['uid_page'] ?? 0;
38
            if ($eventUid > 0 || $pageUid > 0) {
39
                $eventCacheService = GeneralUtility::makeInstance(EventCacheService::class);
40
                $eventCacheService->flushEventCache($eventUid, $pageUid);
41
            }
42
        }
43
    }
44
45
    /**
46
     * Checks if the fields defined in $checkFields are set in the data-array of pi_flexform.
47
     * If a field is present and contains an empty value, the field is unset. This way empty plugin settings
48
     * don't overwrite TypoScript settings.
49
     *
50
     * Structure of the checkFields array:
51
     *
52
     * array('sheet' => array('field1', 'field2'));
53
     *
54
     * @param string $status
55
     * @param string $table
56
     * @param string $id
57
     * @param array $fieldArray
58
     * @param DataHandler $dataHandler
59
     */
60
    public function processDatamap_postProcessFieldArray($status, $table, $id, &$fieldArray, &$dataHandler): void
0 ignored issues
show
Unused Code introduced by
The parameter $id is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

60
    public function processDatamap_postProcessFieldArray($status, $table, /** @scrutinizer ignore-unused */ $id, &$fieldArray, &$dataHandler): void

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
61
    {
62
        if ($table === 'tt_content' &&
63
            $status === 'update' &&
64
            isset($fieldArray['pi_flexform']) &&
65
            $dataHandler->checkValue_currentRecord['CType'] === 'list' &&
66
            in_array(
67
                $dataHandler->checkValue_currentRecord['list_type'],
68
                [
69
                    'sfeventmgt_pieventlist',
70
                    'sfeventmgt_pieventdetail',
71
                    'sfeventmgt_pieventregistration',
72
                    'sfeventmgt_pieventsearch',
73
                    'sfeventmgt_pieventcalendar',
74
                    'sfeventmgt_piuserreg',
75
                    'sfeventmgt_pipayment',
76
                ]
77
            )
78
        ) {
79
            $checkFields = [
80
                'notification' => [
81
                    'settings.notification.senderEmail',
82
                    'settings.notification.senderName',
83
                    'settings.notification.adminEmail',
84
                    'settings.notification.replyToEmail',
85
                    'settings.notification.registrationNew.userSubject',
86
                    'settings.notification.registrationWaitlistNew.userSubject',
87
                    'settings.notification.registrationNew.adminSubject',
88
                    'settings.notification.registrationWaitlistNew.adminSubject',
89
                    'settings.notification.registrationConfirmed.userSubject',
90
                    'settings.notification.registrationWaitlistConfirmed.userSubject',
91
                    'settings.notification.registrationConfirmed.adminSubject',
92
                    'settings.notification.registrationWaitlistConfirmed.adminSubject',
93
                    'settings.notification.registrationCancelled.userSubject',
94
                    'settings.notification.registrationCancelled.adminSubject',
95
                    'settings.notification.registrationWaitlistMoveUp.userSubject',
96
                    'settings.notification.registrationWaitlistMoveUp.adminSubject',
97
                ],
98
                'sDEF' => [
99
                    'settings.displayMode',
100
                    'settings.orderField',
101
                    'settings.orderDirection',
102
                    'settings.topEventRestriction',
103
                    'settings.timeRestrictionLow',
104
                    'settings.timeRestrictionHigh',
105
                    'settings.includeCurrent',
106
                    'settings.queryLimit',
107
                    'settings.category',
108
                    'settings.storagePage',
109
                    'settings.registration.requiredFields',
110
                    'settings.userRegistration.displayMode',
111
                    'settings.userRegistration.orderField',
112
                    'settings.userRegistration.orderDirection',
113
                    'settings.userRegistration.storagePage',
114
                    'settings.userRegistration.recursive',
115
                    'settings.singleEvent',
116
                ],
117
                'additional' => [
118
                    'settings.detailPid',
119
                    'settings.listPid',
120
                    'settings.registrationPid',
121
                    'settings.paymentPid',
122
                    'settings.restrictForeignRecordsToStoragePage',
123
                ],
124
                'pagination' => [
125
                    'settings.pagination.enablePagination',
126
                    'settings.pagination.itemsPerPage',
127
                    'settings.pagination.maxNumPages',
128
                ],
129
                'template' => [
130
                    'settings.templateLayout',
131
                ],
132
            ];
133
134
            $flexformData = GeneralUtility::xml2array($fieldArray['pi_flexform']);
135
            if (!is_array($flexformData)) {
136
                return;
137
            }
138
139
            foreach ($checkFields as $sheet => $fields) {
140
                foreach ($fields as $field) {
141
                    if (isset($flexformData['data'][$sheet]['lDEF'][$field]['vDEF']) &&
142
                        ($flexformData['data'][$sheet]['lDEF'][$field]['vDEF'] === '' ||
143
                            $flexformData['data'][$sheet]['lDEF'][$field]['vDEF'] === '0')
144
                    ) {
145
                        unset($flexformData['data'][$sheet]['lDEF'][$field]);
146
                    }
147
                }
148
149
                // If remaining sheet does not contain fields, then remove the sheet
150
                if (isset($flexformData['data'][$sheet]['lDEF']) && $flexformData['data'][$sheet]['lDEF'] === []) {
151
                    unset($flexformData['data'][$sheet]);
152
                }
153
            }
154
155
            $flexFormTools = GeneralUtility::makeInstance(FlexFormTools::class);
156
            $fieldArray['pi_flexform'] = $flexFormTools->flexArray2Xml($flexformData, true);
157
        }
158
    }
159
160
    /**
161
     * Sets the TCA type of the fields 'registration' and 'registration_waitlist' to 'none' for copy and localize,
162
     * so field values will not be duplicated in the copy of the object.
163
     *
164
     * @param string $command
165
     * @param string $table
166
     * @param int $id
167
     * @param mixed $value
168
     * @param DataHandler $pObj
169
     * @param bool $pasteUpdate
170
     */
171
    public function processCmdmap_preProcess($command, $table, $id, $value, $pObj, $pasteUpdate): void
0 ignored issues
show
Unused Code introduced by
The parameter $pObj is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

171
    public function processCmdmap_preProcess($command, $table, $id, $value, /** @scrutinizer ignore-unused */ $pObj, $pasteUpdate): void

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $id is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

171
    public function processCmdmap_preProcess($command, $table, /** @scrutinizer ignore-unused */ $id, $value, $pObj, $pasteUpdate): void

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $pasteUpdate is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

171
    public function processCmdmap_preProcess($command, $table, $id, $value, $pObj, /** @scrutinizer ignore-unused */ $pasteUpdate): void

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $value is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

171
    public function processCmdmap_preProcess($command, $table, $id, /** @scrutinizer ignore-unused */ $value, $pObj, $pasteUpdate): void

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
172
    {
173
        if (in_array($command, ['copy', 'localize']) && $table === self::EVENT_TABLE) {
174
            $GLOBALS['TCA'][self::EVENT_TABLE]['columns']['registration']['config']['type'] = 'none';
175
            $GLOBALS['TCA'][self::EVENT_TABLE]['columns']['registration_waitlist']['config']['type'] = 'none';
176
        }
177
    }
178
179
    /**
180
     * (1) Sets the TCA type of certain fields back to their original state after a copy or move command
181
     * (2) Handles deletion of custom notifications when deleting an event
182
     *
183
     * @param string $command
184
     * @param string $table
185
     * @param int $id
186
     * @param string $value
187
     * @param DataHandler $pObj
188
     * @param bool $pasteUpdate
189
     * @param array $pasteDatamap
190
     */
191
    public function processCmdmap_postProcess($command, $table, $id, $value, $pObj, $pasteUpdate, $pasteDatamap): void
0 ignored issues
show
Unused Code introduced by
The parameter $pObj is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

191
    public function processCmdmap_postProcess($command, $table, $id, $value, /** @scrutinizer ignore-unused */ $pObj, $pasteUpdate, $pasteDatamap): void

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $pasteUpdate is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

191
    public function processCmdmap_postProcess($command, $table, $id, $value, $pObj, /** @scrutinizer ignore-unused */ $pasteUpdate, $pasteDatamap): void

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $pasteDatamap is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

191
    public function processCmdmap_postProcess($command, $table, $id, $value, $pObj, $pasteUpdate, /** @scrutinizer ignore-unused */ $pasteDatamap): void

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $value is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

191
    public function processCmdmap_postProcess($command, $table, $id, /** @scrutinizer ignore-unused */ $value, $pObj, $pasteUpdate, $pasteDatamap): void

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
192
    {
193
        if (in_array($command, ['copy', 'localize']) && $table === self::EVENT_TABLE) {
194
            $GLOBALS['TCA'][self::EVENT_TABLE]['columns']['registration']['config']['type'] = 'inline';
195
            $GLOBALS['TCA'][self::EVENT_TABLE]['columns']['registration_waitlist']['config']['type'] = 'inline';
196
        } elseif ($command === 'delete' && $table === self::EVENT_TABLE) {
197
            $this->deleteCustomNotificationsByEvent($id);
198
        }
199
    }
200
201
    /**
202
     * Removes all custom notification log entries for the given event UID
203
     */
204
    protected function deleteCustomNotificationsByEvent(int $eventUid): void
205
    {
206
        $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
207
            ->getQueryBuilderForTable(self::CUSTOMNOTIFICATIONLOG_TABLE);
208
        $queryBuilder
209
            ->delete(self::CUSTOMNOTIFICATIONLOG_TABLE)
210
            ->where(
211
                $queryBuilder->expr()->eq('event', $queryBuilder->createNamedParameter($eventUid, Connection::PARAM_INT))
212
            )
213
            ->executeStatement();
214
    }
215
}
216