1 | <?php |
||
27 | class Client |
||
28 | { |
||
29 | const LIVE = 'live'; |
||
30 | |||
31 | const RECORD = 'record'; |
||
32 | |||
33 | const PLAYBACK = 'playback'; |
||
34 | |||
35 | protected $mode = 'live'; |
||
36 | |||
37 | protected $callList = []; |
||
38 | |||
39 | protected $recordLocation; |
||
40 | |||
41 | protected $recordFileName = 'saveState.json'; |
||
42 | |||
43 | private $shutdownRegistered = false; |
||
44 | |||
45 | /** |
||
46 | * @var GuzzleClient |
||
47 | */ |
||
48 | private $client; |
||
49 | |||
50 | 31 | public function __construct($options = []) |
|
51 | { |
||
52 | 31 | $options = array_replace_recursive( |
|
53 | 31 | ['mode' => null, 'recordLocation' => null, 'recordFileName' => null], |
|
54 | $options |
||
55 | ); |
||
56 | |||
57 | 31 | if ($options['mode'] !== null) { |
|
58 | 31 | $this->mode = $options['mode']; |
|
59 | } |
||
60 | |||
61 | 31 | if ($options['recordLocation'] !== null) { |
|
62 | 1 | $this->recordLocation = $options['recordLocation']; |
|
63 | } |
||
64 | |||
65 | 31 | if ($options['recordFileName'] !== null) { |
|
66 | 31 | $this->recordFileName = $options['recordFileName']; |
|
67 | } |
||
68 | |||
69 | 31 | $this->setupClient(); |
|
70 | 31 | $this->registerShutdown(); |
|
71 | } |
||
72 | |||
73 | 28 | public function __call($method, $args) |
|
74 | { |
||
75 | 28 | if (count($args) < 1) { |
|
76 | throw new \InvalidArgumentException('Magic request methods require a URI and optional options array'); |
||
77 | } |
||
78 | |||
79 | 28 | $uri = $args[0]; |
|
80 | 28 | $opts = isset($args[1]) ? $args[1] : []; |
|
81 | |||
82 | 28 | return substr($method, -5) === 'Async' |
|
83 | ? $this->requestAsync(substr($method, 0, -5), $uri, $opts) |
||
84 | 28 | : $this->request($method, $uri, $opts); |
|
85 | } |
||
86 | |||
87 | /** |
||
88 | * @param $method |
||
89 | * @param null $uri |
||
90 | * @param array $options |
||
91 | * |
||
92 | * @return ResponseInterface |
||
93 | */ |
||
94 | 28 | public function request($method, $uri = null, array $options = []) |
|
98 | |||
99 | /** |
||
100 | * @param $method |
||
101 | * @param null $uri |
||
102 | * @param array $options |
||
103 | * |
||
104 | * @return ResponseInterface |
||
105 | */ |
||
106 | public function requestAsync($method, $uri = null, array $options = []) |
||
110 | |||
111 | protected function requestWrapper($method, $uri = null, array $options = [], $async = false) |
||
112 | { |
||
123 | |||
124 | 28 | protected function doRequest($method, $uri = null, array $options = [], $async = false) |
|
142 | |||
143 | /** |
||
144 | * Get the client for making calls |
||
145 | * |
||
146 | * @return Client |
||
147 | */ |
||
148 | 31 | protected function setupClient() |
|
156 | |||
157 | 31 | protected function getRecordLocation() |
|
169 | |||
170 | 31 | protected function getRecordFilePath() |
|
177 | |||
178 | 30 | protected function getRecordings() |
|
185 | |||
186 | 1 | public function endRecord() |
|
203 | |||
204 | 31 | protected function registerShutdown() |
|
211 | |||
212 | /** |
||
213 | * @param $responses |
||
214 | * @return array |
||
215 | */ |
||
216 | 1 | protected function responsesToArray($responses) |
|
247 | |||
248 | /** |
||
249 | * @param $items |
||
250 | * @return Response[] |
||
251 | */ |
||
252 | 30 | protected function arrayToResponses($items) |
|
272 | } |
||
273 |