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

AbstractRequest   A

Complexity

Total Complexity 24

Size/Duplication

Total Lines 286
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 24
c 1
b 0
f 0
lcom 1
cbo 2
dl 0
loc 286
rs 10

21 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getEndpoint() 0 4 1
A getApiUrl() 0 4 1
A initialize() 0 12 2
A getParameters() 0 4 1
A getParameter() 0 4 1
A setParameter() 0 10 2
A getUsername() 0 4 1
A setUsername() 0 6 1
A getPassword() 0 4 1
A setPassword() 0 6 1
A getVersion() 0 4 1
A getImagesFile() 0 4 1
A setImagesFile() 0 6 1
A getClassifierIds() 0 4 1
A setClassifierIds() 0 6 1
A setVersion() 0 6 1
A configure() 0 3 1
A getData() 0 8 1
A send() 0 8 1
A getResponse() 0 8 2
1
<?php
2
3
namespace Bobbyshaw\WatsonVisualRecognition\Message;
4
5
use Symfony\Component\HttpFoundation\ParameterBag;
6
use GuzzleHttp\ClientInterface;
7
use Bobbyshaw\WatsonVisualRecognition\Helper;
8
9
/**
10
 * Class AbstractRequest
11
 *
12
 * This abstract class implements RequestInterface and defines a basic
13
 * set of functions that all Watson Requests are intended to include.
14
 *
15
 * Requests of this class are usually created using the createRequest
16
 * function and then actioned using methods within this
17
 * class or a class that extends this class.
18
 *
19
 * @package Bobbyshaw\WatsonVisualRecognition\Message
20
 */
