1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Muzzle\Messages; |
4
|
|
|
|
5
|
|
|
use ArrayAccess; |
6
|
|
|
use Exception; |
7
|
|
|
use GuzzleHttp\Exception\GuzzleException; |
8
|
|
|
use Prophecy\Promise\PromiseInterface; |
9
|
|
|
use Psr\Http\Message\RequestInterface; |
10
|
|
|
use Psr\Http\Message\ResponseInterface; |
11
|
|
|
use Throwable; |
12
|
|
|
|
13
|
|
|
class Transaction implements ArrayAccess |
14
|
|
|
{ |
15
|
|
|
|
16
|
|
|
protected $request; |
17
|
|
|
protected $response; |
18
|
|
|
protected $error; |
19
|
|
|
protected $options; |
20
|
|
|
|
21
|
|
|
public static function new() |
22
|
|
|
{ |
23
|
|
|
|
24
|
|
|
return new static; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @param RequestInterface $request |
29
|
|
|
* @return Transaction |
30
|
|
|
*/ |
31
|
|
|
public function setRequest(RequestInterface $request) : Transaction |
32
|
|
|
{ |
33
|
|
|
|
34
|
|
|
$this->request = $request; |
35
|
|
|
|
36
|
|
|
return $this; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @return RequestInterface|AssertableRequest |
41
|
|
|
*/ |
42
|
|
|
public function request() : RequestInterface |
43
|
|
|
{ |
44
|
|
|
|
45
|
|
|
return $this->request; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @param ResponseInterface|PromiseInterface $response |
50
|
|
|
* @return Transaction |
51
|
|
|
*/ |
52
|
|
|
public function setResponse($response) : Transaction |
53
|
|
|
{ |
54
|
|
|
|
55
|
|
|
$this->response = $response; |
56
|
|
|
|
57
|
|
|
return $this; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public function setResponseOrError($response) : Transaction |
61
|
|
|
{ |
62
|
|
|
|
63
|
|
|
if ($response instanceof Exception) { |
64
|
|
|
return $this->setError($response); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
return $this->setResponse($response); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @return ResponseInterface|AssertableResponse|PromiseInterface |
72
|
|
|
*/ |
73
|
|
|
public function response() |
74
|
|
|
{ |
75
|
|
|
|
76
|
|
|
return $this->response; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @param Throwable $error |
81
|
|
|
* @return Transaction |
82
|
|
|
*/ |
83
|
|
|
public function setError(Throwable $error) : Transaction |
84
|
|
|
{ |
85
|
|
|
|
86
|
|
|
$this->error = $error; |
87
|
|
|
return $this; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @return GuzzleException |
92
|
|
|
*/ |
93
|
|
|
public function error() |
94
|
|
|
{ |
95
|
|
|
|
96
|
|
|
return $this->error; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @param array $options |
101
|
|
|
* @return Transaction |
102
|
|
|
*/ |
103
|
|
|
public function setOptions(array $options) : Transaction |
104
|
|
|
{ |
105
|
|
|
|
106
|
|
|
$this->options = $options; |
107
|
|
|
|
108
|
|
|
return $this; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @return array |
113
|
|
|
*/ |
114
|
|
|
public function options() : array |
115
|
|
|
{ |
116
|
|
|
|
117
|
|
|
return $this->options; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* {@inheritdoc} |
122
|
|
|
*/ |
123
|
|
|
public function offsetExists($offset) : bool |
124
|
|
|
{ |
125
|
|
|
return isset($this->{$offset}); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* {@inheritdoc} |
130
|
|
|
*/ |
131
|
|
|
public function offsetGet($offset) |
132
|
|
|
{ |
133
|
|
|
if (! $this->offsetExists($offset)) { |
134
|
|
|
return null; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
return $this->{$offset}; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* {@inheritdoc} |
142
|
|
|
* @return self |
143
|
|
|
*/ |
144
|
|
|
public function offsetSet($offset, $value) : Transaction |
145
|
|
|
{ |
146
|
|
|
$this->{$offset} = $value; |
147
|
|
|
|
148
|
|
|
return $this; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* {@inheritdoc} |
153
|
|
|
*/ |
154
|
|
|
public function offsetUnset($offset) : void |
155
|
|
|
{ |
156
|
|
|
unset($this->{$offset}); |
157
|
|
|
} |
158
|
|
|
} |
159
|
|
|
|