FeatureFlagService::flagEntries()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 2
nc 2
nop 0
dl 0
loc 4
ccs 0
cts 0
cp 0
crap 6
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Aoe\FeatureFlag\Service;
4
5
/***************************************************************
6
 *  Copyright notice
7
 *
8
 *  (c) 2024 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\Repository\FeatureFlagRepository;
31
use Aoe\FeatureFlag\Service\Exception\FeatureNotFoundException;
32
use Aoe\FeatureFlag\System\Typo3\Configuration;
33
use TYPO3\CMS\Extbase\Persistence\PersistenceManagerInterface;
34
35
class FeatureFlagService
36
{
37
    /**
38
     * @var int
39
     */
40
    public const BEHAVIOR_HIDE = 0;
41
42
    /**
43
     * @var int
44
     */
45
    public const BEHAVIOR_SHOW = 1;
46
47
    private array $cachedFlags = [];
48
49
    public function __construct(
50
        private readonly FeatureFlagRepository $featureFlagRepository,
51
        private readonly PersistenceManagerInterface $persistenceManager,
52
        private readonly Configuration $configuration
53
    ) {
54
    }
55
56
    public function isFeatureEnabled(string $flag): bool
57 4
    {
58
        return $this->getFeatureFlag($flag)
59
            ->isEnabled();
60
    }
61
62 4
    public function updateFeatureFlag(string $flag, bool $enabled): void
63 4
    {
64 4
        $flagModel = $this->getFeatureFlag($flag);
65 4
        $flagModel->setEnabled($enabled);
66
67
        $this->featureFlagRepository->update($flagModel);
68
        $this->persistenceManager->persistAll();
69
70 3
        $this->cachedFlags[$flag] = $flagModel;
71
    }
72 3
73 2
    /**
74
     * Flags entries in database
75
     */
76
    public function flagEntries(): void
77
    {
78
        foreach ($this->configuration->getTables() as $table) {
79
            $this->featureFlagRepository->updateFeatureFlagStatusForTable($table);
80 1
        }
81
    }
82 1
83 1
    protected function getFeatureFlag(string $flag): FeatureFlag
84
    {
85 1
        if (!array_key_exists($flag, $this->cachedFlags)) {
86 1
            $flagModel = $this->featureFlagRepository->findByFlag($flag);
87
            if (!$flagModel instanceof FeatureFlag) {
88 1
                throw new FeatureNotFoundException('Feature Flag not found: "' . $flag . '"', 1383842028);
89 1
            }
90
91
            $this->cachedFlags[$flag] = $flagModel;
92
        }
93
94
        return $this->cachedFlags[$flag];
95
    }
96
}
97