1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Gregoriohc\Protean\Common\Messages; |
4
|
|
|
|
5
|
|
|
use ArrayAccess; |
6
|
|
|
use Gregoriohc\Protean\Common\Concerns\Parametrizable; |
7
|
|
|
use IteratorAggregate; |
8
|
|
|
use JsonSerializable; |
9
|
|
|
use RuntimeException; |
10
|
|
|
|
11
|
|
|
abstract class AbstractRequest implements RequestInterface, ArrayAccess, IteratorAggregate, JsonSerializable |
12
|
|
|
{ |
13
|
|
|
use Parametrizable; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* The gateway |
17
|
|
|
* |
18
|
|
|
* @var \Gregoriohc\Protean\Common\AbstractGateway |
19
|
|
|
*/ |
20
|
|
|
protected $gateway; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* The response to this request (if the request has been sent) |
24
|
|
|
* |
25
|
|
|
* @var ResponseInterface |
26
|
|
|
*/ |
27
|
|
|
protected $response; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* AbstractRequest constructor. |
31
|
|
|
* @param \Gregoriohc\Protean\Common\AbstractGateway $gateway |
32
|
|
|
* @param array $parameters |
33
|
|
|
*/ |
34
|
24 |
|
public function __construct($gateway, $parameters = []) |
35
|
|
|
{ |
36
|
24 |
|
$this->gateway = $gateway; |
37
|
24 |
|
$this->bootParameters($parameters); |
38
|
24 |
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @param array $data |
42
|
|
|
* @return ResponseInterface |
43
|
|
|
*/ |
44
|
9 |
|
protected function createResponse($data) |
45
|
|
|
{ |
46
|
9 |
|
$responseClass = $this->responseClass(); |
47
|
|
|
|
48
|
9 |
|
return $this->response = new $responseClass($this, $data); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Get the response to this request (if the request has been sent) |
53
|
|
|
* |
54
|
|
|
* @return ResponseInterface |
55
|
|
|
*/ |
56
|
6 |
|
public function response() |
57
|
|
|
{ |
58
|
6 |
|
if (null === $this->response) { |
59
|
3 |
|
throw new RuntimeException('You must call send() before accessing the Response.'); |
60
|
|
|
} |
61
|
|
|
|
62
|
3 |
|
return $this->response; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Send the request |
67
|
|
|
* |
68
|
|
|
* @return ResponseInterface |
69
|
|
|
*/ |
70
|
15 |
|
public function send() |
71
|
|
|
{ |
72
|
15 |
|
$this->validateParameters(); |
73
|
|
|
|
74
|
9 |
|
$data = $this->data(); |
75
|
|
|
|
76
|
9 |
|
return $this->sendData($data); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @return \Gregoriohc\Protean\Common\AbstractGateway |
81
|
|
|
*/ |
82
|
3 |
|
public function gateway() |
83
|
|
|
{ |
84
|
3 |
|
return $this->gateway; |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|