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
|
|
|
* |
13
|
|
|
* @return Guzzle |
14
|
|
|
* |
15
|
|
|
* @throws NotSupportedException when Guzzle vendor version is not supported. |
16
|
|
|
*/ |
17
|
|
|
public static function createClient($baseUri, $options = []) |
18
|
|
|
{ |
19
|
|
|
$guzzleVersion = self::detectGuzzleVersion(); |
20
|
|
|
|
21
|
|
|
switch ($guzzleVersion) { |
22
|
|
|
case 6: |
23
|
|
|
return new Version\Guzzle6($baseUri, $options); |
24
|
|
|
|
25
|
|
|
case 5: |
26
|
|
|
return new Version\Guzzle5($baseUri); |
27
|
|
|
|
28
|
|
|
case 3: |
29
|
|
|
return new Version\Guzzle3($baseUri); |
30
|
|
|
} |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* get a mock of right client |
35
|
|
|
* |
36
|
|
|
* @param Response[] $mockedResponseCollection |
37
|
|
|
* @return Guzzle |
38
|
|
|
* @throws \Exception |
39
|
|
|
*/ |
40
|
|
|
public static function createClientMock(array $mockedResponseCollection) |
41
|
|
|
{ |
42
|
|
|
$guzzleVersion = self::detectGuzzleVersion(); |
43
|
|
|
|
44
|
|
|
switch ($guzzleVersion) { |
45
|
|
|
case 6: |
46
|
|
|
$mock = new Mock\Guzzle6Mock(); |
47
|
|
|
return $mock->getMock($mockedResponseCollection); |
48
|
|
|
|
49
|
|
|
case 5: |
50
|
|
|
$mock = new Mock\Guzzle5Mock(); |
51
|
|
|
return $mock->getMock($mockedResponseCollection); |
|
|
|
|
52
|
|
|
|
53
|
|
|
case 3: |
54
|
|
|
$mock = new Mock\Guzzle3Mock(); |
55
|
|
|
return $mock->getMock($mockedResponseCollection); |
56
|
|
|
|
57
|
|
|
default: |
58
|
|
|
throw new \Exception('no guzzle version match'); |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @return int current Guzzle vendor version. |
64
|
|
|
* |
65
|
|
|
* @throws NotSupportedException when Guzzle vendor version is not supported. |
66
|
|
|
*/ |
67
|
|
|
public static function detectGuzzleVersion() |
68
|
|
|
{ |
69
|
|
|
if (self::supportsGuzzle6()) { |
70
|
|
|
return 6; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
if (self::supportsGuzzle5()) { |
74
|
|
|
return 5; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
if (self::supportsGuzzle3()) { |
78
|
|
|
return 3; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
throw new UnsupportedException(); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @return bool |
86
|
|
|
*/ |
87
|
|
|
private static function supportsGuzzle6() |
88
|
|
|
{ |
89
|
|
|
return class_exists('GuzzleHttp\\Client') && !trait_exists('GuzzleHttp\\HasDataTrait'); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @return bool |
94
|
|
|
*/ |
95
|
|
|
private static function supportsGuzzle5() |
96
|
|
|
{ |
97
|
|
|
return class_exists('GuzzleHttp\\Client') && trait_exists('GuzzleHttp\\HasDataTrait'); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @return bool |
102
|
|
|
*/ |
103
|
|
|
private static function supportsGuzzle3() |
104
|
|
|
{ |
105
|
|
|
return class_exists('Guzzle\\Service\\Client'); |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|
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: