Passed
Push — TYPO3_11 ( 042e4a...3080e3 )
by
unknown
03:07
created

TCA::getObjectManager()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 3
c 0
b 0
f 0
nc 2
nop 0
dl 0
loc 7
ccs 0
cts 4
cp 0
crap 6
rs 10
1
<?php
2
namespace Aoe\FeatureFlag\System\Typo3;
3
4
/***************************************************************
5
 *  Copyright notice
6
 *
7
 *  (c) 2021 AOE GmbH <[email protected]>
8
 *
9
 *  All rights reserved
10
 *
11
 *  This script is part of the TYPO3 project. The TYPO3 project is
12
 *  free software; you can redistribute it and/or modify
13
 *  it under the terms of the GNU General Public License as published by
14
 *  the Free Software Foundation; either version 3 of the License, or
15
 *  (at your option) any later version.
16
 *
17
 *  The GNU General Public License can be found at
18
 *  http://www.gnu.org/copyleft/gpl.html.
19
 *
20
 *  This script is distributed in the hope that it will be useful,
21
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
22
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23
 *  GNU General Public License for more details.
24
 *
25
 *  This copyright notice MUST APPEAR in all copies of the script!
26
 ***************************************************************/
27
28
use Aoe\FeatureFlag\Domain\Model\FeatureFlag;
29
use Aoe\FeatureFlag\Domain\Model\Mapping;
30
use Aoe\FeatureFlag\Domain\Repository\FeatureFlagRepository;
31
use Aoe\FeatureFlag\Domain\Repository\MappingRepository;
32
use Aoe\FeatureFlag\Service\Exception\FeatureNotFoundException;
33
use TYPO3\CMS\Core\DataHandling\DataHandler;
34
use TYPO3\CMS\Core\Localization\LanguageService;
35
use TYPO3\CMS\Core\Utility\GeneralUtility;
36
use TYPO3\CMS\Extbase\Persistence\Generic\PersistenceManager;
37
use TYPO3\CMS\Extbase\Persistence\QueryResultInterface;
38
39
class TCA
40
{
41
    /**
42
     * @var string
43
     */
44
    const FIELD_BEHAVIOR = 'tx_featureflag_behavior';
45
46
    /**
47
     * @var string
48
     */
49
    const FIELD_FLAG = 'tx_featureflag_flag';
50
51
    /**
52
     * @var QueryResultInterface
53
     */
54
    protected static $hashedMappings;
55
56
    /**
57
     * Hook for updates in Typo3 backend
58
     * @param array $incomingFieldArray
59
     * @param string $table
60
     * @param integer $id
61
     * @param DataHandler $dataHandler
62
     * @codingStandardsIgnoreStart
63
     */
64
    public function processDatamap_preProcessFieldArray(
65
        &$incomingFieldArray,
66
        $table,
67
        $id,
68
        DataHandler $dataHandler
69
    ) {
70
        // @codingStandardsIgnoreEnd
71
        if (
72
            array_key_exists(self::FIELD_BEHAVIOR, $incomingFieldArray) &&
73
            array_key_exists(self::FIELD_FLAG, $incomingFieldArray)
74
        ) {
75
            $pid = $dataHandler->getPID($table, $id);
76
            $this->updateMapping($table, $id, $incomingFieldArray[self::FIELD_FLAG], $pid, $incomingFieldArray[self::FIELD_BEHAVIOR]);
0 ignored issues
show
Bug introduced by
It seems like $pid can also be of type false; however, parameter $pid of Aoe\FeatureFlag\System\Typo3\TCA::updateMapping() does only seem to accept integer, maybe add an additional type check? ( Ignorable by Annotation )

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

76
            $this->updateMapping($table, $id, $incomingFieldArray[self::FIELD_FLAG], /** @scrutinizer ignore-type */ $pid, $incomingFieldArray[self::FIELD_BEHAVIOR]);
Loading history...
77
            unset($incomingFieldArray[self::FIELD_BEHAVIOR]);
78
            unset($incomingFieldArray[self::FIELD_FLAG]);
79
        }
80
    }
81
82
    /**
83
     * Hook for deletes in Typo3 Backend. It also delete all overwrite protection
84
     * @param string $command
85
     * @param string $table
86
     * @param integer $id
87
     * @codingStandardsIgnoreStart
88
     */
89
    public function processCmdmap_postProcess($command, $table, $id)
90
    {
91
        // @codingStandardsIgnoreEnd
92
        if ($command !== 'delete') {
93
            return;
94
        }
95
        $mappings = $this->getMappingRepository()->findAllByForeignTableNameAndUid($id, $table);
96
        if (false === is_array($mappings) && false === ($mappings instanceof QueryResultInterface)) {
97
            return;
98
        }
99
        foreach ($mappings as $mapping) {
100
            if ($mapping instanceof Mapping) {
101
                $this->getMappingRepository()->remove($mapping);
102
            }
103
        }
104
        $this->getPersistenceManager()->persistAll();
105
    }
106
107
    /**
108
     * @param string $table
109
     * @param array $row
110
     * @param string $status
111
     * @param string $iconName
112
     * @return string
113
     */
114
    public function postOverlayPriorityLookup($table, $row, &$status, $iconName)
0 ignored issues
show
Unused Code introduced by
The parameter $status 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

114
    public function postOverlayPriorityLookup($table, $row, /** @scrutinizer ignore-unused */ &$status, $iconName)

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...
115
    {
116
        if ($this->isMappingAvailableForTableAndUid($row['uid'], $table)) {
117
            $mapping = $this->getMappingRepository()->findOneByForeignTableNameAndUid($row['uid'], $table);
118
            if ($mapping instanceof Mapping) {
0 ignored issues
show
introduced by
$mapping is always a sub-type of Aoe\FeatureFlag\Domain\Model\Mapping.
Loading history...
119
                if ($row['hidden'] === '1') {
120
                    return 'record-has-feature-flag-which-is-hidden';
121
                }
122
                if ($iconName !== '') {
123
                    // if record is e.g. hidden or protected by FE-group, than show that (e.g. 'hidden' or 'fe_group'-)overlay as default
124
                    return $iconName;
125
                }
126
                return 'record-has-feature-flag-which-is-visible';
127
            }
128
        }
129
130
        // return given icon-name as fall-back
131
        return $iconName;
132
    }
133
134
    /**
135
     * @param string $table
136
     * @param int $id
137
     * @param int $featureFlag
138
     * @param int $pid
139
     * @param string $behavior
140
     */
141
    protected function updateMapping($table, $id, $featureFlag, $pid, $behavior)
142
    {
143
        $mapping = $this->getMappingRepository()->findOneByForeignTableNameAndUid($id, $table);
144
        if ($mapping instanceof Mapping) {
0 ignored issues
show
introduced by
$mapping is always a sub-type of Aoe\FeatureFlag\Domain\Model\Mapping.
Loading history...
145
            if ('0' === $featureFlag) {
0 ignored issues
show
introduced by
The condition '0' === $featureFlag is always false.
Loading history...
146
                $this->getMappingRepository()->remove($mapping);
147
            } else {
148
                $mapping->setFeatureFlag($this->getFeatureFlagByUid($featureFlag));
149
                $mapping->setBehavior($behavior);
150
            }
151
            $mapping->setTstamp(time());
152
            $this->getMappingRepository()->update($mapping);
153
        } elseif ('0' !== $featureFlag) {
154
            /** @var Mapping $mapping */
155
            $mapping = new Mapping();
156
            $mapping->setPid($pid);
157
            $mapping->setFeatureFlag($this->getFeatureFlagByUid($featureFlag));
158
            $mapping->setForeignTableName($table);
159
            $mapping->setForeignTableUid($id);
160
            $mapping->setCrdate(time());
161
            $mapping->setTstamp(time());
162
            $mapping->setBehavior($behavior);
163
            $this->getMappingRepository()->add($mapping);
164
        }
165
        $this->getPersistenceManager()->persistAll();
166
    }
167
168
    /**
169
     * @param int $foreignTableUid
170
     * @param string $foreignTableName
171
     * @return bool
172
     */
173
    protected function isMappingAvailableForTableAndUid($foreignTableUid, $foreignTableName)
174
    {
175
        if (null === self::$hashedMappings) {
176
            self::$hashedMappings = $this->getMappingRepository()->getHashedMappings();
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->getMappingReposit...()->getHashedMappings() of type array or array is incompatible with the declared type TYPO3\CMS\Extbase\Persistence\QueryResultInterface of property $hashedMappings.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
177
        }
178
        $identifier = sha1($foreignTableUid . '_' . $foreignTableName);
179
        if (array_key_exists($identifier, self::$hashedMappings)) {
0 ignored issues
show
Bug introduced by
It seems like self::hashedMappings can also be of type TYPO3\CMS\Extbase\Persistence\QueryResultInterface; however, parameter $array of array_key_exists() does only seem to accept ArrayObject|array, maybe add an additional type check? ( Ignorable by Annotation )

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

179
        if (array_key_exists($identifier, /** @scrutinizer ignore-type */ self::$hashedMappings)) {
Loading history...
180
            return true;
181
        }
182
183
        return false;
184
    }
185
186
    /**
187
     * @param int $uid
188
     * @return FeatureFlag
189
     * @throws FeatureNotFoundException
190
     */
191
    protected function getFeatureFlagByUid($uid)
192
    {
193
        /** @var FeatureFlag $featureFlag */
194
        $featureFlag = $this->getFeatureFlagRepository()->findByUid($uid);
195
        if (false === ($featureFlag instanceof FeatureFlag)) {
196
            throw new FeatureNotFoundException(
197
                'Feature Flag not found by uid: "' . $uid . '"',
198
                1384340431
199
            );
200
        }
201
202
        return $featureFlag;
203
    }
204
205
    /**
206
     * @return MappingRepository
207
     */
208
    protected function getMappingRepository()
209
    {
210
        return GeneralUtility::makeInstance(MappingRepository::class);
211
    }
212
213
    /**
214
     * @return FeatureFlagRepository
215
     */
216
    protected function getFeatureFlagRepository()
217
    {
218
        return GeneralUtility::makeInstance(FeatureFlagRepository::class);
219
    }
220
221
    /**
222
     * @return PersistenceManager
223
     */
224
    protected function getPersistenceManager()
225
    {
226
        return GeneralUtility::makeInstance(PersistenceManager::class);
227
    }
228
229
    /**
230
     * @return LanguageService
231
     */
232
    protected function getLanguageService()
233
    {
234
        return $GLOBALS['LANG'];
235
    }
236
}
237