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

GetClassifiersRequest::sendData()   B

Complexity

Conditions 4
Paths 10

Size

Total Lines 27
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

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