WebApiExtension   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 78
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 9
c 2
b 0
f 0
lcom 0
cbo 4
dl 0
loc 78
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getConfigKey() 0 4 1
A initialize() 0 3 1
A process() 0 3 1
A configure() 0 11 1
A load() 0 5 1
A loadClient() 0 17 3
A loadContextInitializer() 0 9 1
1
<?php
2
3
/*
4
 * This file is part of the Behat WebApiExtension.
5
 * (c) Konstantin Kudryashov <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace Behat\WebApiExtension\ServiceContainer;
12
13
use Behat\Behat\Context\ServiceContainer\ContextExtension;
14
use Behat\Testwork\ServiceContainer\Extension as ExtensionInterface;
15
use Behat\Testwork\ServiceContainer\ExtensionManager;
16
use GuzzleHttp\ClientInterface;
17
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
18
use Symfony\Component\DependencyInjection\ContainerBuilder;
19
use Symfony\Component\DependencyInjection\Definition;
20
use Symfony\Component\DependencyInjection\Reference;
21
22
/**
23
 * Web API extension for Behat.
24
 *
25
 * @author Konstantin Kudryashov <[email protected]>
26
 */
27
class WebApiExtension implements ExtensionInterface
28
{
29
    const CLIENT_ID = 'web_api.client';
30
31
    /**
32
     * {@inheritdoc}
33
     */
34
    public function getConfigKey()
35
    {
36
        return 'web_api';
37
    }
38
39
    /**
40
     * {@inheritdoc}
41
     */
42
    public function initialize(ExtensionManager $extensionManager)
43
    {
44
    }
45
46
    /**
47
     * {@inheritdoc}
48
     */
49
    public function configure(ArrayNodeDefinition $builder)
50
    {
51
        $builder
52
            ->addDefaultsIfNotSet()
53
            ->children()
54
                ->scalarNode('base_url')
55
                    ->defaultValue('http://localhost')
56
                    ->end()
57
                ->end()
58
            ->end();
59
    }
60
61
    /**
62
     * {@inheritdoc}
63
     */
64
    public function load(ContainerBuilder $container, array $config)
65
    {
66
        $this->loadClient($container, $config);
67
        $this->loadContextInitializer($container, $config);
68
    }
69
70
    private function loadClient(ContainerBuilder $container, $config)
71
    {
72
        // Guzzle 6 BC bridge
73
        if (version_compare(ClientInterface::VERSION, '6.0', '>=')) {
74
            $config['base_uri'] = $config['base_url'];
75
            unset($config['bar_url']);
76
77
            if (isset($config['defaults'])) {
78
                $defaults = $config['defaults'];
79
                unset($config['defaults']);
80
                $config = array_merge($config, $defaults);
81
            }
82
        }
83
84
        $definition = new Definition('GuzzleHttp\Client', array($config));
85
        $container->setDefinition(self::CLIENT_ID, $definition);
86
    }
87
88
    private function loadContextInitializer(ContainerBuilder $container, $config)
89
    {
90
        $definition = new Definition('Behat\WebApiExtension\Context\Initializer\ApiClientAwareInitializer', array(
91
          new Reference(self::CLIENT_ID),
92
          $config
93
        ));
94
        $definition->addTag(ContextExtension::INITIALIZER_TAG);
95
        $container->setDefinition('web_api.context_initializer', $definition);
96
    }
97
98
    /**
99
     * {@inheritdoc}
100
     */
101
    public function process(ContainerBuilder $container)
102
    {
103
    }
104
}
105