1 | <?php |
||
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) |
|
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 = []) |
|
86 | |||
87 | /** |
||
88 | * Get all parameters |
||
89 | * |
||
90 | * @return array |
||
91 | */ |
||
92 | 6 | public function getParameters() |
|
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() |
|
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() |
|
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) |
|
211 | } |
||
212 |
Let’s assume that you have a directory layout like this:
and let’s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/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 beforeOtherDir/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: