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

GuzzleFactory   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A createGuzzle() 0 15 4
B detectGuzzleVersion() 0 22 5
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