Completed
Push — master ( fb64ee...e0a775 )
by
unknown
12:51
created

processDatamap_afterDatabaseOperations()   D

Complexity

Conditions 10
Paths 4

Size

Total Lines 41
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 41
rs 4.8196
c 0
b 0
f 0
cc 10
eloc 25
nc 4
nop 5

How to fix   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 Aoe\AoeDbSequenzer\Service;
3
4
/***************************************************************
5
 *  Copyright notice
6
 *
7
 *  (c) 2017 AOE GmbH ([email protected])
8
 *  All rights reserved
9
 *
10
 *  This script is part of the TYPO3 project. The TYPO3 project is
11
 *  free software; you can redistribute it and/or modify
12
 *  it under the terms of the GNU General Public License as published by
13
 *  the Free Software Foundation; either version 2 of the License, or
14
 *  (at your option) any later version.
15
 *
16
 *  The GNU General Public License can be found at
17
 *  http://www.gnu.org/copyleft/gpl.html.
18
 *
19
 *  This script is distributed in the hope that it will be useful,
20
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
21
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22
 *  GNU General Public License for more details.
23
 *
24
 *  This copyright notice MUST APPEAR in all copies of the script!
25
 ***************************************************************/
26
27
use Aoe\AoeDbSequenzer\Domain\Model\OverwriteProtection;
28
use Aoe\AoeDbSequenzer\Domain\Repository\OverwriteProtectionRepository;
29
use TYPO3\CMS\Core\DataHandling\DataHandler;
30
use TYPO3\CMS\Core\Utility\GeneralUtility;
31
use TYPO3\CMS\Extbase\Object\ObjectManager;
32
use TYPO3\CMS\Extbase\Object\ObjectManagerInterface;
33
use TYPO3\CMS\Extbase\Persistence\Generic\PersistenceManager;
34
35
/**
36
 * @package Aoe\AoeDbSequenzer
37
 */
