1 | <?php |
||
12 | class HttpPlayback |
||
13 | { |
||
14 | protected $mode = 'live'; |
||
15 | |||
16 | protected $callList = []; |
||
17 | |||
18 | protected $recordLocation; |
||
19 | |||
20 | protected $recordFileName = 'saveState.json'; |
||
21 | |||
22 | private $shutdownRegistered = false; |
||
23 | |||
24 | private static $instances = []; |
||
25 | |||
26 | /** |
||
27 | * @var Client |
||
28 | */ |
||
29 | private $client; |
||
30 | |||
31 | 33 | public static function getInstance($options = []) |
|
48 | |||
49 | 30 | public function setPlaybackOptions($options = []) |
|
68 | |||
69 | /** |
||
70 | * Get the client for making calls |
||
71 | * |
||
72 | * @return Client |
||
73 | */ |
||
74 | 26 | public function getHttpClient() |
|
75 | { |
||
76 | 26 | if ($this->client !== null) { |
|
77 | 25 | return $this->client; |
|
78 | } |
||
79 | |||
80 | 26 | $handler = HandlerStack::create(); |
|
81 | |||
82 | 26 | if ($this->mode == 'record') { |
|
83 | $history = Middleware::history($this->callList); |
||
84 | $handler->push($history); |
||
85 | 26 | } elseif ($this->mode == 'playback') { |
|
86 | 26 | $recordings = $this->getRecordings(); |
|
87 | 26 | $mockHandler = new MockHandler($this->arrayToResponses($recordings)); |
|
88 | 26 | $handler = HandlerStack::create($mockHandler); |
|
89 | 26 | } |
|
90 | |||
91 | 26 | $this->registerShutdown(); |
|
92 | 26 | $this->client = new Client(['handler' => $handler]); |
|
93 | |||
94 | 26 | return $this->client; |
|
95 | } |
||
96 | |||
97 | /** |
||
98 | * Sets the client |
||
99 | * |
||
100 | * @param Client $client |
||
101 | * @return $this |
||
102 | */ |
||
103 | public function setHttpClient($client) |
||
109 | |||
110 | 27 | public function getRecordLocation() |
|
111 | { |
||
112 | 27 | if (!$this->recordLocation) { |
|
113 | 26 | $dir = __DIR__; |
|
114 | 26 | $dirPos = strrpos($dir, "src/API"); |
|
115 | 26 | $dir = substr($dir, 0, $dirPos); |
|
116 | |||
117 | 26 | $this->recordLocation = $dir.'Resources/recordings/'; |
|
118 | 26 | } |
|
119 | |||
120 | 27 | return $this->recordLocation; |
|
121 | } |
||
122 | |||
123 | 27 | public function getRecordFilePath() |
|
130 | |||
131 | 26 | public function getRecordings() |
|
138 | |||
139 | 1 | public function endRecord() |
|
140 | { |
||
155 | |||
156 | 26 | protected function registerShutdown() |
|
163 | |||
164 | /** |
||
165 | * @param $responses |
||
166 | * @return array |
||
167 | */ |
||
168 | 1 | protected function responsesToArray($responses) |
|
200 | |||
201 | /** |
||
202 | * @param $items |
||
203 | * @return Response[] |
||
204 | */ |
||
205 | 26 | protected function arrayToResponses($items) |
|
225 | } |
||
226 |