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); |
|
|
|
|
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
|
|
|
|
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: