Completed
Push — develop ( 3bcd98...bdc471 )
by Tom
02:31
created

CreateClassifierRequest::sendData()   B

Complexity

Conditions 4
Paths 7

Size

Total Lines 44
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 44
rs 8.5806
cc 4
eloc 25
nc 7
nop 1
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 CreateClassifierRequest
13
 * @package Bobbyshaw\WatsonVisualRecognition\Message
14
 */
15
class CreateClassifierRequest extends AbstractRequest
16
{
17
    const CLASSIFIERS_PATH = 'classifiers/';
18
    const ALLOWED_FILE_TYPES = ['.zip'];
19
20
21
    public function getData()
22
    {
23
        $data = parent::getData();
24
25
        $data['positive_examples'] = $this->getPositiveExamples();
26
        $data['negative_examples'] = $this->getNegativeExamples();
27
        $data['name'] = $this->getName();
28
29
        return $data;
30
    }
31
32
    /**
33
     * Send HTTP request to create a new classifier
34
     *
35
     * @param array $data
36
     * @return ClassifierResponse
37
     * @throws AuthException
38
     * @throws \Exception
39
     */
40
    public function sendData($data)
41
    {
42
        $this->validateData($data);
43
44
        $params = [
45
            [
46
                'name' => 'positive_examples',
47
                'contents' => fopen($data['positive_examples'], 'r'),
48
                'filename' => $data['positive_examples']
49
            ],
50
            [
51
                'name' => 'negative_examples',
52
                'contents' => fopen($data['negative_examples'], 'r'),
53
                'filename' => $data['negative_examples']
54
            ],
55
            [
56
                'name' => 'name',
57
                'contents' => $data['name']
58
            ]
59
        ];
60
61
        $multipartStream = new MultipartStream($params);
62
63
        $request = new Request(
64
            'POST',
65
            $this->getApiUrl(static::CLASSIFIERS_PATH) . '?' . http_build_query(['version' => $data['version']]),
66
            ['Authorization' => 'Basic ' . base64_encode($data['username'] . ':' . $data['password'])],
67
            $multipartStream
68
        );
69
70
        try {
71
            $response = $this->httpClient->send($request);
72
73
            if ($response->getStatusCode() != 200) {
74
                throw new \Exception($response->getBody()->getContents(), $response->getStatusCode());
75
            }
76
        } catch (ClientException $e) {
77
            if ($e->getCode() == 401) {
78
                throw new AuthException('Invalid credentials provided');
79
            }
80
        }
81
82
        return $this->response = new ClassifierResponse($this, $response->getBody());
83
    }
84
85
86
    /**
87
     * Validate Data
88
     *
89
     * @param $data
90
     * @return bool
91
     * @throws InvalidArgumentException
92
     */
93
    protected function validateData($data)
94
    {
95
        if (!in_array(substr($data['positive_examples'], -4), static::ALLOWED_FILE_TYPES)
96
            || !in_array(substr($data['negative_examples'], -4), static::ALLOWED_FILE_TYPES)) {
97
            throw new InvalidArgumentException(
98
                'Example files needs to be one of the following types: ' . implode(', ', static::ALLOWED_FILE_TYPES)
99
            );
100
        }
101
102
        return true;
103
    }
104
}
105