GetClassifierRequest::sendData()   A
last analyzed

Complexity

Conditions 3
Paths 5

Size

Total Lines 23
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 23
ccs 14
cts 14
cp 1
rs 9.0856
cc 3
eloc 14
nc 5
nop 1
crap 3
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 4
    public function getData()
18
    {
19 4
        $data = parent::getData();
20
21 4
        $data['classifier_id'] = $this->getClassifierId();
22
23 4
        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 3
    public function sendData($data)
34
    {
35 3
        $query = ['version' => $data['version']];
36
37 3
        $request = new Request(
38 3
            'GET',
39 3
            $this->getApiUrl(static::CLASSIFIERS_PATH) . $data['classifier_id'] . '?' . http_build_query($query),
40 3
            ['Authorization' => 'Basic ' . base64_encode($data['username'] . ':' . $data['password'])]
41 3
        );
42
43 3
        $this->response = null;
44
45
        try {
46 3
            $response = $this->httpClient->send($request);
47 2
            $this->response = new ClassifierResponse($this, $response->getBody());
48 3
        } catch (ClientException $e) {
49 1
            if ($e->getCode() == 401) {
50 1
                throw new AuthException('Invalid credentials provided');
51
            }
52
        }
53
54 2
        return $this->response;
55
    }
56
}
57