ToggleConfig   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 137
Duplicated Lines 0 %

Test Coverage

Coverage 96%

Importance

Changes 2
Bugs 0 Features 1
Metric Value
eloc 58
dl 0
loc 137
ccs 48
cts 50
cp 0.96
rs 10
c 2
b 0
f 1
wmc 14

10 Methods

Rating   Name   Duplication   Size   Complexity  
A strategyTypes() 0 3 1
A driver() 0 3 1
A toggles() 0 3 1
A __construct() 0 40 4
A segmentTypes() 0 3 1
A apiPrefix() 0 3 1
A apiEnabled() 0 3 1
A assertDriverOptions() 0 6 2
A assertDriver() 0 6 1
A driverOptions() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Pheature\Crud\Psr11\Toggle;
6
7
use Webmozart\Assert\Assert;
8
9
/**
10
 * @psalm-import-type WriteFeature from \Pheature\Core\Toggle\Write\Feature
11
 * @psalm-import-type InMemoryFeature from \Pheature\InMemory\Toggle\InMemoryConfig
12
 * @psalm-type StrategyTypeFactoryConfig array<array{type: string, factory_id: string}>
13
 * @psalm-type SegmentTypeFactoryConfig array<array{type: string, factory_id: string}>
14
 * @psalm-type ToggleConfigOptions array{
15
 *   api_enabled: bool,
16
 *   api_prefix: string,
17
 *   driver: string,
18
 *   driver_options?: array<string>,
19
 *   strategy_types?: StrategyTypeFactoryConfig,
20
 *   segment_types?: SegmentTypeFactoryConfig,
21
 *   toggles?: array<InMemoryFeature>
22
 * }
23
 */
24
final class ToggleConfig
25
{
26
    public const DRIVER_IN_MEMORY = 'inmemory';
27
    public const DRIVER_DBAL = 'dbal';
28
    public const DRIVER_CHAIN = 'chain';
29
    private const VALID_DRIVERS = [
30
        self::DRIVER_IN_MEMORY,
31
        self::DRIVER_DBAL,
32
        self::DRIVER_CHAIN,
33
    ];
34
    private const VALID_DRIVER_OPTIONS = [
35
        self::DRIVER_IN_MEMORY,
36
        self::DRIVER_DBAL,
37
    ];
38
39
    private string $driver;
40
    /** @var array<string> */
41
    private $driverOptions;
42
    private bool $apiEnabled;
43
    private string $apiPrefix;
44
    /** @var StrategyTypeFactoryConfig */
0 ignored issues
show
Bug introduced by
The type Pheature\Crud\Psr11\Togg...rategyTypeFactoryConfig was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
45
    private array $strategyTypes;
46
    /** @var SegmentTypeFactoryConfig */
0 ignored issues
show
Bug introduced by
The type Pheature\Crud\Psr11\Togg...egmentTypeFactoryConfig was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
47
    private array $segmentTypes;
48
    /** @var array<InMemoryFeature> */
49
    private array $toggles;
50
51
    /**
52
     * @param array<string, mixed> $config
53
     */
54 37
    public function __construct(array $config)
55
    {
56 37
        $this->assertDriver($config);
57 27
        Assert::keyExists($config, 'api_enabled');
58 26
        Assert::boolean($config['api_enabled']);
59 25
        Assert::keyExists($config, 'api_prefix');
60 24
        Assert::string($config['api_prefix']);
61
62 23
        $this->apiEnabled = $config['api_enabled'];
63 23
        $this->apiPrefix = $config['api_prefix'];
64
        /** @var string $driver */
65 23
        $driver = $config['driver'];
66 23
        $this->driver = $driver;
67
        /** @var array<string> $driverOptions */
68 23
        $driverOptions = $config['driver_options'] ?? [];
69 23
        $this->driverOptions = $driverOptions;
70
71 23
        $this->strategyTypes = [];
0 ignored issues
show
Documentation Bug introduced by
It seems like array() of type array is incompatible with the declared type Pheature\Crud\Psr11\Togg...rategyTypeFactoryConfig of property $strategyTypes.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
72 23
        $this->segmentTypes = [];
0 ignored issues
show
Documentation Bug introduced by
It seems like array() of type array is incompatible with the declared type Pheature\Crud\Psr11\Togg...egmentTypeFactoryConfig of property $segmentTypes.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
73 23
        $this->toggles = [];
74
75 23
        if (array_key_exists('strategy_types', $config)) {
76 11
            Assert::isArray($config['strategy_types']);
77
            /** @var array<array{type: string, factory_id: string}> $strategyTypes */
78 10
            $strategyTypes = $config['strategy_types'];
79 10
            $this->strategyTypes = $strategyTypes;
80
        }
81
82 22
        if (array_key_exists('segment_types', $config)) {
83 10
            Assert::isArray($config['segment_types']);
84
            /** @var array<array{type: string, factory_id: string}> $segmentTypes */
85 9
            $segmentTypes = $config['segment_types'];
86 9
            $this->segmentTypes = $segmentTypes;
87
        }
88
89 21
        if (array_key_exists('toggles', $config)) {
90 12
            Assert::isArray($config['toggles']);
91
            /** @var array<InMemoryFeature> $toggles */
92 11
            $toggles = $config['toggles'];
93 11
            $this->toggles = $toggles;
94
        }
95
    }
96
97
    /**
98
     * @param array<string, mixed> $config
99
     * @return void
100
     */
101 37
    private function assertDriver(array $config): void
102
    {
103 37
        Assert::keyExists($config, 'driver');
104 35
        Assert::inArray($config['driver'], self::VALID_DRIVERS);
105
106 31
        $this->assertDriverOptions($config);
107
    }
108
109
    /**
110
     * @param array<string, mixed> $config
111
     * @return void
112
     */
113 31
    private function assertDriverOptions(array $config): void
114
    {
115 31
        if (self::DRIVER_CHAIN === $config['driver']) {
116 10
            Assert::keyExists($config, 'driver_options');
117 9
            Assert::notEmpty($config['driver_options']);
118 7
            Assert::allInArray($config['driver_options'], self::VALID_DRIVER_OPTIONS);
119
        }
120
    }
121
122 3
    public function apiEnabled(): bool
123
    {
124 3
        return $this->apiEnabled;
125
    }
126
127 3
    public function apiPrefix(): string
128
    {
129 3
        return $this->apiPrefix;
130
    }
131
132 17
    public function driver(): string
133
    {
134 17
        return $this->driver;
135
    }
136
137
    /**
138
     * @return array<string>
139
     */
140 9
    public function driverOptions(): array
141
    {
142 9
        return $this->driverOptions;
143
    }
144
145
    /** @return StrategyTypeFactoryConfig */
146 1
    public function strategyTypes(): array
147
    {
148 1
        return $this->strategyTypes;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->strategyTypes returns the type array which is incompatible with the documented return type Pheature\Crud\Psr11\Togg...rategyTypeFactoryConfig.
Loading history...
149
    }
150
151
    /** @return SegmentTypeFactoryConfig */
152
    public function segmentTypes(): array
153
    {
154
        return $this->segmentTypes;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->segmentTypes returns the type array which is incompatible with the documented return type Pheature\Crud\Psr11\Togg...egmentTypeFactoryConfig.
Loading history...
155
    }
156
157
    /** @return array<InMemoryFeature> */
158 5
    public function toggles(): array
159
    {
160 5
        return $this->toggles;
161
    }
162
}
163