Passed
Push — main ( 7381d9...bdb840 )
by Felix
10:26
created

FeatureFlagService   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 108
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 26
dl 0
loc 108
ccs 26
cts 26
cp 1
rs 10
c 0
b 0
f 0
wmc 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A flagEntries() 0 4 2
A isFeatureEnabled() 0 3 1
A getFeatureFlag() 0 18 5
A __construct() 0 8 1
A updateFeatureFlag() 0 9 1
1
<?php
2
namespace Aoe\FeatureFlag\Service;
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\Repository\FeatureFlagRepository;
30
use Aoe\FeatureFlag\Service\Exception\FeatureNotFoundException;
31
use Aoe\FeatureFlag\System\Typo3\Configuration;
32
use RuntimeException;
33
use TYPO3\CMS\Extbase\Persistence\Exception\IllegalObjectTypeException;
34
use TYPO3\CMS\Extbase\Persistence\PersistenceManagerInterface;
35
36
class FeatureFlagService
37
{
38
    /**
39
     * @var int
40
     */
41
    const BEHAVIOR_HIDE = 0;
42
43
    /**
44
     * @var int
45
     */
46
    const BEHAVIOR_SHOW = 1;
47
48
    /**
49
     * @var FeatureFlagRepository
50
     */
51
    private $featureFlagRepository;
52
53
    /**
54
     * @var PersistenceManagerInterface
55
     */
56
    private $persistenceManager;
57
58
    /**
59
     * @var Configuration
60
     */
61
    private $configuration;
62
63
    /**
64
     * @var array
65
     */
66
    private $cachedFlags = [];
67
68
    /**
69
     * @param FeatureFlagRepository $featureFlagRepository
70
     * @param PersistenceManagerInterface $persistenceManager
71
     * @param Configuration $configuration
72
     */
73 6
    public function __construct(
74
        FeatureFlagRepository $featureFlagRepository,
75
        PersistenceManagerInterface $persistenceManager,
76
        Configuration $configuration
77
    ) {
78 6
        $this->featureFlagRepository = $featureFlagRepository;
79 6
        $this->persistenceManager = $persistenceManager;
80 6
        $this->configuration = $configuration;
81 6
    }
82
83
    /**
84
     * @param string $flag
85
     * @return FeatureFlag
86
     * @throws FeatureNotFoundException
87
     * @throws RuntimeException
88
     * @return boolean
89
     */
90 4
    protected function getFeatureFlag($flag)
91
    {
92 4
        if (false === is_array($GLOBALS['TCA']) || false === isset($GLOBALS['TCA']['tx_featureflag_domain_model_featureflag'])) {
93
            // This can happen, when we call a REST-endpoint (by using restler-extension without initialized FE) and TYPO3 7:
94
            // Without TCA, we would load (in this and ALL other following PHP-requests) the featureFlag without initialized
95
            // 'property-values' (method 'FeatureFlag::isEnabled' would return NULL), so we MUST
96
            // avoid to load any featureFlag without correct loaded TCA!
97 1
            throw new RuntimeException('TCA is not loaded - we can\'t load featureFlag "'.$flag.'"');
98
        }
99
100 3
        if (false === array_key_exists($flag, $this->cachedFlags)) {
101 3
            $flagModel = $this->featureFlagRepository->findByFlag($flag);
102 3
            if (false === $flagModel instanceof FeatureFlag) {
103 1
                throw new FeatureNotFoundException('Feature Flag not found: "' . $flag . '"', 1383842028);
104
            }
105 2
            $this->cachedFlags[$flag] = $flagModel;
106
        }
107 2
        return $this->cachedFlags[$flag];
108
    }
109
110
    /**
111
     * @param $flag
112
     * @return bool
113
     * @throws FeatureNotFoundException
114
     */
115 4
    public function isFeatureEnabled($flag)
116
    {
117 4
        return $this->getFeatureFlag($flag)->isEnabled();
118
    }
119
120
    /**
121
     * @param $flag
122
     * @param $enabled
123
     * @throws FeatureNotFoundException
124
     * @throws IllegalObjectTypeException
125
     */
126 1
    public function updateFeatureFlag($flag, $enabled)
127
    {
128 1
        $flagModel = $this->getFeatureFlag($flag);
129 1
        $flagModel->setEnabled($enabled);
130
131 1
        $this->featureFlagRepository->update($flagModel);
132 1
        $this->persistenceManager->persistAll();
133
134 1
        $this->cachedFlags[$flag] = $flagModel;
135 1
    }
136
137
    /**
138
     * Flags entries in database
139
     */
140 1
    public function flagEntries()
141
    {
142 1
        foreach ($this->configuration->getTables() as $table) {
143 1
            $this->featureFlagRepository->updateFeatureFlagStatusForTable($table);
144
        }
145
    }
146
}