Passed
Push — main ( 400cf4...75e45b )
by Felix
03:05
created

getTcaTablesWithFeatureFlagSupport()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 0
dl 0
loc 8
ccs 0
cts 5
cp 0
crap 6
rs 10
c 0
b 0
f 0
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 TYPO3\CMS\Core\Configuration\Event\AfterTcaCompilationEvent;
30
use TYPO3\CMS\Core\Configuration\ExtensionConfiguration;
31
use TYPO3\CMS\Core\Utility\ExtensionManagementUtility;
32
use TYPO3\CMS\Core\Utility\GeneralUtility;
33
34
class TcaPostProcessor
35
{
36
    /**
37
     * Add feature-flag-fields to TCA-fields of DB-tables which support feature-flags
38
     */
39
    public function postProcessTca(AfterTcaCompilationEvent $event): void
40
    {
41
        foreach ($this->getTcaTablesWithFeatureFlagSupport() as $table) {
42
            ExtensionManagementUtility::addTCAcolumns(
43
                $table,
44
                [
45
                    'tx_featureflag_info' => [
46
                        'exclude' => 1,
47
                        'label' => 'LLL:EXT:feature_flag/Resources/Private/Language/locallang.xlf:tx_featureflag_info.label',
48
                        'config' => [
49
                            'type' => 'user',
50
                            'renderType' => 'infoText',
51
                        ],
52
                    ],
53
                    'tx_featureflag_flag' => [
54
                        'exclude' => 1,
55
                        'label' => 'LLL:EXT:feature_flag/Resources/Private/Language/locallang.xlf:tx_featureflag_flag',
56
                        'config' => [
57
                            'type' => 'user',
58
                            'renderType' => 'selectFeatureFlag',
59
                            'size' => 1,
60
                        ],
61
                    ],
62
                    'tx_featureflag_behavior' => [
63
                        'exclude' => 1,
64
                        'label' => 'LLL:EXT:feature_flag/Resources/Private/Language/locallang.xlf:tx_featureflag_behavior',
65
                        'config' => [
66
                            'type' => 'user',
67
                            'renderType' => 'selectFeatureFlagBehaviour',
68
                            'size' => 1,
69
                        ],
70
                    ],
71
                ]
72
            );
73
            ExtensionManagementUtility::addToAllTCAtypes(
74
                $table,
75
                '
76
                    --div--;LLL:EXT:feature_flag/Resources/Private/Language/locallang.xlf:feature_flag,
77
                        tx_featureflag_info, tx_featureflag_flag, tx_featureflag_behavior
78
                '
79
            );
80
        }
81
82
        $event->setTca($GLOBALS['TCA']);
83
    }
84
85
    private function getTcaTablesWithFeatureFlagSupport(): array
86
    {
87
        $config = GeneralUtility::makeInstance(ExtensionConfiguration::class)->get('feature_flag');
88
89
        if (isset($config['tables'])) {
90
            return explode(',', $config['tables']);
91
        }
92
        return [];
93
    }
94
}
95