GoutteFactory::isGuzzle6()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 3
nc 2
nop 0
1
<?php
2
3
/*
4
 * This file is part of the Behat MinkExtension.
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\MinkExtension\ServiceContainer\Driver;
12
13
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
14
use Symfony\Component\DependencyInjection\Definition;
15
16
/**
17
 * @author Christophe Coevoet <[email protected]>
18
 */
19
class GoutteFactory implements DriverFactory
20
{
21
    /**
22
     * {@inheritdoc}
23
     */
24
    public function getDriverName()
25
    {
26
        return 'goutte';
27
    }
28
29
    /**
30
     * {@inheritdoc}
31
     */
32
    public function supportsJavascript()
33
    {
34
        return false;
35
    }
36
37
    /**
38
     * {@inheritdoc}
39
     */
40
    public function configure(ArrayNodeDefinition $builder)
41
    {
42
        $builder
43
            ->children()
44
                ->arrayNode('server_parameters')
45
                    ->useAttributeAsKey('key')
46
                    ->prototype('variable')->end()
47
                ->end()
48
                ->arrayNode('guzzle_parameters')
49
                    ->useAttributeAsKey('key')
50
                    ->prototype('variable')->end()
51
                    ->info(
52
                        "For Goutte 1.x, these are the second argument of the Guzzle3 client constructor.\n".
53
                        'For Goutte 2.x, these are the elements passed in the "defaults" key of the Guzzle4 config.'
54
                    )
55
                ->end()
56
            ->end()
57
        ;
58
    }
59
60
    /**
61
     * {@inheritdoc}
62
     */
63
    public function buildDriver(array $config)
64
    {
65
        if (!class_exists('Behat\Mink\Driver\GoutteDriver')) {
66
            throw new \RuntimeException(
67
                'Install MinkGoutteDriver in order to use goutte driver.'
68
            );
69
        }
70
71
        if ($this->isGoutte1()) {
72
            $guzzleClient = $this->buildGuzzle3Client($config['guzzle_parameters']);
73
        } elseif ($this->isGuzzle6()) {
74
            $guzzleClient = $this->buildGuzzle6Client($config['guzzle_parameters']);
75
        } else {
76
            $guzzleClient = $this->buildGuzzle4Client($config['guzzle_parameters']);
77
        }
78
79
        $clientDefinition = new Definition('Behat\Mink\Driver\Goutte\Client', array(
80
            $config['server_parameters'],
81
        ));
82
        $clientDefinition->addMethodCall('setClient', array($guzzleClient));
83
84
        return new Definition('Behat\Mink\Driver\GoutteDriver', array(
85
            $clientDefinition,
86
        ));
87
    }
88
89 View Code Duplication
    private function buildGuzzle6Client(array $parameters)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
90
    {
91
        // Force the parameters set by default in Goutte to reproduce its behavior
92
        $parameters['allow_redirects'] = false;
93
        $parameters['cookies'] = true;
94
95
        return new Definition('GuzzleHttp\Client', array($parameters));
96
    }
97
98 View Code Duplication
    private function buildGuzzle4Client(array $parameters)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
99
    {
100
        // Force the parameters set by default in Goutte to reproduce its behavior
101
        $parameters['allow_redirects'] = false;
102
        $parameters['cookies'] = true;
103
104
        return new Definition('GuzzleHttp\Client', array(array('defaults' => $parameters)));
105
    }
106
107
    private function buildGuzzle3Client(array $parameters)
108
    {
109
        // Force the parameters set by default in Goutte to reproduce its behavior
110
        $parameters['redirect.disable'] = true;
111
112
        return new Definition('Guzzle\Http\Client', array(null, $parameters));
113
    }
114
115
    private function isGoutte1()
116
    {
117
        $refl = new \ReflectionParameter(array('Goutte\Client', 'setClient'), 0);
118
119
        if ($refl->getClass() && 'Guzzle\Http\ClientInterface' === $refl->getClass()->getName()) {
120
            return true;
121
        }
122
123
        return false;
124
    }
125
126
    private function isGuzzle6()
127
    {
128
        return interface_exists('GuzzleHttp\ClientInterface') &&
129
            version_compare(\GuzzleHttp\ClientInterface::VERSION, '6.0.0', '>=');
130
    }
131
}
132