Passed
Push — 1.0.x ( 4c6986...086a36 )
by Koldo
10:37
created

ToggleConfig::apiEnabled()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Pheature\Crud\Psr11\Toggle;
6
7
use Webmozart\Assert\Assert;
8
9
final class ToggleConfig
10
{
11
    public const DRIVER_IN_MEMORY = 'inmemory';
12
    public const DRIVER_DBAL = 'dbal';
13
    private const VALID_DRIVERS = [
14
        self::DRIVER_IN_MEMORY,
15
        self::DRIVER_DBAL,
16
    ];
17
18
    private string $driver;
19
    private bool $apiEnabled;
20
    private string $apiPrefix;
21
    /** @var array<array<string, string>> */
22
    private array $strategyTypes;
23
    /** @var array<string, mixed> */
24
    private array $toggles;
25
26
    /**
27
     * @param array<string, mixed> $config
28
     */
29 10
    public function __construct(array $config)
30
    {
31 10
        $this->assertDriver($config);
32 8
        Assert::keyExists($config, 'api_enabled');
33 8
        Assert::boolean($config['api_enabled']);
34 8
        Assert::keyExists($config, 'api_prefix');
35 8
        Assert::string($config['api_prefix']);
36
37 8
        $this->apiEnabled = $config['api_enabled'];
38 8
        $this->apiPrefix = $config['api_prefix'];
39 8
        $this->driver = (string) $config['driver'];
40 8
        $this->strategyTypes = [];
41 8
        $this->toggles = [];
42
43 8
        if (array_key_exists('strategy_types', $config)) {
44
            Assert::isArray($config['strategy_types']);
45
            /** @var array<array<string, string>> $strategyTypes */
46
            $strategyTypes = $config['strategy_types'];
47
            $this->strategyTypes = $strategyTypes;
48
        }
49
50 8
        if (array_key_exists('toggles', $config)) {
51
            Assert::isArray($config['toggles']);
52
            /** @var array<string, mixed> $toggles */
53
            $toggles = $config['toggles'];
54
            $this->toggles = $toggles;
55
        }
56 8
    }
57
58
    /**
59
     * @param array<string, mixed> $config
60
     * @return void
61
     */
62 10
    private function assertDriver(array $config): void
63
    {
64 10
        Assert::keyExists($config, 'driver');
65 10
        Assert::string($config['driver']);
66 10
        Assert::inArray($config['driver'], self::VALID_DRIVERS);
67 8
    }
68
69
    public function apiEnabled(): bool
70
    {
71
        return $this->apiEnabled;
72
    }
73
74
    public function apiPrefix(): string
75
    {
76
        return $this->apiPrefix;
77
    }
78
79 8
    public function driver(): string
80
    {
81 8
        return $this->driver;
82
    }
83
84
    /**
85
     * @return array<array<string, string>>
86
     */
87
    public function strategyTypes(): array
88
    {
89
        return $this->strategyTypes;
90
    }
91
92
    /**
93
     * @return array<string, mixed>
94
     */
95 2
    public function toggles(): array
96
    {
97 2
        return $this->toggles;
98
    }
99
}
100