FeatureToggle   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 6
dl 0
loc 56
rs 10
c 0
b 0
f 0
ccs 13
cts 13
cp 1

3 Methods

Rating   Name   Duplication   Size   Complexity  
A hasFeature() 0 13 3
A __construct() 0 4 2
A getFeatures() 0 3 1
1
<?php
2
/* Copyright (C) 2017 Michael Giesler
3
 *
4
 * This file is part of Dembelo.
5
 *
6
 * Dembelo is free software: you can redistribute it and/or modify
7
 * it under the terms of the GNU Affero General Public License as published by
8
 * the Free Software Foundation, either version 3 of the License, or
9
 * (at your option) any later version.
10
 *
11
 * Dembelo is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
 * GNU Affero General Public License 3 for more details.
15
 *
16
 * You should have received a copy of the GNU Affero General Public License 3
17
 * along with Dembelo. If not, see <http://www.gnu.org/licenses/>.
18
 */
19
20
namespace DembeloMain\Model;
21
22
/**
23
 * Class FeatureToggle
24
 */
25
class FeatureToggle
26
{
27
    /**
28
     * @var bool[]
29
     */
30
    private $features = [
31
        'test_feature'  => false, // for unittesting, don't remove
32
        'login_enabled' => false, // registration and login enabled in navigation?
33
        'login_needed'  => false, //
34
        'paywall'       => true,
35
    ];
36
37
    /**
38
     * @var bool[]
39
     */
40
    private $featureParameters = [];
41
42
    /**
43
     * FeatureToggle constructor.
44
     * @param array $featureParameters
45
     */
46 10
    public function __construct(array $featureParameters)
47
    {
48 10
        foreach ($featureParameters as $featureParameter) {
49 2
            $this->featureParameters += $featureParameter;
50
        }
51 10
    }
52
53
    /**
54
     * checks availability of a feature
55
     * @param string $featureKey
56
     *
57
     * @return bool
58
     */
59 4
    public function hasFeature(string $featureKey): bool
60
    {
61 4
        if (!isset($this->features[$featureKey])) {
62 1
            return false;
63
        }
64
65 3
        $defaultValue = $this->features[$featureKey];
66
67 3
        if (!isset($this->featureParameters[$featureKey])) {
68 1
            return $defaultValue;
69
        }
70
71 2
        return $this->featureParameters[$featureKey];
72
    }
73
74
    /**
75
     * returns an array of existing features
76
     * @return string[]
77
     */
78 3
    public function getFeatures(): array
79
    {
80 3
        return array_keys($this->features);
81
    }
82
}
83