MetroMarketsFFExtension::configureConfigCat()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 8
c 1
b 0
f 0
nc 3
nop 2
dl 0
loc 16
rs 10
1
<?php
2
3
namespace MetroMarkets\FFBundle\DependencyInjection;
4
5
6
use MetroMarkets\FFBundle\FeatureFlagService;
7
use MetroMarkets\FFBundle\Providers\ConfigCatProvider;
8
use MetroMarkets\FFBundle\Providers\FalseProvider;
9
use MetroMarkets\FFBundle\Providers\RandomProvider;
10
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
11
use Symfony\Component\Config\FileLocator;
12
use Symfony\Component\DependencyInjection\ContainerBuilder;
13
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
14
use Symfony\Component\DependencyInjection\Reference;
15
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
16
17
18
class MetroMarketsFFExtension extends Extension
19
{
20
    public function getAlias()
21
    {
22
        return 'metro_markets_ff';
23
    }
24
25
    public function load(array $configs, ContainerBuilder $container)
26
    {
27
        $loader = new YamlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config'));
28
        $loader->load('services.yml');
29
30
        $configuration = $this->getConfiguration($configs, $container);
31
        $config = $this->processConfiguration($configuration, $configs);
0 ignored issues
show
Bug introduced by
It seems like $configuration can also be of type null; however, parameter $configuration of Symfony\Component\Depend...:processConfiguration() does only seem to accept Symfony\Component\Config...\ConfigurationInterface, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

31
        $config = $this->processConfiguration(/** @scrutinizer ignore-type */ $configuration, $configs);
Loading history...
32
33
        $adapter = $config['provider'];
34
35
        switch ($adapter) {
36
            case 'configcat':
37
                $this->configureConfigCat($container, $config);
38
                break;
39
            case 'random':
40
                $this->configureRandom($container);
41
                break;
42
            //logger is the default
43
            default:
44
                $serviceDefinition = $container->getDefinition(FeatureFlagService::class);
45
                $serviceDefinition->setArgument(0, new Reference(FalseProvider::class));
46
47
                break;
48
        }
49
    }
50
51
    private function configureConfigCat(ContainerBuilder $container, array $config)
52
    {
53
        $adapterDefinition = $container->getDefinition(ConfigCatProvider::class);
54
55
        if (!isset($config['configcat'])) {
56
            throw new InvalidConfigurationException('Configcat config must be set when using provider:configcat');
57
        }
58
59
        if (!isset($config['configcat']['sdk_key'])) {
60
            throw new InvalidConfigurationException('Configcat sdk_key must be set');
61
        }
62
63
        $adapterDefinition->setArgument(0, $config['configcat']['sdk_key']);
64
65
        $serviceDefinition = $container->getDefinition(FeatureFlagService::class);
66
        $serviceDefinition->setArgument(0, new Reference(ConfigCatProvider::class));
67
    }
68
69
    private function configureRandom(ContainerBuilder $container)
70
    {
71
        $serviceDefinition = $container->getDefinition(FeatureFlagService::class);
72
        $serviceDefinition->setArgument(0, new Reference(RandomProvider::class));
73
    }
74
}