GuzzleFactory   A
last analyzed

Complexity

Total Complexity 17

Size/Duplication

Total Lines 94
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 0
Metric Value
wmc 17
lcom 1
cbo 7
dl 0
loc 94
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A createClient() 0 15 4
A createClientMock() 0 18 4
A detectGuzzleVersion() 0 16 4
A supportsGuzzle6() 0 4 2
A supportsGuzzle5() 0 4 2
A supportsGuzzle3() 0 4 1
1
<?php
2
3
namespace CanalTP\AbstractGuzzle;
4
5
use CanalTP\AbstractGuzzle\Exception\UnsupportedException;
6
use GuzzleHttp\Psr7\Response;
7
8
class GuzzleFactory
9
{
10
    /**
11
     * @param string $baseUri
12
     * @param array $options
13
     * @return Version\Guzzle3|Version\Guzzle5|Version\Guzzle6
14
     */
15
    public static function createClient($baseUri, $options = [])
16
    {
17
        $guzzleVersion = self::detectGuzzleVersion();
18
19
        switch ($guzzleVersion) {
20
            case 6:
21
                return new Version\Guzzle6($baseUri, $options);
22
23
            case 5:
24
                return new Version\Guzzle5($baseUri, $options);
25
26
            case 3:
27
                return new Version\Guzzle3($baseUri, $options);
28
        }
29
    }
30
31
    /**
32
     * get a mock of right client
33
     *
34
     * @param Response[] $mockedResponseCollection
35
     * @return Guzzle
36
     */
37
    public static function createClientMock(array $mockedResponseCollection)
38
    {
39
        $guzzleVersion = self::detectGuzzleVersion();
40
41
        switch ($guzzleVersion) {
42
            case 6:
43
                $mock = new Mock\Guzzle6Mock();
44
                return $mock->getMock($mockedResponseCollection);
45
46
            case 5:
47
                $mock = new Mock\Guzzle5Mock();
48
                return $mock->getMock($mockedResponseCollection);
0 ignored issues
show
Documentation introduced by
$mockedResponseCollection is of type array<integer,object<GuzzleHttp\Psr7\Response>>, but the function expects a array<integer,object<Guz...Http\Message\Response>>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
49
50
            case 3:
51
                $mock = new Mock\Guzzle3Mock();
52
                return $mock->getMock($mockedResponseCollection);
53
        }
54
    }
55
56
    /**
57
     * @return int current Guzzle vendor version.
58
     *
59
     * @throws NotSupportedException when Guzzle vendor version is not supported.
60
     */
61
    public static function detectGuzzleVersion()
62
    {
63
        if (self::supportsGuzzle6()) {
64
            return 6;
65
        }
66
67
        if (self::supportsGuzzle5()) {
68
            return 5;
69
        }
70
71
        if (self::supportsGuzzle3()) {
72
            return 3;
73
        }
74
75
        throw new UnsupportedException();
76
    }
77
78
    /**
79
     * @return bool
80
     */
81
    private static function supportsGuzzle6()
82
    {
83
        return class_exists('GuzzleHttp\\Client') && !trait_exists('GuzzleHttp\\HasDataTrait');
84
    }
85
86
    /**
87
     * @return bool
88
     */
89
    private static function supportsGuzzle5()
90
    {
91
        return class_exists('GuzzleHttp\\Client') && trait_exists('GuzzleHttp\\HasDataTrait');
92
    }
93
94
    /**
95
     * @return bool
96
     */
97
    private static function supportsGuzzle3()
98
    {
99
        return class_exists('Guzzle\\Service\\Client');
100
    }
101
}
102