Completed
Push — develop ( 68eef9...892aaa )
by Tom
04:09
created

DeleteClassifierRequest::sendData()   B

Complexity

Conditions 4
Paths 7

Size

Total Lines 24
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 24
ccs 13
cts 13
cp 1
rs 8.6845
cc 4
eloc 14
nc 7
nop 1
crap 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 5
    public function getData()
21
    {
22 5
        $data = parent::getData();
23
24 5
        $data['classifier_id'] = $this->getClassifierId();
25
26 5
        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 4
    public function sendData($data)
38
    {
39 4
        $query = ['version' => $data['version']];
40
41 4
        $request = new Request(
42 4
            'DELETE',
43 4
            $this->getApiUrl(static::CLASSIFIERS_PATH) . $data['classifier_id'] . '?' . http_build_query($query),
44 4
            ['Authorization' => 'Basic ' . base64_encode($data['username'] . ':' . $data['password'])]
45
        );
46
47
        try {
48 4
            $response = $this->httpClient->send($request);
49
50 3
            if ($response->getStatusCode() != 200) {
51 3
                throw new \Exception($response->getBody()->getContents(), $response->getStatusCode());
52
            }
53 1
        } catch (ClientException $e) {
54 1
            if ($e->getCode() == 401) {
55 1
                throw new AuthException('Invalid credentials provided');
56
            }
57
        }
58
59 3
        return $this->response = new Response($this, $response->getBody());
60
    }
61
}
62