1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace CybozuHttp; |
4
|
|
|
|
5
|
|
|
use GuzzleHttp\Client as GuzzleClient; |
6
|
|
|
use GuzzleHttp\Subscriber\Log\Formatter; |
7
|
|
|
use GuzzleHttp\Subscriber\Log\LogSubscriber; |
8
|
|
|
use CybozuHttp\Subscriber\ErrorSubscriber; |
9
|
|
|
use CybozuHttp\Exception\FailedAuthException; |
10
|
|
|
use CybozuHttp\Exception\NotExistRequiredException; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* @author ochi51<[email protected]> |
14
|
|
|
*/ |
15
|
|
|
class Client extends GuzzleClient |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @var Config |
19
|
|
|
*/ |
20
|
|
|
protected $config; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Client constructor. |
24
|
|
|
* @param array $config |
25
|
|
|
*/ |
26
|
8 |
|
public function __construct($config = []) |
27
|
|
|
{ |
28
|
8 |
|
$config = new Config($config); |
29
|
8 |
|
if (!$config->hasRequired()) { |
30
|
1 |
|
throw new NotExistRequiredException(); |
31
|
|
|
} |
32
|
|
|
|
33
|
8 |
|
parent::__construct($config->toArray()); |
34
|
|
|
|
35
|
8 |
|
$this->config = $config; |
36
|
|
|
|
37
|
8 |
|
$this->attachErrorSubscriber($config); |
38
|
8 |
|
$this->attachLogSubscriber($config); |
39
|
8 |
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @param Config $config |
43
|
|
|
*/ |
44
|
8 |
|
protected function attachErrorSubscriber(Config $config) |
45
|
|
|
{ |
46
|
8 |
|
$this->getEmitter()->attach(new ErrorSubscriber($config)); |
47
|
8 |
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @param Config $config |
51
|
|
|
*/ |
52
|
8 |
|
protected function attachLogSubscriber(Config $config) |
53
|
|
|
{ |
54
|
8 |
|
if ($config->get('debug') && $config->get('logfile')) { |
55
|
5 |
|
$logSubscriber = new LogSubscriber(fopen($config->get('logfile'), 'a'), Formatter::DEBUG); |
56
|
5 |
|
$this->getEmitter()->attach($logSubscriber); |
57
|
5 |
|
} |
58
|
8 |
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @return Config |
62
|
|
|
*/ |
63
|
3 |
|
public function getConfig() |
64
|
|
|
{ |
65
|
3 |
|
return $this->config; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @param array $config |
70
|
|
|
*/ |
71
|
2 |
|
public function changeAuthOptions(array $config) |
72
|
|
|
{ |
73
|
1 |
|
$baseConfig = $this->config->toArray(); |
74
|
1 |
|
unset($baseConfig['defaults']); |
75
|
1 |
|
$mergeConfig = $config + $baseConfig; |
76
|
|
|
|
77
|
1 |
|
$config = (new Config($mergeConfig)); |
78
|
2 |
|
$options = $config->toArray()['defaults']; |
79
|
1 |
|
foreach ($options as $key => $option) { |
80
|
1 |
|
$this->setDefaultOption($key, $option); |
81
|
1 |
|
} |
82
|
|
|
|
83
|
1 |
|
$this->attachLogSubscriber($config); |
84
|
|
|
|
85
|
1 |
|
$this->config = $config; |
86
|
1 |
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @param string $prefix |
90
|
|
|
* @throws FailedAuthException |
91
|
|
|
*/ |
92
|
1 |
|
public function connectionTest($prefix = '/') |
93
|
|
|
{ |
94
|
1 |
|
$response = $this->get($prefix); |
95
|
|
|
$url = $response->getEffectiveUrl(); |
96
|
|
|
if ($url && strpos($url, $this->getBaseUrl()) !== 0) { |
97
|
|
|
throw new FailedAuthException('Wronged auth information.'); |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|