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

FeatureFlagService::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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