1 | <?php |
||
25 | abstract class AbstractRequest implements RequestInterface |
||
26 | { |
||
27 | /** |
||
28 | * The request parameters |
||
29 | * |
||
30 | * @var \Symfony\Component\HttpFoundation\ParameterBag |
||
31 | */ |
||
32 | protected $parameters; |
||
33 | |||
34 | /** |
||
35 | * The request client |
||
36 | * |
||
37 | * @var \GuzzleHttp\ClientInterface |
||
38 | */ |
||
39 | protected $httpClient; |
||
40 | |||
41 | /** |
||
42 | * The HTTP request object |
||
43 | * |
||
44 | * @var \Symfony\Component\HttpFoundation\Request |
||
45 | */ |
||
46 | protected $httpRequest; |
||
47 | |||
48 | /** |
||
49 | * An associated ResponseInterface |
||
50 | * |
||
51 | * @var ResponseInterface |
||
52 | */ |
||
53 | protected $response; |
||
54 | |||
55 | /** |
||
56 | * Create a new Request |
||
57 | * |
||
58 | * @param ClientInterface $httpClient A Guzzle client to make API class with |
||
59 | * @param Request $httpRequest A Symfony HTTP request object |
||
60 | */ |
||
61 | 18 | public function __construct(ClientInterface $httpClient, Request $httpRequest) |
|
|
|||
62 | { |
||
63 | 18 | $this->httpClient = $httpClient; |
|
64 | 18 | $this->httpRequest = $httpRequest; |
|
65 | 18 | $this->initialize(); |
|
66 | 18 | } |
|
67 | |||
68 | /** |
||
69 | * Initialize the object with parameters |
||
70 | * |
||
71 | * Any unknown parameters passed will be ignored |
||
72 | * |
||
73 | * @param array $parameters an associative array of parameters |
||
74 | * |
||
75 | * @return $this |
||
76 | * @throws RuntimeException |
||
77 | */ |
||
78 | 30 | public function initialize(array $parameters = []) |
|
79 | { |
||
80 | 30 | if (null !== $this->response) { |
|
81 | 3 | throw new RuntimeException('Request cannot be modified after it has been sent!'); |
|
82 | } |
||
83 | |||
84 | 30 | $this->parameters = new ParameterBag; |
|
85 | |||
86 | 30 | Helper::initialize($this, $parameters); |
|
87 | |||
88 | 30 | return $this; |
|
89 | } |
||
90 | |||
91 | /** |
||
92 | * Send the request |
||
93 | * |
||
94 | * @return ResponseInterface |
||
95 | */ |
||
96 | 12 | public function send() |
|
97 | { |
||
98 | 12 | $data = $this->getData(); |
|
99 | |||
100 | 12 | return $this->sendData($data); |
|
101 | } |
||
102 | |||
103 | /** |
||
104 | * Get all parameters |
||
105 | * |
||
106 | * @return array |
||
107 | */ |
||
108 | 6 | public function getParameters() |
|
112 | |||
113 | /** |
||
114 | * Get a single parameter |
||
115 | * |
||
116 | * @param string $key The parameter key |
||
117 | * |
||
118 | * @return mixed |
||
119 | */ |
||
120 | 9 | public function getParameter($key) |
|
124 | |||
125 | /** |
||
126 | * Set a single parameter |
||
127 | * |
||
128 | * @param string $key The parameter key |
||
129 | * @param mixed $value The value to set |
||
130 | * |
||
131 | * @return AbstractRequest $this Provides a fluent interface |
||
132 | * @throws RuntimeException if a request parameter is modified after the request is sent |
||
133 | */ |
||
134 | 15 | protected function setParameter($key, $value) |
|
144 | |||
145 | /** |
||
146 | * Sets the username of the request |
||
147 | * |
||
148 | * @param string $value |
||
149 | * |
||
150 | * @return AbstractRequest |
||
151 | */ |
||
152 | 12 | public function setUsername($value) |
|
156 | |||
157 | /** |
||
158 | * Gets the username of the request |
||
159 | * |
||
160 | * @return string |
||
161 | */ |
||
162 | 6 | public function getUsername() |
|
166 | |||
167 | /** |
||
168 | * Sets the password of the request |
||
169 | * |
||
170 | * @param string $value |
||
171 | * |
||
172 | * @return AbstractRequest |
||
173 | */ |
||
174 | 9 | public function setPassword($value) |
|
178 | |||
179 | /** |
||
180 | * Gets the password of the request |
||
181 | * |
||
182 | * @return mixed |
||
183 | */ |
||
184 | 6 | public function getPassword() |
|
188 | |||
189 | /** |
||
190 | * Get the associated Response |
||
191 | * |
||
192 | * @return ResponseInterface |
||
193 | */ |
||
194 | 6 | public function getResponse() |
|
202 | } |
||
203 |