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

GetClassifierRequest::getData()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 8
rs 9.4285
cc 1
eloc 4
nc 1
nop 0
1
<?php
2
3
namespace Bobbyshaw\WatsonVisualRecognition\Message;
4
5
use Bobbyshaw\WatsonVisualRecognition\Exceptions\AuthException;
6
use GuzzleHttp\Exception\ClientException;
7
use GuzzleHttp\Psr7\Request;
8
9
/**
10
 * Class GetClassifierRequest
11
 * @package Bobbyshaw\WatsonVisualRecognition\Message
12
 */
13
class GetClassifierRequest extends AbstractRequest
14
{
15
    const CLASSIFIERS_PATH = 'classifiers/';
16
17
    public function getData()
18
    {
19
        $data = parent::getData();
20
21
        $data['classifier_id'] = $this->getClassifierId();
22
23
        return $data;
24
    }
25
26
    /**
27
     * Send HTTP request to get list of classifiers
28
     *
29
     * @param array $data
30
     * @return ClassifiersResponse
31
     * @throws AuthException
32
     */
33
    public function sendData($data)
34
    {
35
        $query = ['version' => $data['version']];
36
37
        $request = new Request(
38
            'GET',
39
            $this->getApiUrl(static::CLASSIFIERS_PATH) . $data['classifier_id'] . '?' . http_build_query($query),
40
            ['Authorization' => 'Basic ' . base64_encode($data['username'] . ':' . $data['password'])]
41
        );
42
43
        $this->response = null;
44
45
        try {
46
            $response = $this->httpClient->send($request);
47
            $this->response = new ClassifierResponse($this, $response->getBody());
48
        } catch (ClientException $e) {
49
            if ($e->getCode() == 401) {
50
                throw new AuthException('Invalid credentials provided');
51
            }
52
        }
53
54
        return $this->response;
55
    }
56
}
57