Completed
Push — develop ( 4ff741...64bd10 )
by Tom
14:53
created

Client::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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