Extension::resolveConsumerVersion()   A
last analyzed

Complexity

Conditions 3
Paths 5

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 0
cts 6
cp 0
rs 9.9
c 0
b 0
f 0
cc 3
nc 5
nop 1
crap 12
1
<?php
2
3
namespace SmartGamma\Behat\PactExtension;
4
5
use Behat\Testwork\ServiceContainer\Extension as ExtensionInterface;
6
use Behat\Testwork\ServiceContainer\ExtensionManager;
7
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
8
use Symfony\Component\Config\FileLocator;
9
use Symfony\Component\DependencyInjection\ContainerBuilder;
10
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
11
12
class Extension implements ExtensionInterface
13
{
14
    const PARAMETER_NAME_PACT_PROVIDERS = 'pact.providers.config';
15
16
    const PARAMETER_NAME_PACT_COMMON_CONFIG = 'pact.common.config';
17
18
    /**
19
     * {@inheritdoc}
20
     */
21
    public function configure(ArrayNodeDefinition $builder)
22
    {
23
        $builder
24
            ->children()
25
            ->arrayNode('common')
26
            ->useAttributeAsKey('key')
27
            ->prototype('variable')->end()
28
            ->end()
29
            ->arrayNode('providers')
30
            ->prototype('variable')->end()
31
            ->end();
32
    }
33
34
    /**
35
     * {@inheritdoc}
36
     */
37
    public function load(ContainerBuilder $container, array $config)
38
    {
39
        $this->resolveConsumerVersion($config);
40
        $container->setParameter(self::PARAMETER_NAME_PACT_PROVIDERS, $this->normalizeProvidersConfig($config['providers']));
41
        $container->setParameter(self::PARAMETER_NAME_PACT_COMMON_CONFIG, $config['common']);
42
43
        $loader = new YamlFileLoader($container, new FileLocator(__DIR__ . '/ServiceContainer/config'));
44
        $loader->load('services.yml');
45
    }
46
47
    /**
48
     * @param array $config
49
     */
50
    private function resolveConsumerVersion(array &$config)
51
    {
52
        try {
53
            $reflex = new \ReflectionClassConstant('\App\Kernel', 'PACT_CONSUMER_VERSION');
54
            $config['common']['PACT_CONSUMER_VERSION'] = $reflex->getValue();
55
        } catch (\ReflectionException $e) {
56
            if (false === isset($config['common']['PACT_CONSUMER_VERSION'])) {
57
                new \Exception('You should define PACT_CONSUMER_VERSION');
58
            }
59
        }
60
    }
61
62
    /**
63
     * @param array $originalConfig
64
     *
65
     * @return array
66
     */
67
    private function normalizeProvidersConfig(array $originalConfig): array
68
    {
69
        $config = [];
70
71
        foreach ($originalConfig as $one) {
72
            foreach ($one as $key => $val) {
73
                $config[$key]                          = [];
74
                $config[$key]['PACT_PROVIDER_NAME']    = $key;
75
                $parts                                 = explode(':', $val);
76
                $config[$key]['PACT_MOCK_SERVER_HOST'] = $parts[0];
77
                $config[$key]['PACT_MOCK_SERVER_PORT'] = $parts[1];
78
            }
79
        }
80
81
        return $config;
82
    }
83
84
    /**
85
     * {@inheritdoc}
86
     */
87
    public function getConfigKey()
88
    {
89
        return 'pact';
90
    }
91
92
    /**
93
     * {@inheritdoc}
94
     */
95
    public function initialize(ExtensionManager $extensionManager)
96
    {
97
    }
98
99
    /**
100
     * {@inheritdoc}
101
     */
102
    public function process(ContainerBuilder $container)
103
    {
104
    }
105
}
106