Completed
Pull Request — master (#1)
by Jean-Baptiste
05:12
created

GuzzleFactory::supportsGuzzle3()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace CanalTP\AbstractGuzzle;
4
5
use CanalTP\AbstractGuzzle\Exception\UnsupportedException;
6
7
class GuzzleFactory
8
{
9
    /**
10
     * @param array $config
11
     *
12
     * @return Guzzle
13
     *
14
     * @throws NotSupportedException when Guzzle vendor version is not supported.
15
     */
16
    public static function createGuzzle($config)
17
    {
18
        $guzzleVersion = self::detectGuzzleVersion();
19
20
        switch ($guzzleVersion) {
21
            case 6:
22
                return new Version\Guzzle6($config);
23
                
24
            case 5:
25
                return new Version\Guzzle5($config);
26
27
            case 3:
28
                return new Version\Guzzle3($config);
29
        }
30
    }
31
32
    /**
33
     * @return int current Guzzle vendor version.
34
     *
35
     * @throws NotSupportedException when Guzzle vendor version is not supported.
36
     */
37
    public static function detectGuzzleVersion()
38
    {
39
        // this namespace only exist since version 5.x+
40
        if (class_exists('GuzzleHttp\\Client')) {
41
42
            // default request options are defined differently in version 5 and 6
43
            $clientReflexion = new \ReflectionClass('GuzzleHttp\\Client');
44
            if ($clientReflexion->hasProperty('config')) {
45
                return 6;
46
            }
47
48
            if ($clientReflexion->hasProperty('defaults')) {
49
                return 5;
50
            }
51
        }
52
53
        if (class_exists('Guzzle\\Service\\Client')) {
54
            return 3;
55
        }
56
57
        throw new UnsupportedException();
58
    }
59
}
60