Passed
Push — main ( f1cdfe...00a2ed )
by
unknown
02:53
created

TCA::getLanguageService()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
1
<?php
2
3
namespace Aoe\FeatureFlag\System\Typo3;
4
5
/***************************************************************
6
 *  Copyright notice
7
 *
8
 *  (c) 2021 AOE GmbH <[email protected]>
9
 *
10
 *  All rights reserved
11
 *
12
 *  This script is part of the TYPO3 project. The TYPO3 project is
13
 *  free software; you can redistribute it and/or modify
14
 *  it under the terms of the GNU General Public License as published by
15
 *  the Free Software Foundation; either version 3 of the License, or
16
 *  (at your option) any later version.
17
 *
18
 *  The GNU General Public License can be found at
19
 *  http://www.gnu.org/copyleft/gpl.html.
20
 *
21
 *  This script is distributed in the hope that it will be useful,
22
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
23
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24
 *  GNU General Public License for more details.
25
 *
26
 *  This copyright notice MUST APPEAR in all copies of the script!
27
 ***************************************************************/
28
29
use Aoe\FeatureFlag\Domain\Model\FeatureFlag;
30
use Aoe\FeatureFlag\Domain\Model\Mapping;
31
use Aoe\FeatureFlag\Domain\Repository\FeatureFlagRepository;
32
use Aoe\FeatureFlag\Domain\Repository\MappingRepository;
33
use Aoe\FeatureFlag\Service\Exception\FeatureNotFoundException;
34
use TYPO3\CMS\Core\DataHandling\DataHandler;
35
use TYPO3\CMS\Core\Localization\LanguageService;
36
use TYPO3\CMS\Core\Utility\GeneralUtility;
37
use TYPO3\CMS\Extbase\Persistence\Generic\PersistenceManager;
38
use TYPO3\CMS\Extbase\Persistence\QueryResultInterface;
39
40
class TCA
41
{
42
    /**
43
     * @var string
44
     */
45
    public const FIELD_BEHAVIOR = 'tx_featureflag_behavior';
46
47
    /**
48
     * @var string
49
     */
50
    public const FIELD_FLAG = 'tx_featureflag_flag';
51
52
    protected static ?array $hashedMappings = null;
53
54
    /**
55
     * Hook for updates in Typo3 backend
56
     * @codingStandardsIgnoreStart
57
     */
58 4
    public function processDatamap_preProcessFieldArray(
59
        array &$incomingFieldArray,
60
        string $table,
61
        string $id,
62
        DataHandler $dataHandler
63
    ): void {
64
        // @codingStandardsIgnoreEnd
65
        if (
66 4
            array_key_exists(self::FIELD_BEHAVIOR, $incomingFieldArray) &&
67 4
            array_key_exists(self::FIELD_FLAG, $incomingFieldArray)
68
        ) {
69 3
            $pid = $dataHandler->getPID($table, (int) $id);
70 3
            $this->updateMapping($table, $id, $incomingFieldArray[self::FIELD_FLAG], $pid, $incomingFieldArray[self::FIELD_BEHAVIOR]);
71 3
            unset($incomingFieldArray[self::FIELD_BEHAVIOR]);
72 3
            unset($incomingFieldArray[self::FIELD_FLAG]);
73
        }
74 4
    }
75
76
    /**
77
     * Hook for deletes in Typo3 Backend. It also delete all overwrite protection
78
     * @codingStandardsIgnoreStart
79
     */
80 2
    public function processCmdmap_postProcess(string $command, string $table, int $id): void
81
    {
82
        // @codingStandardsIgnoreEnd
83 2
        if ($command !== 'delete') {
84 1
            return;
85
        }
86 1
        $mappings = $this->getMappingRepository()
87 1
            ->findAllByForeignTableNameAndUid($id, $table);
88 1
        if (!is_array($mappings) && !$mappings instanceof QueryResultInterface) {
89
            return;
90
        }
91 1
        foreach ($mappings as $mapping) {
92 1
            if ($mapping instanceof Mapping) {
93 1
                $this->getMappingRepository()
94 1
                    ->remove($mapping);
95
            }
96
        }
97 1
        $this->getPersistenceManager()
98 1
            ->persistAll();
99 1
    }
100
101
    public function postOverlayPriorityLookup(string $table, array $row, array $status, string $iconName): string
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

101
    public function postOverlayPriorityLookup(string $table, array $row, /** @scrutinizer ignore-unused */ array $status, string $iconName): string

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...
102
    {
103
        if ($row['uid'] && $this->isMappingAvailableForTableAndUid($row['uid'], $table)) {
104
            $mapping = $this->getMappingRepository()
105
                ->findOneByForeignTableNameAndUid($row['uid'], $table);
106
            if ($mapping instanceof Mapping) {
107
                if ($row['hidden'] === '1') {
108
                    return 'record-has-feature-flag-which-is-hidden';
109
                }
110
                if ($iconName !== '') {
111
                    // if record is e.g. hidden or protected by FE-group, than show that (e.g. 'hidden' or 'fe_group'-)overlay as default
112
                    return $iconName;
113
                }
114
                return 'record-has-feature-flag-which-is-visible';
115
            }
116
        }
117
118
        // return given icon-name as fall-back
119
        return $iconName;
120
    }
121
122
    /**
123
     * @todo fix code style
124
     */
125 3
    protected function updateMapping(string $table, string $id, $featureFlag, $pid, string $behavior): void
126
    {
127 3
        $mapping = $this->getMappingRepository()
128 3
            ->findOneByForeignTableNameAndUid($id, $table);
129 3
        if ($mapping instanceof Mapping) {
130 1
            if ($featureFlag == '0') {
131 1
                $this->getMappingRepository()
132 1
                    ->remove($mapping);
133
            } else {
134
                $mapping->setFeatureFlag($this->getFeatureFlagByUid($featureFlag));
135
                $mapping->setBehavior($behavior);
136
            }
137 1
            $mapping->setTstamp((string) time());
138 1
            $this->getMappingRepository()
139 1
                ->update($mapping);
140 2
        } elseif ($featureFlag !== '0') {
141
            /** @var Mapping $mapping */
142 1
            $mapping = new Mapping();
143 1
            $mapping->setPid($pid);
144 1
            $mapping->setFeatureFlag($this->getFeatureFlagByUid($featureFlag));
145 1
            $mapping->setForeignTableName($table);
146 1
            $mapping->setForeignTableUid((int) $id);
147 1
            $mapping->setCrdate((string) time());
148 1
            $mapping->setTstamp((string) time());
149 1
            $mapping->setBehavior($behavior);
150 1
            $this->getMappingRepository()
151 1
                ->add($mapping);
152
        }
153 3
        $this->getPersistenceManager()
154 3
            ->persistAll();
155 3
    }
156
157
    protected function isMappingAvailableForTableAndUid(string $foreignTableUid, string $foreignTableName): bool
158
    {
159
        if (self::$hashedMappings === null) {
160
            self::$hashedMappings = $this->getMappingRepository()->getHashedMappings();
161
        }
162
        $identifier = sha1($foreignTableUid . '_' . $foreignTableName);
163
164
        return array_key_exists($identifier, self::$hashedMappings);
165
    }
166
167
    /**
168
     * @throws FeatureNotFoundException
169
     */
170
    protected function getFeatureFlagByUid(int $uid): FeatureFlag
171
    {
172
        /** @var FeatureFlag $featureFlag */
173
        $featureFlag = $this->getFeatureFlagRepository()
174
            ->findByUid($uid);
175
176
        return $featureFlag;
177
    }
178
179
    protected function getMappingRepository(): object
180
    {
181
        return GeneralUtility::makeInstance(MappingRepository::class);
182
    }
183
184
    protected function getFeatureFlagRepository(): object
185
    {
186
        return GeneralUtility::makeInstance(FeatureFlagRepository::class);
187
    }
188
189
    protected function getPersistenceManager(): object
190
    {
191
        return GeneralUtility::makeInstance(PersistenceManager::class);
192
    }
193
194
    protected function getLanguageService(): LanguageService
195
    {
196
        return $GLOBALS['LANG'];
197
    }
198
}
199