21
22
abstract class AbstractRequest implements RequestInterface
23
{
24
    const ENDPOINT = 'https://gateway.watsonplatform.net/visual-recognition-beta/api/v2/';
25
26
    /**
27
     * The request parameters
28
     *
29
     * @var \Symfony\Component\HttpFoundation\ParameterBag
30
     */
31
    protected $parameters;
32
33
    /**
34
     * The request client.
35
     *
36
     * @var \GuzzleHttp\ClientInterface
37
     */
38
    protected $httpClient;
39
40
41
    /**
42
     * An associated ResponseInterface.
43
     *
44
     * @var ResponseInterface
45
     */
46
    protected $response;
47
48
    /**
49
     * Create a new Request
50
     *
51
     * @param ClientInterface $httpClient  A Guzzle client to make API calls with
52
     */
53
    public function __construct(ClientInterface $httpClient)
54
    {
55
        $this->httpClient = $httpClient;
56
        $this->initialize();
57
    }
58
59
    /**
60
     * Get the watson API endpoint
61
     *
62
     * @api
63
     * @return string
64
     */
65
    public function getEndpoint()
66
    {
67
        return static::ENDPOINT;
68
    }
69
70
    /**
71
     * Get full API URL
72
     *
73
     * @param String $path of API needed
74
     * @returns String of full URL
75
     */
76
    public function getApiUrl($path)
77
    {
78
        return $this->getEndpoint() . $path;
79
    }
80
81
    /**
82
     * Initialize the object with parameters.
83
     *
84
     * @param array $parameters An associative array of parameters
85
     *
86
     * @return $this
87
     * @throws \RuntimeException
88
     */
89
    public function initialize(array $parameters = array())
90
    {
91
        if (null !== $this->response) {
92
            throw new \RuntimeException('Request cannot be modified after it has been sent!');
93
        }
94
95
        $this->parameters = new ParameterBag;
96
97
        Helper::initialize($this, $parameters);
98
99
        return $this;
100
    }
101
102
    /**
103
     * Get all parameters as an associative array.
104
     *
105
     * @return array
106
     */
107
    public function getParameters()
108
    {
109
        return $this->parameters->all();
110
    }
111
112
    /**
113
     * Get a single parameter.
114
     *
115
     * @param string $key The parameter key
116
     * @return mixed
117
     */
118
    protected function getParameter($key)
119
    {
120
        return $this->parameters->get($key);
121
    }
122
123
    /**
124
     * Set a single parameter
125
     *
126
     * @param string $key The parameter key
127
     * @param mixed $value The value to set
128
     * @return AbstractRequest Provides a fluent interface
129
     * @throws \RuntimeException if a request parameter is modified after the request has been sent.
130
     */
131
    protected function setParameter($key, $value)
132
    {
133
        if (null !== $this->response) {
134
            throw new \RuntimeException('Request cannot be modified after it has been sent!');
135
        }
136
137
        $this->parameters->set($key, $value);
138
139
        return $this;
140
    }
141
142
    /**
143
     * Get Username
144
     *
145
     * @return mixed
146
     */
147
    public function getUsername()
148
    {
149
        return $this->getParameter('username');
150
    }
151
152
    /**
153
     * Set Username
154
     *
155
     * @param $value
156
     * @return $this
157
     */
158
    public function setUsername($value)
159
    {
160
        $this->setParameter('username', $value);
161
162
        return $this;
163
    }
164
165
    /**
166
     * Get password
167
     *
168
     * @return mixed
169
     */
170
    public function getPassword()
171
    {
172
        return $this->getParameter('password');
173
    }
174
175
    /**
176
     * Set Password
177
     *
178
     * @param $value
179
     * @return $this
180
     */
181
    public function setPassword($value)
182
    {
183
        $this->setParameter('password', $value);
184
185
        return $this;
186
    }
187
188
    /**
189
     * Get Version
190
     *
191
     * @return mixed
192
     */
193
    public function getVersion()
194
    {
195
        return $this->getParameter('version');
196
    }
197
198
    /**
199
     * Get image file.
200
     *
201
     * @return mixed
202
     */
203
    public function getImagesFile()
204
    {
205
        return $this->getParameter('images_file');
206
    }
207
208
    /**
209
     * Set Image file
210
     *
211
     * @param $value
212
     * @return AbstractRequest
213
     */
214
    public function setImagesFile($value)
215
    {
216
        $this->setParameter('images_file', $value);
217
218
        return $this;
219
    }
220
221
    /**
222
     * Get Classifier Ids
223
     *
224
     * @return mixed
225
     */
226
    public function getClassifierIds()
227
    {
228
        return $this->getParameter('classifier_ids');
229
    }
230
231
    /**
232
     * Set Classifier Ids
233
     *
234
     * @param $value
235
     * @return $this
236
     */
237
    public function setClassifierIds($value)
238
    {
239
        $this->setParameter('classifier_ids', $value);
240
241
        return $this;
242
    }
243
244
    /**
245
     * Set version
246
     *
247
     * @param $value
248
     * @return $this
249
     */
250
    public function setVersion($value)
251
    {
252
        $this->setParameter('version', $value);
253
254
        return $this;
255
    }
256
257
258
    /**
259
     * Configure command is run before every request is sent
260
     */
261
    public function configure()
262
    {
263
    }
264
265
    /**
266
     * Get Default request data params
267
     *
268
     * @return array
269
     */
270
    public function getData()
271
    {
272
        return [
273
            'username' => $this->getUsername(),
274
            'password' => $this->getPassword(),
275
            'version'  => $this->getVersion()
276
        ];
277
    }
278
279
280
    /**
281
     * Send the request
282
     *
283
     * @return ResponseInterface
284
     */
285
    public function send()
286
    {
287
        $this->configure();
288
289
        $data = $this->getData();
290
291
        return $this->sendData($data);
292
    }
293
294
    /**
295
     * Get the associated Response.
296
     *
297
     * @return ResponseInterface
298
     */
299
    public function getResponse()
300
    {
301
        if (null === $this->response) {
302
            throw new \RuntimeException('You must call send() before accessing the Response!');
303
        }
304
305
        return $this->response;
306
    }
307
}
308