Passed
Push — main ( 9b4de3...46b341 )
by Felix
02:33
created

TcaPostProcessor   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Test Coverage

Coverage 90%

Importance

Changes 0
Metric Value
eloc 35
c 0
b 0
f 0
dl 0
loc 65
ccs 9
cts 10
cp 0.9
rs 10
wmc 4

2 Methods

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