GuzzleExtension   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 100
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 5

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 7
lcom 0
cbo 5
dl 0
loc 100
ccs 44
cts 44
cp 1
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 9 1
A getConfigKey() 0 4 1
A initialize() 0 3 1
A load() 0 10 1
A process() 0 3 1
A loadClient() 0 13 1
A loadContextInitializer() 0 16 1
1
<?php
2
/**
3
 * Behat Guzzle Extension
4
 *
5
 * PHP version 5
6
 *
7
 * @package Behat\GuzzleExtension
8
 * @author  Dave Nash <[email protected]>
9
 * @license http://opensource.org/licenses/MIT The MIT License
10
 * @version GIT: $Id$
11
 * @link    https://github.com/teaandcode/behat-guzzle-extension GuzzleExtension
12
 */
13
14
namespace Behat\GuzzleExtension\ServiceContainer;
15
16
use Behat\Behat\Context\ServiceContainer\ContextExtension;
17
use Behat\Testwork\ServiceContainer\Extension as ExtensionInterface;
18
use Behat\Testwork\ServiceContainer\ExtensionManager;
19
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
20
use Symfony\Component\DependencyInjection\ContainerBuilder;
21
use Symfony\Component\DependencyInjection\Definition;
22
use Symfony\Component\DependencyInjection\Reference;
23
24
/**
25
 * Guzzle extension for Behat class
26
 *
27
 * @package Behat\GuzzleExtension\ServiceContainer
28
 * @author  Dave Nash <[email protected]>
29
 * @license http://opensource.org/licenses/MIT The MIT License
30
 * @version Release: @package_version@
31
 * @link    https://github.com/teaandcode/behat-guzzle-extension GuzzleExtension
32
 */
33
class GuzzleExtension implements ExtensionInterface
34
{
35
    const GUZZLE_CLIENT_ID = 'guzzle.client';
36
37
    /**
38
     * {@inheritDoc}
39
     */
40 1
    public function configure(ArrayNodeDefinition $builder)
41
    {
42 1
        $builder->addDefaultsIfNotSet()
43 1
            ->children()
44 1
                ->scalarNode('base_url')->defaultNull()->end()
45 1
                ->scalarNode('service_descriptions')->defaultNull()->end()
46 1
            ->end()
47 1
        ->end();
48 1
    }
49
50
    /**
51
     * {@inheritDoc}
52
     */
53 1
    public function getConfigKey()
54
    {
55 1
        return 'guzzle';
56
    }
57
58
    /**
59
     * {@inheritdoc}
60
     */
61 1
    public function initialize(ExtensionManager $extensionManager)
62
    {
63 1
    }
64
65
    /**
66
     * {@inheritDoc}
67
     */
68 1
    public function load(ContainerBuilder $container, array $config)
69
    {
70 1
        $baseUrl = rtrim($config['base_url'], '/');
71
72 1
        $container->setParameter('guzzle.base_url', $baseUrl);
73 1
        $container->setParameter('guzzle.parameters', $config);
74
75 1
        $this->loadClient($container);
76 1
        $this->loadContextInitializer($container);
77 1
    }
78
79
    /**
80
     * {@inheritDoc}
81
     */
82 1
    public function process(ContainerBuilder $container)
83
    {
84 1
    }
85
86
    /**
87
     * Load Client
88
     *
89
     * @param ContainerBuilder $container DI Container
90
     *
91
     * @access private
92
     * @return void
93
     */
94 1
    private function loadClient(ContainerBuilder $container)
95
    {
96 1
        $container->setDefinition(
97 1
            self::GUZZLE_CLIENT_ID,
98 1
            new Definition(
99 1
                'Guzzle\Service\Client',
100
                array(
101 1
                    'baseUrl' => $container->getParameter('guzzle.base_url'),
102 1
                    'config' => $container->getParameter('guzzle.parameters')
103 1
                )
104 1
            )
105 1
        );
106 1
    }
107
108
    /**
109
     * Load Context Initializer
110
     *
111
     * @param ContainerBuilder $container DI Container
112
     *
113
     * @access private
114
     * @return void
115
     */
116 1
    private function loadContextInitializer(ContainerBuilder $container)
117
    {
118 1
        $definition = new Definition(
119 1
            'Behat\GuzzleExtension\Context\Initializer\GuzzleAwareInitializer',
120
            array(
121 1
                new Reference(self::GUZZLE_CLIENT_ID),
122 1
                '%guzzle.parameters%',
123
            )
124 1
        );
125 1
        $definition->addTag(
126 1
            ContextExtension::INITIALIZER_TAG,
127 1
            array('priority' => 0)
128 1
        );
129
130 1
        $container->setDefinition('guzzle.context_initializer', $definition);
131 1
    }
132
}
133