Completed
Pull Request — master (#1)
by Jean-Baptiste
03:08
created

GuzzleFactory::supportsGuzzle6()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
rs 10
cc 2
eloc 2
nc 2
nop 0
1
<?php
2
3
namespace CanalTP\AbstractGuzzle;
4
5
use CanalTP\AbstractGuzzle\Exception\UnsupportedException;
6
7
class GuzzleFactory
8
{
9
    /**
10
     * @param string $baseUri
11
     *
12
     * @return Guzzle
13
     *
14
     * @throws NotSupportedException when Guzzle vendor version is not supported.
15
     */
16
    public static function createGuzzle($baseUri)
17
    {
18
        $guzzleVersion = self::detectGuzzleVersion();
19
20
        switch ($guzzleVersion) {
21
            case 6:
22
                return new Version\Guzzle6($baseUri);
23
24
            case 5:
25
                return new Version\Guzzle5($baseUri);
26
27
            case 3:
28
                return new Version\Guzzle3($baseUri);
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
        if (self::supportsGuzzle6()) {
40
            return 6;
41
        }
42
43
        if (self::supportsGuzzle5()) {
44
            return 5;
45
        }
46
47
        if (self::supportsGuzzle3()) {
48
            return 3;
49
        }
50
51
        throw new UnsupportedException();
52
    }
53
54
    /**
55
     * @return bool
56
     */
57
    private static function supportsGuzzle6()
58
    {
59
        return class_exists('GuzzleHttp\\Client') && !trait_exists('GuzzleHttp\\HasDataTrait');
60
    }
61
62
    /**
63
     * @return bool
64
     */
65
    private static function supportsGuzzle5()
66
    {
67
        return class_exists('GuzzleHttp\\Client') && trait_exists('GuzzleHttp\\HasDataTrait');
68
    }
69
70
    /**
71
     * @return bool
72
     */
73
    private static function supportsGuzzle3()
74
    {
75
        return class_exists('Guzzle\\Service\\Client');
76
    }
77
}
78