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

Client::createClassifier()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace Bobbyshaw\WatsonVisualRecognition;
4
5
use Bobbyshaw\WatsonVisualRecognition\Message\ClassifyRequest;
6
use Bobbyshaw\WatsonVisualRecognition\Message\CreateClassifierRequest;
7
use Bobbyshaw\WatsonVisualRecognition\Message\GetClassifierRequest;
8
use Bobbyshaw\WatsonVisualRecognition\Message\GetClassifiersRequest;
9
use Bobbyshaw\WatsonVisualRecognition\Message\RequestInterface;
10
use GuzzleHttp\ClientInterface as HttpClientInterface;
11
use GuzzleHttp\Client as HttpClient;
12
use Symfony\Component\HttpFoundation\ParameterBag;
13
14
/**
15
 * This is the primary library class which handles request to the IBM Watson Visual Recognition Service
16
 *
17
 * @package Bobbyshaw\WatsonVisualRecognition
18
 * @author Tom Robertshaw <[email protected]>
19
 * @api
20
 */
21
class Client implements ClientInterface
22
{
23
24
    /**
25
     * Request parameters
26
     *
27
     * @var \Symfony\Component\HttpFoundation\ParameterBag
28
     */
29
    protected $parameters;
30
31
    /**
32
     * HTTP Client
33
     * @var \GuzzleHttp\ClientInterface
34
     */
35
    protected $httpClient;
36
37
    /**
38
     * Instantiate Visual Recognition client with API credentials
39
     *
40
     * @api
41
     * @param HttpClientInterface|null $httpClient
42
     * @throws \Exception
43
     */
44
    public function __construct(HttpClientInterface $httpClient = null)
45
    {
46
        $this->httpClient = $httpClient ?: $this->getDefaultHttpClient();
47
    }
48
49
    /**
50
     * Get the global default HTTP client.
51
     *
52
     * @return HttpClient
53
     */
54
    protected function getDefaultHttpClient()
55
    {
56
        return new HttpClient(
57
            array(
58
                'curl.options' => array(CURLOPT_CONNECTTIMEOUT => 60),
59
            )
60
        );
61
    }
62
63
    /**
64
     * Initialize this client with default parameters
65
     *
66
     * @param  array $parameters
67
     * @return $this
68
     */
69
    public function initialize(array $parameters = array())
70
    {
71
        $this->parameters = new ParameterBag;
72
73
        // set default parameters
74
        foreach ($this->getDefaultParameters() as $key => $value) {
75
            $this->parameters->set($key, $value);
76
        }
77
78
        Helper::initialize($this, $parameters);
79
80
        return $this;
81
    }
82
83
    /**
84
     * Create new request object and initialize.
85
     *
86
     * @param $class
87
     * @param array $parameters
88
     * @return mixed
89
     */
90
    protected function createRequest($class, array $parameters)
91
    {
92
        /** @var RequestInterface $obj */
93
        $obj = new $class($this->httpClient);
94
95
        return $obj->initialize(array_replace($this->getParameters(), $parameters));
96
    }
97
98
    /**
99
     * Default & required parameters for requests
100
     *
101
     */
102
    public function getDefaultParameters()
103
    {
104
        return array(
105
            'username' => '',
106
            'password' => '',
107
            'version' => '2015-12-02'
108
        );
109
    }
110
111
    /**
112
     * Get Parameters
113
     *
114
     * @return array
115
     */
116
    public function getParameters()
117
    {
118
        return $this->parameters->all();
119
    }
120
121
    /**
122
     * Get Username
123
     *
124
     * @return mixed
125
     */
126
    public function getUsername()
127
    {
128
        return $this->parameters->get('username');
129
    }
130
131
    /**
132
     * Set Username
133
     *
134
     * @param $value
135
     * @return $this
136
     */
137
    public function setUsername($value)
138
    {
139
        $this->parameters->set('username', $value);
140
141
        return $this;
142
    }
143
144
    /**
145
     * Get password
146
     *
147
     * @return mixed
148
     */
149
    public function getPassword()
150
    {
151
        return $this->parameters->get('password');
152
    }
153
154
    /**
155
     * Set Password
156
     *
157
     * @param $value
158
     * @return $this
159
     */
160
    public function setPassword($value)
161
    {
162
        $this->parameters->set('password', $value);
163
164
        return $this;
165
    }
166
167
    /**
168
     * Get Version
169
     *
170
     * @return mixed
171
     */
172
    public function getVersion()
173
    {
174
        return $this->parameters->get('version');
175
    }
176
177
    /**
178
     * Set version
179
     *
180
     * @param $value
181
     * @return $this
182
     */
183
    public function setVersion($value)
184
    {
185
        $this->parameters->set('version', $value);
186
187
        return $this;
188
    }
189
190
    /**
191
     * Get list of available classifiers
192
     *
193
     * @api
194
     * @param array $parameters
195
     * @return GetClassifiersRequest
196
     * @throws \Exception
197
     */
198
    public function getClassifiers(array $parameters = [])
199
    {
200
        return $this->createRequest(GetClassifiersRequest::class, $parameters);
201
    }
202
203
    /**
204
     * Get detail on individual classifier
205
     *
206
     * @param array $parameters
207
     * @return mixed
208
     */
209
    public function getClassifier(array $parameters = [])
210
    {
211
        return $this->createRequest(GetClassifierRequest::class, $parameters);
212
    }
213
214
    /**
215
     * Classify image
216
     *
217
     * @param array $parameters
218
     * @return ClassifyRequest
219
     */
220
    public function classify(array $parameters = [])
221
    {
222
        return $this->createRequest(ClassifyRequest::class, $parameters);
223
    }
224
225
    /**
226
     * Train a new classifier
227
     *
228
     * @param array $parameters
229
     * @return mixed
230
     */
231
    public function createClassifier(array $parameters = [])
232
    {
233
        return $this->createRequest(CreateClassifierRequest::class, $parameters);
234
    }
235
}
236