Completed
Push — develop ( b68cde...86f0dd )
by Adam
57s queued 52s
created

AbstractRequest   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 175
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 14
lcom 1
cbo 3
dl 0
loc 175
ccs 35
cts 35
cp 1
rs 10
c 0
b 0
f 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A initialize() 0 12 2
A getParameters() 0 4 1
A getParameter() 0 4 1
A getUsername() 0 4 1
A setUsername() 0 4 1
A getPassword() 0 4 1
A setPassword() 0 4 1
A getResponse() 0 8 2
A send() 0 6 1
A setParameter() 0 10 2
1
<?php
2
/**
3
 * Abstract Request
4
 */
5
6
namespace IBM\Watson\Common\Message;
7
8
use GuzzleHttp\ClientInterface;
9
use IBM\Watson\Common\Exception\RuntimeException;
10
use IBM\Watson\Common\Helper;
11
use Omnipay\Common\Message\ResponseInterface;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, IBM\Watson\Common\Message\ResponseInterface.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
12
use Symfony\Component\HttpFoundation\ParameterBag;
13
use Symfony\Component\HttpFoundation\Request;
14
15
/**
16
 * Abstract Request
17
 *
18
 * This abstract class implements RequestInterface and defines a basic
19
 * set of functions that all Watson Request are intended to include.
20
 *
21
 * @see RequestInterface
22
 */
23
abstract class AbstractRequest implements RequestInterface
24
{
25
    /**
26
     * The request parameters
27
     *
28
     * @var \Symfony\Component\HttpFoundation\ParameterBag
29
     */
30
    protected $parameters;
31
32
    /**
33
     * The request client
34
     *
35
     * @var \Guzzle\Http\ClientInterface
36
     */
37
    protected $httpClient;
38
39
    /**
40
     * The HTTP request object
41
     *
42
     * @var \Symfony\Component\HttpFoundation\Request
43
     */
44
    protected $httpRequest;
45
46
    /**
47
     * An associated ResponseInterface
48
     *
49
     * @var ResponseInterface
50
     */
51
    protected $response;
52
53
    /**
54
     * Create a new Request
55
     *
56
     * @param ClientInterface $httpClient A Guzzle client to make API calls with
57
     * @param Request         $httpRequest A Symfony HTTP requet object
58
     */
59 18
    public function __construct(ClientInterface $httpClient, Request $httpRequest)
0 ignored issues
show
Bug introduced by
You have injected the Request via parameter $httpRequest. This is generally not recommended as there might be multiple instances during a request cycle (f.e. when using sub-requests). Instead, it is recommended to inject the RequestStack and retrieve the current request each time you need it via getCurrentRequest().
Loading history...
60
    {
61 18
        $this->httpClient = $httpClient;
0 ignored issues
show
Documentation Bug introduced by
It seems like $httpClient of type object<GuzzleHttp\ClientInterface> is incompatible with the declared type object<Guzzle\Http\ClientInterface> of property $httpClient.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
62 18
        $this->httpRequest = $httpRequest;
63 18
        $this->initialize();
64 18
    }
65
66
    /**
67
     * Initialize the request with parameters
68
     *
69
     * @param array $parameters Associative array of parameters
70
     *
71
     * @return $this
72
     * @throws RuntimeException
73
     */
74 21
    public function initialize(array $parameters = [])
75
    {
76 21
        if (null !== $this->response) {
77 3
            throw new RuntimeException('Request cannot be modified after it has been sent!');
78
        }
79
80 21
        $this->parameters = new ParameterBag;
81
82 21
        Helper::initialize($this, $parameters);
83
84 21
        return $this;
85
    }
86
87
    /**
88
     * Get all parameters
89
     *
90
     * @return array
91
     */
92 6
    public function getParameters()
93
    {
94 6
        return $this->parameters->all();
95
    }
96
97
    /**
98
     * Get a parameter
99
     *
100
     * @param string $key The parameter key
101
     * @return mixed
102
     */
103 3
    public function getParameter($key)
104
    {
105 3
        return $this->parameters->get($key);
106
    }
107
108
    /**
109
     * Get the API username
110
     *
111
     * @return string
112
     */
113 3
    public function getUsername()
114
    {
115 3
        return $this->getParameter('username');
116
    }
117
118
    /**
119
     * Set the API username
120
     *
121
     * @param string $value
122
     *
123
     * @return $this
124
     */
125 9
    public function setUsername($value)
126
    {
127 9
        return $this->setParameter('username', $value);
128
    }
129
130
    /**
131
     * Get the API password
132
     *
133
     * @return string
134
     */
135 3
    public function getPassword()
136
    {
137 3
        return $this->getParameter('password');
138
    }
139
140
    /**
141
     * Set the API password
142
     *
143
     * @param string $value
144
     *
145
     * @return $this
146
     */
147 6
    public function setPassword($value)
148
    {
149 6
        return $this->setParameter('password', $value);
150
    }
151
152
    /**
153
     * Get the associated Response
154
     *
155
     * @return ResponseInterface
156
     * @throws RuntimeException
157
     */
158 6
    public function getResponse()
159
    {
160 6
        if (null === $this->response) {
161 3
            throw new RuntimeException('You must call send() before accessing the Response!');
162
        }
163
164 3
        return $this->response;
165
    }
166
167
    /**
168
     * Send the request
169
     *
170
     * @return ResponseInterface
171
     */
172 9
    public function send()
173
    {
174 9
        $data = $this->getData();
175
176 9
        return $this->sendData($data);
177
    }
178
179
    /**
180
     * Set a parameter on the request
181
     *
182
     * @param string $key
183
     * @param mixed $value
184
     * @return $this
185
     * @throws RuntimeException
186
     */
187 9
    protected function setParameter($key, $value)
188
    {
189 9
        if (null !== $this->response) {
190 3
            throw new RuntimeException('Request cannot be modified after it has been sent!');
191
        }
192
193 6
        $this->parameters->set($key, $value);
194
195 6
        return $this;
196
    }
197
}
198