|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @author @ct-jensschulze <[email protected]> |
|
4
|
|
|
* @created: 22.01.15, 13:51 |
|
5
|
|
|
*/ |
|
6
|
|
|
|
|
7
|
|
|
namespace Commercetools\Core; |
|
8
|
|
|
|
|
9
|
|
|
use Commercetools\Core\Client\Adapter\AdapterFactory; |
|
10
|
|
|
use Commercetools\Core\Client\Adapter\AdapterInterface; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* @package Commercetools\Core |
|
14
|
|
|
*/ |
|
15
|
|
|
abstract class AbstractHttpClient |
|
16
|
|
|
{ |
|
17
|
|
|
const VERSION = '1.0.0-RC8'; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @var AdapterInterface |
|
21
|
|
|
*/ |
|
22
|
|
|
protected $httpClient; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @var AdapterFactory |
|
26
|
|
|
*/ |
|
27
|
|
|
protected $adapterFactory; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @var Config |
|
31
|
|
|
*/ |
|
32
|
|
|
protected $config; |
|
33
|
|
|
|
|
34
|
|
|
protected $userAgent; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @param Config|array $config |
|
38
|
|
|
*/ |
|
39
|
25 |
|
public function __construct($config) |
|
40
|
1 |
|
{ |
|
41
|
25 |
|
$this->setConfig($config); |
|
42
|
25 |
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @param Config|array $config |
|
46
|
|
|
* @return $this |
|
47
|
|
|
*/ |
|
48
|
26 |
|
public function setConfig($config) |
|
49
|
|
|
{ |
|
50
|
26 |
|
if ($config instanceof Config) { |
|
51
|
25 |
|
$this->config = $config; |
|
52
|
26 |
|
} elseif (is_array($config)) { |
|
53
|
1 |
|
$this->config = Config::fromArray($config); |
|
54
|
1 |
|
} |
|
55
|
26 |
|
$this->getConfig()->check(); |
|
56
|
|
|
|
|
57
|
26 |
|
return $this; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* @return Config |
|
62
|
|
|
*/ |
|
63
|
28 |
|
public function getConfig() |
|
64
|
|
|
{ |
|
65
|
28 |
|
if (is_null($this->config)) { |
|
66
|
2 |
|
$this->config = new Config(); |
|
67
|
2 |
|
} |
|
68
|
28 |
|
return $this->config; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* @param array $options |
|
74
|
|
|
* @return AdapterInterface |
|
75
|
|
|
*/ |
|
76
|
24 |
|
public function getHttpClient($options = []) |
|
77
|
|
|
{ |
|
78
|
24 |
|
if (is_null($this->httpClient)) { |
|
79
|
24 |
|
$headers = ['User-Agent' => $this->getUserAgent()]; |
|
80
|
24 |
|
if (!is_null($this->getConfig()->getAcceptEncoding())) { |
|
81
|
|
|
$headers['Accept-Encoding'] = $this->getConfig()->getAcceptEncoding(); |
|
82
|
|
|
} |
|
83
|
24 |
|
$options = array_merge( |
|
84
|
|
|
[ |
|
85
|
24 |
|
'base_uri' => $this->getBaseUrl(), |
|
86
|
|
|
'headers' => $headers |
|
87
|
24 |
|
], |
|
88
|
|
|
$options |
|
89
|
24 |
|
); |
|
90
|
24 |
|
$class = $this->getAdapterFactory()->getClass($this->getConfig()->getAdapter()); |
|
91
|
|
|
|
|
92
|
24 |
|
$this->httpClient = new $class($options); |
|
93
|
24 |
|
} |
|
94
|
|
|
|
|
95
|
24 |
|
return $this->httpClient; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* @return AdapterFactory |
|
100
|
|
|
*/ |
|
101
|
24 |
|
public function getAdapterFactory() |
|
102
|
|
|
{ |
|
103
|
24 |
|
if (is_null($this->adapterFactory)) { |
|
104
|
24 |
|
$this->adapterFactory = new AdapterFactory(); |
|
105
|
24 |
|
} |
|
106
|
|
|
|
|
107
|
24 |
|
return $this->adapterFactory; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
abstract protected function getBaseUrl(); |
|
111
|
|
|
|
|
112
|
24 |
|
protected function getUserAgent() |
|
113
|
|
|
{ |
|
114
|
24 |
|
if (is_null($this->userAgent)) { |
|
115
|
24 |
|
$agent = 'commercetools-php-sdk ' . static::VERSION; |
|
116
|
24 |
|
if (extension_loaded('curl')) { |
|
117
|
24 |
|
$agent .= ' curl/' . curl_version()['version']; |
|
118
|
24 |
|
} |
|
119
|
24 |
|
$agent .= ' PHP/' . PHP_VERSION; |
|
120
|
24 |
|
$this->userAgent = $agent; |
|
121
|
24 |
|
} |
|
122
|
|
|
|
|
123
|
24 |
|
return $this->userAgent; |
|
124
|
|
|
} |
|
125
|
|
|
} |
|
126
|
|
|
|