AbstractRequest::setNegativeExamples()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
crap 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 38
    public function __construct(ClientInterface $httpClient)
53 1
    {
54 38
        $this->httpClient = $httpClient;
55 38
        $this->initialize();
56 38
    }
57
58
    /**
59
     * Get the watson API endpoint
60
     *
61
     * @api
62
     * @return string
63
     */
64 21
    public function getEndpoint()
65
    {
66 21
        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 21
    public function getApiUrl($path)
76
    {
77 21
        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 38
    public function initialize(array $parameters = array())
89
    {
90 38
        if (null !== $this->response) {
91 1
            throw new \RuntimeException('Request cannot be modified after it has been sent!');
92
        }
93
94 38
        $this->parameters = new ParameterBag;
95
96 38
        Helper::initialize($this, $parameters);
97
98 38
        return $this;
99
    }
100
101
    /**
102
     * Get all parameters as an associative array.
103
     *
104
     * @return array
105
     */
106 1
    public function getParameters()
107
    {
108 1
        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 28
    protected function getParameter($key)
118
    {
119 28
        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 32
    protected function setParameter($key, $value)
131
    {
132 32
        if (null !== $this->response) {
133 1
            throw new \RuntimeException('Request cannot be modified after it has been sent!');
134
        }
135
136 32
        $this->parameters->set($key, $value);
137
138 32
        return $this;
139
    }
140
141
    /**
142
     * Get Username
143
     *
144
     * @return string
145
     */
146 28
    public function getUsername()
147
    {
148 28
        return $this->getParameter('username');
149
    }
150
151
    /**
152
     * Set Username
153
     *
154
     * @param string $value
155
     * @return $this
156
     */
157 27
    public function setUsername($value)
158
    {
159 27
        $this->setParameter('username', $value);
160
161 27
        return $this;
162
    }
163
164
    /**
165
     * Get password
166
     *
167
     * @return string
168
     */
169 28
    public function getPassword()
170
    {
171 28
        return $this->getParameter('password');
172
    }
173
174
    /**
175
     * Set Password
176
     *
177
     * @param string $value
178
     * @return $this
179
     */
180 25
    public function setPassword($value)
181
    {
182 25
        $this->setParameter('password', $value);
183
184 25
        return $this;
185
    }
186
187
    /**
188
     * Get Version
189
     *
190
     * @return string
191
     */
192 28
    public function getVersion()
193
    {
194 28
        return $this->getParameter('version');
195
    }
196
197
    /**
198
     * Set version
199
     *
200
     * @param $value
201
     * @return $this
202
     */
203 25
    public function setVersion($value)
204
    {
205 25
        $this->setParameter('version', $value);
206
207 25
        return $this;
208
    }
209
210
    /**
211
     * Set verbose parameter
212
     *
213
     * @return string
214
     */
215 8
    public function getVerbose()
216
    {
217 8
        return $this->getParameter('verbose');
218
    }
219
220
    /**
221
     * @param string $value
222
     * @return $this
223
     */
224 2
    public function setVerbose($value)
225
    {
226 2
        $this->setParameter('verbose', $value);
227
228 2
        return $this;
229
    }
230
231
    /**
232
     * Get image file.
233
     *
234
     * @return string
235
     */
236 6
    public function getImagesFile()
237
    {
238 6
        return $this->getParameter('images_file');
239
    }
240
241
    /**
242
     * Set Image file
243
     *
244
     * @param string $value
245
     * @return $this
246
     */
247 6
    public function setImagesFile($value)
248
    {
249 6
        $this->setParameter('images_file', $value);
250
251 6
        return $this;
252
    }
253
254
    /**
255
     * Get Classifier Ids
256
     *
257
     * @return string[]
258
     */
259 6
    public function getClassifierIds()
260
    {
261 6
        return $this->getParameter('classifier_ids');
262
    }
263
264
    /**
265
     * Set Classifier Ids
266
     *
267
     * @param string[] $value
268
     * @return $this
269
     */
270 6
    public function setClassifierIds($value)
271
    {
272 6
        $this->setParameter('classifier_ids', $value);
273
274 6
        return $this;
275
    }
276
277
    /**
278
     * Get Classifier ID
279
     *
280
     * @return String
281
     */
282 9
    public function getClassifierId()
283
    {
284 9
        return $this->getParameter('classifier_id');
285
    }
286
287
    /**
288
     * Set Classifier ID
289
     *
290
     * @param String $value
291
     * @return $this
292
     */
293 9
    public function setClassifierId($value)
294
    {
295 9
        $this->setParameter('classifier_id', $value);
296
297 9
        return $this;
298
    }
299
300
    /**
301
     * Get positive examples zip file path
302
     *
303
     * @return string
304
     */
305 5
    public function getPositiveExamples()
306
    {
307 5
        return $this->getParameter('positive_examples');
308
    }
309
310
    /**
311
     * Set file path of positive examples
312
     *
313
     * @param string $value
314
     * @return $this
315
     */
316 5
    public function setPositiveExamples($value)
317
    {
318 5
        $this->setParameter('positive_examples', $value);
319
320 5
        return $this;
321
    }
322
323
    /**
324
     * Get negative examples zip file path
325
     *
326
     * @return string
327
     */
328 5
    public function getNegativeExamples()
329
    {
330 5
        return $this->getParameter('negative_examples');
331
    }
332
333
    /**
334
     * Set file path of negative examples
335
     *
336
     * @param string $value
337
     * @return $this
338
     */
339 5
    public function setNegativeExamples($value)
340
    {
341 5
        $this->setParameter('negative_examples', $value);
342
343 5
        return $this;
344
    }
345
346
    /**
347
     * Get classifier name
348
     *
349
     * @return string
350
     */
351 5
    public function getName()
352
    {
353 5
        return $this->getParameter('name');
354
    }
355
356
    /**
357
     * Set classifier name
358
     *
359
     * @param string $value
360
     * @return $this
361
     */
362 5
    public function setName($value)
363
    {
364 5
        $this->setParameter('name', $value);
365
366 5
        return $this;
367
    }
368
369
    /**
370
     * Get Default request data params
371
     *
372
     * @return array
373
     */
374 28
    public function getData()
375
    {
376
        return [
377 28
            'username' => $this->getUsername(),
378 28
            'password' => $this->getPassword(),
379 28
            'version' => $this->getVersion()
380 28
        ];
381
    }
382
383
384
    /**
385
     * Send the request
386
     *
387
     * @return ResponseInterface
388
     */
389 23
    public function send()
390
    {
391 23
        $data = $this->getData();
392
393 23
        return $this->sendData($data);
394
    }
395
396
    /**
397
     * Get the associated Response.
398
     *
399
     * @return ResponseInterface
400
     */
401 2
    public function getResponse()
402
    {
403 2
        if (null === $this->response) {
404 1
            throw new \RuntimeException('You must call send() before accessing the Response!');
405
        }
406
407 1
        return $this->response;
408
    }
409
}
410