Completed
Push — develop ( fe8c75...3bcd98 )
by Tom
02:26
created

AbstractRequest::setClassifierId()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 6
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
3
namespace Bobbyshaw\WatsonVisualRecognition\Message;
4
5
use Symfony\Component\HttpFoundation\ParameterBag;
6
use GuzzleHttp\ClientInterface;
7
use Bobbyshaw\WatsonVisualRecognition\Helper;
8
9
/**
10
 * Class AbstractRequest
11
 *
12
 * This abstract class implements RequestInterface and defines a basic
13
 * set of functions that all Watson Requests are intended to include.
14
 *
15
 * Requests of this class are usually created using the createRequest
16
 * function and then actioned using methods within this
17
 * class or a class that extends this class.
18
 *
19
 * @package Bobbyshaw\WatsonVisualRecognition\Message
20
 */
21
abstract class AbstractRequest implements RequestInterface
22
{
23
    const ENDPOINT = 'https://gateway.watsonplatform.net/visual-recognition-beta/api/v2/';
24
25
    /**
26
     * The request parameters
27
     *
28
     * @var \Symfony\Component\HttpFoundation\ParameterBag
29
     */
30
    protected $parameters;
31
32
    /**
33
     * The request client.
34
     *
35
     * @var \GuzzleHttp\ClientInterface
36
     */
37
    protected $httpClient;
38
39
40
    /**
41
     * An associated ResponseInterface.
42
     *
43
     * @var ResponseInterface
44
     */
45
    protected $response;
46
47
    /**
48
     * Create a new Request
49
     *
50
     * @param ClientInterface $httpClient A Guzzle client to make API calls with
51
     */
52
    public function __construct(ClientInterface $httpClient)
53
    {
54
        $this->httpClient = $httpClient;
55
        $this->initialize();
56
    }
57
58
    /**
59
     * Get the watson API endpoint
60
     *
61
     * @api
62
     * @return string
63
     */
64
    public function getEndpoint()
65
    {
66
        return static::ENDPOINT;
67
    }
68
69
    /**
70
     * Get full API URL
71
     *
72
     * @param String $path of API needed
73
     * @returns String of full URL
74
     */
75
    public function getApiUrl($path)
76
    {
77
        return $this->getEndpoint() . $path;
78
    }
79
80
    /**
81
     * Initialize the object with parameters.
82
     *
83
     * @param array $parameters An associative array of parameters
84
     *
85
     * @return $this
86
     * @throws \RuntimeException
87
     */
88
    public function initialize(array $parameters = array())
89
    {
90
        if (null !== $this->response) {
91
            throw new \RuntimeException('Request cannot be modified after it has been sent!');
92
        }
93
94
        $this->parameters = new ParameterBag;
95
96
        Helper::initialize($this, $parameters);
97
98
        return $this;
99
    }
100
101
    /**
102
     * Get all parameters as an associative array.
103
     *
104
     * @return array
105
     */
106
    public function getParameters()
107
    {
108
        return $this->parameters->all();
109
    }
110
111
    /**
112
     * Get a single parameter.
113
     *
114
     * @param string $key The parameter key
115
     * @return mixed
116
     */
117
    protected function getParameter($key)
118
    {
119
        return $this->parameters->get($key);
120
    }
121
122
    /**
123
     * Set a single parameter
124
     *
125
     * @param string $key The parameter key
126
     * @param mixed $value The value to set
127
     * @return AbstractRequest Provides a fluent interface
128
     * @throws \RuntimeException if a request parameter is modified after the request has been sent.
129
     */
130
    protected function setParameter($key, $value)
131
    {
132
        if (null !== $this->response) {
133
            throw new \RuntimeException('Request cannot be modified after it has been sent!');
134
        }
135
136
        $this->parameters->set($key, $value);
137
138
        return $this;
139
    }
140
141
    /**
142
     * Get Username
143
     *
144
     * @return string
145
     */
146
    public function getUsername()
147
    {
148
        return $this->getParameter('username');
149
    }
150
151
    /**
152
     * Set Username
153
     *
154
     * @param string $value
155
     * @return $this
156
     */
157
    public function setUsername($value)
158
    {
159
        $this->setParameter('username', $value);
160
161
        return $this;
162
    }
163
164
    /**
165
     * Get password
166
     *
167
     * @return string
168
     */
169
    public function getPassword()
170
    {
171
        return $this->getParameter('password');
172
    }
173
174
    /**
175
     * Set Password
176
     *
177
     * @param string $value
178
     * @return $this
179
     */
180
    public function setPassword($value)
181
    {
182
        $this->setParameter('password', $value);
183
184
        return $this;
185
    }
186
187
    /**
188
     * Get Version
189
     *
190
     * @return string
191
     */
192
    public function getVersion()
193
    {
194
        return $this->getParameter('version');
195
    }
196
197
    /**
198
     * Set verbose parameter
199
     *
200
     * @return string
201
     */
202
    public function getVerbose()
203
    {
204
        return $this->getParameter('verbose');
205
    }
206
207
    /**
208
     * @param string $value
209
     * @return $this
210
     */
211
    public function setVerbose($value)
212
    {
213
        $this->setParameter('verbose', $value);
214
215
        return $this;
216
    }
217
218
    /**
219
     * Get image file.
220
     *
221
     * @return string
222
     */
223
    public function getImagesFile()
224
    {
225
        return $this->getParameter('images_file');
226
    }
227
228
    /**
229
     * Set Image file
230
     *
231
     * @param string $value
232
     * @return $this
233
     */
234
    public function setImagesFile($value)
235
    {
236
        $this->setParameter('images_file', $value);
237
238
        return $this;
239
    }
240
241
    /**
242
     * Get Classifier Ids
243
     *
244
     * @return string[]
245
     */
246
    public function getClassifierIds()
247
    {
248
        return $this->getParameter('classifier_ids');
249
    }
250
251
    /**
252
     * Set Classifier Ids
253
     *
254
     * @param string[] $value
255
     * @return $this
256
     */
257
    public function setClassifierIds($value)
258
    {
259
        $this->setParameter('classifier_ids', $value);
260
261
        return $this;
262
    }
263
264
    /**
265
     * Get Classifier ID
266
     *
267
     * @return String
268
     */
269
    public function getClassifierId()
270
    {
271
        return $this->getParameter('classifier_id');
272
    }
273
274
    /**
275
     * Set Classifier ID
276
     *
277
     * @param String $value
278
     * @return $this
279
     */
280
    public function setClassifierId($value)
281
    {
282
        $this->setParameter('classifier_id', $value);
283
284
        return $this;
285
    }
286
287
    /**
288
     * Set version
289
     *
290
     * @param $value
291
     * @return $this
292
     */
293
    public function setVersion($value)
294
    {
295
        $this->setParameter('version', $value);
296
297
        return $this;
298
    }
299
300
301
    /**
302
     * Configure command is run before every request is sent
303
     */
304
    public function configure()
305
    {
306
    }
307
308
    /**
309
     * Get Default request data params
310
     *
311
     * @return array
312
     */
313
    public function getData()
314
    {
315
        return [
316
            'username' => $this->getUsername(),
317
            'password' => $this->getPassword(),
318
            'version' => $this->getVersion()
319
        ];
320
    }
321
322
323
    /**
324
     * Send the request
325
     *
326
     * @return ResponseInterface
327
     */
328
    public function send()
329
    {
330
        $this->configure();
331
332
        $data = $this->getData();
333
334
        return $this->sendData($data);
335
    }
336
337
    /**
338
     * Get the associated Response.
339
     *
340
     * @return ResponseInterface
341
     */
342
    public function getResponse()
343
    {
344
        if (null === $this->response) {
345
            throw new \RuntimeException('You must call send() before accessing the Response!');
346
        }
347
348
        return $this->response;
349
    }
350
}
351