1 | <?php |
||
22 | class Client implements HttpClient, HttpAsyncClient |
||
23 | { |
||
24 | use HttpAsyncClientEmulator; |
||
25 | |||
26 | /** |
||
27 | * @var ResponseFactory |
||
28 | */ |
||
29 | private $responseFactory; |
||
30 | |||
31 | /** |
||
32 | * @var RequestInterface[] |
||
33 | */ |
||
34 | private $requests = []; |
||
35 | |||
36 | /** |
||
37 | * @var ResponseInterface[] |
||
38 | */ |
||
39 | private $responses = []; |
||
40 | |||
41 | /** |
||
42 | * @var ResponseInterface|null |
||
43 | */ |
||
44 | private $defaultResponse; |
||
45 | |||
46 | /** |
||
47 | * @var Exception[] |
||
48 | */ |
||
49 | private $exceptions = []; |
||
50 | |||
51 | /** |
||
52 | * @var Exception|null |
||
53 | */ |
||
54 | private $defaultException; |
||
55 | |||
56 | 10 | public function __construct(ResponseFactory $responseFactory = null) |
|
57 | { |
||
58 | 10 | $this->responseFactory = $responseFactory ?: MessageFactoryDiscovery::find(); |
|
59 | 10 | } |
|
60 | |||
61 | /** |
||
62 | * This will in order: |
||
63 | * |
||
64 | * - Throw the next exception in the list and advance |
||
65 | * - Return the next response in the list and advance |
||
66 | * - Throw the default exception if set (forever) |
||
67 | * - Return the default response if set (forever) |
||
68 | * - Create a new empty response with the response factory |
||
69 | */ |
||
70 | 6 | public function sendRequest(RequestInterface $request): ResponseInterface |
|
71 | { |
||
72 | 6 | $this->requests[] = $request; |
|
73 | |||
74 | 6 | if (count($this->exceptions) > 0) { |
|
75 | 1 | throw array_shift($this->exceptions); |
|
76 | } |
||
77 | |||
78 | 5 | if (count($this->responses) > 0) { |
|
79 | 2 | return array_shift($this->responses); |
|
80 | } |
||
81 | |||
82 | 3 | if ($this->defaultException) { |
|
83 | 1 | throw $this->defaultException; |
|
84 | } |
||
85 | |||
86 | 2 | if ($this->defaultResponse) { |
|
87 | 1 | return $this->defaultResponse; |
|
88 | } |
||
89 | |||
90 | // Return success response by default |
||
91 | 1 | return $this->responseFactory->createResponse(); |
|
92 | } |
||
93 | |||
94 | /** |
||
95 | * Adds an exception that will be thrown. |
||
96 | */ |
||
97 | 1 | public function addException(\Exception $exception) |
|
101 | |||
102 | /** |
||
103 | * Sets the default exception to throw when the list of added exceptions and responses is exhausted. |
||
104 | * |
||
105 | * If both a default exception and a default response are set, the exception will be thrown. |
||
106 | */ |
||
107 | 1 | public function setDefaultException(?\Exception $defaultException) |
|
111 | |||
112 | /** |
||
113 | * Adds a response that will be returned in first in first out order. |
||
114 | */ |
||
115 | 2 | public function addResponse(ResponseInterface $response) |
|
119 | |||
120 | /** |
||
121 | * Sets the default response to be returned when the list of added exceptions and responses is exhausted. |
||
122 | */ |
||
123 | 1 | public function setDefaultResponse(?ResponseInterface $defaultResponse) |
|
127 | |||
128 | /** |
||
129 | * Returns requests that were sent. |
||
130 | * |
||
131 | * @return RequestInterface[] |
||
132 | */ |
||
133 | public function getRequests(): array |
||
137 | |||
138 | 2 | public function getLastRequest(): ?RequestInterface |
|
142 | } |
||
143 |