38
class OverwriteProtectionService
39
{
40
    /**
41
     * @var string
42
     */
43
    const OVERWRITE_PROTECTION_TABLE = 'tx_aoedbsequenzer_domain_model_overwriteprotection';
44
45
    /**
46
     * @var string
47
     */
48
    const OVERWRITE_PROTECTION_TILL = 'tx_aoe_dbsquenzer_protectoverwrite_till';
49
50
    /**
51
     * @var string
52
     */
53
    const OVERWRITE_PROTECTION_MODE = 'tx_aoe_dbsquenzer_protectoverwrite_mode';
54
55
    /**
56
     * array of configured tables that should call the sequenzer
57
     *
58
     * @var array
59
     */
60
    private $supportedTables;
61
62
    /**
63
     * @var OverwriteProtectionRepository
64
     */
65
    private $overwriteProtectionRepository;
66
67
    /**
68
     * @var PersistenceManager
69
     */
70
    private $persistenceManager;
71
72
    /**
73
     * @var ObjectManagerInterface
74
     */
75
    private $objectManager;
76
77
    public function __construct()
78
    {
79
        $extConf = unserialize($GLOBALS ['TYPO3_CONF_VARS'] ['EXT'] ['extConf'] ['aoe_dbsequenzer']);
80
        $explodedValues = explode(',', $extConf ['tables']);
81
        $this->supportedTables = array_map('trim', $explodedValues);
82
83
        $this->objectManager = GeneralUtility::makeInstance(ObjectManager::class);
84
        $this->overwriteProtectionRepository = $this->objectManager->get(OverwriteProtectionRepository::class);
85
        $this->persistenceManager = $this->objectManager->get(PersistenceManager::class);
86
    }
87
88
    /**
89
     * Hook for deletes in Typo3 Backend. It also delete all overwrite protection
90
     * @param string $command
91
     * @param string $table
92
     * @param integer $id
93
     */
94
    public function processCmdmap_postProcess($command, $table, $id)
95
    {
96
        if (false === $this->needsOverWriteProtection($table)) {
97
            return;
98
        }
99
        if ($command !== 'delete') {
100
            return;
101
        }
102
        $this->removeOverwriteProtection($id, $table);
103
    }
104
105
    /**
106
     * Hook for updates in Typo3 backend
107
     * @param array $incomingFieldArray
108
     * @param string $table
109
     * @param integer $id
110
     * @param DataHandler $dataHandler
111
     */
112
    public function processDatamap_preProcessFieldArray(&$incomingFieldArray, $table, $id, DataHandler &$dataHandler)
113
    {
114
        if (false === $this->needsOverWriteProtection($table)) {
115
            return;
116
        }
117
118
        // check, if overwrite-protection-fields are set:
119
        // If they are NOT set, it means, that any other extension maybe called the process_datamap!
120
        if (false === array_key_exists(self::OVERWRITE_PROTECTION_TILL, $incomingFieldArray) ||
121
            false === array_key_exists(self::OVERWRITE_PROTECTION_MODE, $incomingFieldArray)
122
        ) {
123
            return;
124
        }
125
126
        // Only handle overwrite protection if database record is not new
127
        if (GeneralUtility::isFirstPartOfStr($id, 'NEW')) {
128
            unset($incomingFieldArray [self::OVERWRITE_PROTECTION_TILL]);
129
            unset($incomingFieldArray [self::OVERWRITE_PROTECTION_MODE]);
130
            return;
131
        }
132
133
        if (false === $this->hasOverWriteProtection($incomingFieldArray)) {
134
            $this->removeOverwriteProtection($id, $table);
135
        } else {
136
            $protectionTime = $incomingFieldArray [self::OVERWRITE_PROTECTION_TILL];
137
            $mode = $incomingFieldArray [self::OVERWRITE_PROTECTION_MODE];
138
139
            $protectionTime = $this->convertClientTimestampToUTC($protectionTime, $table, $dataHandler);
140
141
            $queryResult = $this->overwriteProtectionRepository->findByProtectedUidAndTableName($id, $table);
142
            if ($queryResult->count() === 0) {
143
                /* @var $overwriteProtection OverwriteProtection */
144
                $overwriteProtection = $this->objectManager->get(OverwriteProtection::class);
145
                $overwriteProtection->setProtectedMode($mode);
146
                $overwriteProtection->setPid($dataHandler->getPID($table, $id));
0 ignored issues
show
Security Bug introduced by
It seems like $dataHandler->getPID($table, $id) targeting TYPO3\CMS\Core\DataHandling\DataHandler::getPID() can also be of type false; however, TYPO3\CMS\Extbase\Domain...tDomainObject::setPid() does only seem to accept integer|null, did you maybe forget to handle an error condition?
Loading history...
147
                $overwriteProtection->setProtectedTablename($table);
148
                $overwriteProtection->setProtectedUid($id);
149
                $overwriteProtection->setProtectedTime($protectionTime);
150
                $this->overwriteProtectionRepository->add($overwriteProtection);
151
            } else {
152
                /* @var $overwriteProtection OverwriteProtection */
153
                $overwriteProtection = $queryResult->getFirst();
154
                $overwriteProtection->setProtectedMode($mode);
155
                $overwriteProtection->setProtectedTime($protectionTime);
156
                $this->overwriteProtectionRepository->update($overwriteProtection);
157
            }
158
            $this->persistenceManager->persistAll();
159
        }
160
161
        unset($incomingFieldArray [self::OVERWRITE_PROTECTION_TILL]);
162
        unset($incomingFieldArray [self::OVERWRITE_PROTECTION_MODE]);
163
    }
164
165
    /**
166
     * @param string $status Status "new" or "update"
167
     * @param string $table Table name
168
     * @param string $id Record ID. If new record its a string pointing to index inside \TYPO3\CMS\Core\DataHandling\DataHandler::substNEWwithIDs
169
     * @param array $fieldArray Field array of updated fields in the operation
170
     * @param DataHandler $dataHandler tcemain calling object
171
     * @return void
172
     */
173
    public function processDatamap_afterDatabaseOperations($status, $table, $id, $fieldArray, $dataHandler)
0 ignored issues
show
Unused Code introduced by
The parameter $fieldArray is not used and could be removed.

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

Loading history...
174
    {
175
        // check basic pre-conditions - only handle new database record
176
        if ($status !== 'new' ||
177
            false === GeneralUtility::isFirstPartOfStr($id, 'NEW') ||
178
            false === $this->needsOverWriteProtection($table)
179
        ) {
180
            return;
181
        }
182
183
        // check if all required dataHandler fields are available
184
        if (!isset($dataHandler->datamap[$table]) ||
185
            !isset($dataHandler->datamap[$table][$id]) ||
186
            !isset($dataHandler->datamap[$table][$id][self::OVERWRITE_PROTECTION_TILL]) ||
187
            !isset($dataHandler->datamap[$table][$id][self::OVERWRITE_PROTECTION_MODE]) ||
188
            !isset($dataHandler->substNEWwithIDs[$id])
189
        ) {
190
            return;
191
        }
192
193
        // check if overwrite protection fields are filled
194
        if (false === $this->hasOverWriteProtection($dataHandler->datamap[$table][$id])) {
195
            return;
196
        }
197
198
        $uid = $dataHandler->substNEWwithIDs[$id];
199
        $protectionTime = $dataHandler->datamap[$table][$id][self::OVERWRITE_PROTECTION_TILL];
200
        $mode = $dataHandler->datamap[$table][$id][self::OVERWRITE_PROTECTION_MODE];
201
202
        $protectionTime = $this->convertClientTimestampToUTC($protectionTime, $table, $dataHandler);
203
204
        $overwriteProtection = $this->objectManager->get(OverwriteProtection::class);
205
        $overwriteProtection->setProtectedMode($mode);
206
        $overwriteProtection->setPid($dataHandler->getPID($table, $uid));
207
        $overwriteProtection->setProtectedTablename($table);
208
        $overwriteProtection->setProtectedUid($uid);
209
        $overwriteProtection->setProtectedTime($protectionTime);
210
211
        $this->overwriteProtectionRepository->add($overwriteProtection);
212
        $this->persistenceManager->persistAll();
213
    }
214
215
    /**
216
     * @param array $fields_values
217
     * @return boolean
218
     */
219
    private function hasOverWriteProtection(array $fields_values)
220
    {
221
        if (isset ($fields_values [self::OVERWRITE_PROTECTION_TILL])) {
222
            $value = trim($fields_values [self::OVERWRITE_PROTECTION_TILL]);
223
            if (false === empty ($value) && false !== is_numeric($value)) {
224
                return true;
225
            }
226
        }
227
        return false;
228
    }
229
230
    /**
231
     * If a table is configured to use the sequenzer
232
     *
233
     * @param string $tableName
234
     * @return boolean
235
     */
236
    private function needsOverWriteProtection($tableName)
237
    {
238
        if ($tableName !== self::OVERWRITE_PROTECTION_TABLE && in_array($tableName, $this->supportedTables)) {
239
            return true;
240
        }
241
        return false;
242
    }
243
244
    /**
245
     * remove overwriteProtection
246
     *
247
     * @param integer $id
248
     * @param string $table
249
     */
250
    private function removeOverwriteProtection($id, $table)
251
    {
252
        $queryResult = $this->overwriteProtectionRepository->findByProtectedUidAndTableName($id, $table);
253
        if ($queryResult->count() > 0) {
254
            $overwriteProtection = $queryResult->getFirst();
255
            $this->overwriteProtectionRepository->remove($overwriteProtection);
256
            $this->persistenceManager->persistAll();
257
        }
258
    }
259
260
    /**
261
     * @param string $dateTimeValue
262
     * @param string $table
263
     * @param DataHandler $dataHandler
264
     * @return string
265
     */
266
    private function convertClientTimestampToUTC($dateTimeValue, $table, DataHandler $dataHandler)
267
    {
268
        $evalArray = explode(',', $GLOBALS['TCA'][$table]['columns'][self::OVERWRITE_PROTECTION_TILL]['config']['eval']);
269
270
        $evalResult = $dataHandler->checkValue_input_Eval($dateTimeValue, $evalArray, null);
271
272
        if (isset($evalResult['value'])) {
273
            return $evalResult['value'];
274
        }
275
276
        return $dateTimeValue;
277
    }
278
}
279