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

DeleteClassifierRequest::getData()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 8
rs 9.4285
cc 1
eloc 4
nc 1
nop 0
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