Completed
Push — develop ( 86f0dd...f71342 )
by Adam
12s
created

AbstractRequest::getPassword()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
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 21
    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 21
        $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 21
        $this->httpRequest = $httpRequest;
63 21
        $this->initialize();
64 21
    }
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 36
    public function initialize(array $parameters = [])
75
    {
76 36
        if (null !== $this->response) {
77 3
            throw new RuntimeException('Request cannot be modified after it has been sent!');
78
        }
79
80 36
        $this->parameters = new ParameterBag;
81
82 36
        Helper::initialize($this, $parameters);
83
84 36
        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
     * @param mixed $default Default value
102
     * @return mixed
103
     */
104 27
    public function getParameter($key, $default = null)
105
    {
106 27
        return $this->parameters->get($key, $default);
107
    }
108
109
    /**
110
     * Get the API username
111
     *
112
     * @return string
113
     */
114 21
    public function getUsername()
115
    {
116 21
        return $this->getParameter('username');
117
    }
118
119
    /**
120
     * Set the API username
121
     *
122
     * @param string $value
123
     *
124
     * @return $this
125
     */
126 18
    public function setUsername($value)
127
    {
128 18
        return $this->setParameter('username', $value);
129
    }
130
131
    /**
132
     * Get the API password
133
     *
134
     * @return string
135
     */
136 21
    public function getPassword()
137
    {
138 21
        return $this->getParameter('password');
139
    }
140
141
    /**
142
     * Set the API password
143
     *
144
     * @param string $value
145
     *
146
     * @return $this
147
     */
148 15
    public function setPassword($value)
149
    {
150 15
        return $this->setParameter('password', $value);
151
    }
152
153
    /**
154
     * Get request data
155
     *
156
     * @return array
157
     */
158 18
    public function getData()
159
    {
160
        return [
161 18
            'username'  => $this->getUsername(),
162 18
            'password'  => $this->getPassword(),
163 12
        ];
164
    }
165
166
    /**
167
     * Get the associated Response
168
     *
169
     * @return ResponseInterface
170
     * @throws RuntimeException
171
     */
172 6
    public function getResponse()
173
    {
174 6
        if (null === $this->response) {
175 3
            throw new RuntimeException('You must call send() before accessing the Response!');
176
        }
177
178 3
        return $this->response;
179
    }
180
181
    /**
182
     * Send the request
183
     *
184
     * @return ResponseInterface
185
     */
186 12
    public function send()
187
    {
188 12
        $data = $this->getData();
189
190 12
        return $this->sendData($data);
191
    }
192
193
    /**
194
     * Set a parameter on the request
195
     *
196
     * @param string $key
197
     * @param mixed $value
198
     * @return $this
199
     * @throws RuntimeException
200
     */
201 24
    protected function setParameter($key, $value)
202
    {
203 24
        if (null !== $this->response) {
204 3
            throw new RuntimeException('Request cannot be modified after it has been sent!');
205
        }
206
207 21
        $this->parameters->set($key, $value);
208
209 21
        return $this;
210
    }
211
}
212