1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Abstract Service |
4
|
|
|
*/ |
5
|
|
|
namespace IBM\Watson\Common; |
6
|
|
|
|
7
|
|
|
use GuzzleHttp\ClientInterface as GuzzleClientInterface; |
8
|
|
|
use GuzzleHttp\Client as HttpClient; |
9
|
|
|
use Symfony\Component\HttpFoundation\Request as HttpRequest; |
10
|
|
|
use Symfony\Component\HttpFoundation\ParameterBag; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Base Watson service class |
14
|
|
|
* |
15
|
|
|
* @see ServiceInterface |
16
|
|
|
*/ |
17
|
|
|
abstract class AbstractService implements ServiceInterface |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var \Symfony\Component\HttpFoundation\ParameterBag |
21
|
|
|
*/ |
22
|
|
|
protected $parameters; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var \GuzzleHttp\ClientInterface |
26
|
|
|
*/ |
27
|
|
|
protected $httpClient; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var \Symfony\Component\HttpFoundation\Request |
31
|
|
|
*/ |
32
|
|
|
protected $httpRequest; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Create a new service instance |
36
|
|
|
* |
37
|
|
|
* @param GuzzleClientInterface $httpClient A Guzzle client to make API calls with |
38
|
|
|
* @param HttpRequest $httpRequest A Symfony HTTP request object |
39
|
|
|
*/ |
40
|
6 |
|
public function __construct(GuzzleClientInterface $httpClient = null, HttpRequest $httpRequest = null) |
41
|
|
|
{ |
42
|
6 |
|
$this->httpClient = $httpClient ?: $this->getDefaultHttpClient(); |
43
|
6 |
|
$this->httpRequest = $httpRequest ?: $this->getDefaultHttpRequest(); |
44
|
6 |
|
$this->initialize(); |
45
|
6 |
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Initialize the service with default parameters |
49
|
|
|
* |
50
|
|
|
* @param array $parameters |
51
|
|
|
* @return $this |
52
|
|
|
*/ |
53
|
18 |
|
public function initialize(array $parameters = []) |
54
|
|
|
{ |
55
|
18 |
|
$this->parameters = new ParameterBag; |
56
|
|
|
|
57
|
18 |
|
foreach ($this->getDefaultParameters() as $key => $value) { |
58
|
18 |
|
if (is_array($value)) { |
59
|
3 |
|
$this->parameters->set($key, reset($value)); |
60
|
2 |
|
} else { |
61
|
18 |
|
$this->parameters->set($key, $value); |
62
|
|
|
} |
63
|
12 |
|
} |
64
|
|
|
|
65
|
18 |
|
Helper::initialize($this, $parameters); |
66
|
|
|
|
67
|
18 |
|
return $this; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @return array |
72
|
|
|
*/ |
73
|
18 |
|
public function getDefaultParameters() |
74
|
|
|
{ |
75
|
|
|
return [ |
76
|
18 |
|
'username' => null, |
77
|
12 |
|
'password' => null, |
78
|
12 |
|
]; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @return array |
83
|
|
|
*/ |
84
|
12 |
|
public function getParameters() |
85
|
|
|
{ |
86
|
12 |
|
return $this->parameters->all(); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @param string $key |
91
|
|
|
* @return mixed |
92
|
|
|
*/ |
93
|
6 |
|
public function getParameter($key) |
94
|
|
|
{ |
95
|
6 |
|
return $this->parameters->get($key); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @param string $key |
100
|
|
|
* @param mixed $value |
101
|
|
|
* @return $this |
102
|
|
|
*/ |
103
|
6 |
|
public function setParameter($key, $value) |
104
|
|
|
{ |
105
|
6 |
|
$this->parameters->set($key, $value); |
106
|
|
|
|
107
|
6 |
|
return $this; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @param string $value |
112
|
|
|
* @return $this |
113
|
|
|
*/ |
114
|
3 |
|
public function setUsername($value) |
115
|
|
|
{ |
116
|
3 |
|
return $this->setParameter('username', $value); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @return string |
121
|
|
|
*/ |
122
|
3 |
|
public function getUsername() |
123
|
|
|
{ |
124
|
3 |
|
return $this->getParameter('username'); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* @param string $value |
129
|
|
|
* @return $this |
130
|
|
|
*/ |
131
|
3 |
|
public function setPassword($value) |
132
|
|
|
{ |
133
|
3 |
|
return $this->setParameter('password', $value); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* @return string |
138
|
|
|
*/ |
139
|
3 |
|
public function getPassword() |
140
|
|
|
{ |
141
|
3 |
|
return $this->getParameter('password'); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* @see \IBM\Watson\Common\Message\AbstractRequest |
146
|
|
|
* @param string $class The request class name |
147
|
|
|
* @param array $parameters |
148
|
|
|
* @return \IBM\Watson\Common\Message\AbstractRequest |
149
|
|
|
*/ |
150
|
3 |
|
protected function createRequest($class, array $parameters) |
151
|
|
|
{ |
152
|
3 |
|
$obj = new $class($this->httpClient, $this->httpRequest); |
153
|
|
|
|
154
|
3 |
|
return $obj->initialize(array_replace($this->getParameters(), $parameters)); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* Get the global default HTTP client |
159
|
|
|
* |
160
|
|
|
* @return HttpClient |
161
|
|
|
*/ |
162
|
6 |
|
protected function getDefaultHttpClient() |
163
|
|
|
{ |
164
|
6 |
|
return new HttpClient( |
165
|
|
|
[ |
166
|
6 |
|
'curl.options' => [CURLOPT_CONNECTTIMEOUT => 60], |
167
|
|
|
] |
168
|
4 |
|
); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* Get the global default HTTP request |
173
|
|
|
* |
174
|
|
|
* @return HttpRequest |
175
|
|
|
*/ |
176
|
6 |
|
protected function getDefaultHttpRequest() |
177
|
|
|
{ |
178
|
6 |
|
return HttpRequest::createFromGlobals(); |
179
|
|
|
} |
180
|
|
|
} |
181
|
|
|
|