Completed
Push — develop ( bdc471...890a38 )
by Tom
02:35
created

DeleteClassifierRequest   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getData() 0 8 1
B sendData() 0 24 4
1
<?php
2
3
namespace Bobbyshaw\WatsonVisualRecognition\Message;
4
5
use Bobbyshaw\WatsonVisualRecognition\Exceptions\AuthException;
6
use GuzzleHttp\Exception\ClientException;
7
use GuzzleHttp\Psr7\MultipartStream;
8
use GuzzleHttp\Psr7\Request;
9
use InvalidArgumentException;
10
11
/**
12
 * Class DeleteClassifierRequest
13
 * @package Bobbyshaw\WatsonVisualRecognition\Message
14
 */
15
class DeleteClassifierRequest extends AbstractRequest
16
{
17
    const CLASSIFIERS_PATH = 'classifiers/';
18
19
20
    public function getData()
21
    {
22
        $data = parent::getData();
23
24
        $data['classifier_id'] = $this->getClassifierId();
25
26
        return $data;
27
    }
28
29
    /**
30
     * Send HTTP request to create a new classifier
31
     *
32
     * @param array $data
33
     * @return ClassifierResponse
34
     * @throws AuthException
35
     * @throws \Exception
36
     */
37
    public function sendData($data)
38
    {
39
        $query = ['version' => $data['version']];
40
41
        $request = new Request(
42
            'DELETE',
43
            $this->getApiUrl(static::CLASSIFIERS_PATH) . $data['classifier_id'] . '?' . http_build_query($query),
44
            ['Authorization' => 'Basic ' . base64_encode($data['username'] . ':' . $data['password'])]
45
        );
46
47
        try {
48
            $response = $this->httpClient->send($request);
49
50
            if ($response->getStatusCode() != 200) {
51
                throw new \Exception($response->getBody()->getContents(), $response->getStatusCode());
52
            }
53
        } catch (ClientException $e) {
54
            if ($e->getCode() == 401) {
55
                throw new AuthException('Invalid credentials provided');
56
            }
57
        }
58
59
        return $this->response = new Response($this, $response->getBody());
60
    }
61
}
62