1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace CanalTP\AbstractGuzzle; |
4
|
|
|
|
5
|
|
|
use CanalTP\AbstractGuzzle\Exception\UnsupportedException; |
6
|
|
|
use CanalTP\AbstractGuzzle\Tests\Mock\Guzzle5Mock; |
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
|
|
View Code Duplication |
public static function createGuzzle($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
|
|
|
* @return int current Guzzle vendor version. |
35
|
|
|
* |
36
|
|
|
* @throws NotSupportedException when Guzzle vendor version is not supported. |
37
|
|
|
*/ |
38
|
|
|
public static function detectGuzzleVersion() |
39
|
|
|
{ |
40
|
|
|
if (self::supportsGuzzle6()) { |
41
|
|
|
return 6; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
if (self::supportsGuzzle5()) { |
45
|
|
|
return 5; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
if (self::supportsGuzzle3()) { |
49
|
|
|
return 3; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
throw new UnsupportedException(); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @return bool |
57
|
|
|
*/ |
58
|
|
|
private static function supportsGuzzle6() |
59
|
|
|
{ |
60
|
|
|
return class_exists('GuzzleHttp\\Client') && !trait_exists('GuzzleHttp\\HasDataTrait'); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @return bool |
65
|
|
|
*/ |
66
|
|
|
private static function supportsGuzzle5() |
67
|
|
|
{ |
68
|
|
|
return class_exists('GuzzleHttp\\Client') && trait_exists('GuzzleHttp\\HasDataTrait'); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @return bool |
73
|
|
|
*/ |
74
|
|
|
private static function supportsGuzzle3() |
75
|
|
|
{ |
76
|
|
|
return class_exists('Guzzle\\Service\\Client'); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
View Code Duplication |
public static function createMockedClient(array $mockedResponseCollection) |
|
|
|
|
80
|
|
|
{ |
81
|
|
|
$guzzleVersion = self::detectGuzzleVersion(); |
82
|
|
|
|
83
|
|
|
switch ($guzzleVersion) { |
84
|
|
|
case 6: |
85
|
|
|
return new Version\Guzzle6($baseUri); |
|
|
|
|
86
|
|
|
|
87
|
|
|
case 5: |
88
|
|
|
return new Guzzle5Mock($mockedResponseCollection); |
89
|
|
|
|
90
|
|
|
case 3: |
91
|
|
|
return new Version\Guzzle3($baseUri); |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.