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

GetClassifierRequest   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 4
c 1
b 0
f 1
lcom 1
cbo 7
dl 0
loc 44
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getData() 0 8 1
A sendData() 0 23 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
    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