1 | <?php |
||
22 | class ValidGigyaResponseMiddleware |
||
23 | { |
||
24 | /** |
||
25 | * @var string[] |
||
26 | */ |
||
27 | private $requiredFields = [ |
||
28 | 'errorCode', |
||
29 | 'statusCode', |
||
30 | 'statusReason', |
||
31 | 'callId', |
||
32 | 'time', |
||
33 | ]; |
||
34 | |||
35 | /** |
||
36 | * @var callable |
||
37 | */ |
||
38 | private $handler; |
||
39 | |||
40 | /** |
||
41 | * ValidGigyaResponseMiddleware constructor. |
||
42 | * |
||
43 | * @param callable $handler |
||
44 | */ |
||
45 | 5 | public function __construct(callable $handler) |
|
49 | |||
50 | /** |
||
51 | * Return the handler to assert that the response returned is valid |
||
52 | * |
||
53 | * @param RequestInterface $request |
||
54 | * @param array $options |
||
55 | * |
||
56 | * @return Closure |
||
57 | */ |
||
58 | 5 | public function __invoke(RequestInterface $request, array $options) |
|
69 | |||
70 | /** |
||
71 | * @param GuzzleResponseInterface $response |
||
72 | * |
||
73 | * @throws InvalidTimestampException |
||
74 | * @throws UnknownResponseException |
||
75 | * |
||
76 | * @return void |
||
77 | */ |
||
78 | 5 | private function assert(GuzzleResponseInterface $response) |
|
79 | { |
||
80 | 5 | $data = json_decode($response->getBody(), true); |
|
81 | 5 | if (!is_array($data)) { |
|
82 | 3 | throw new UnknownResponseException($response, 'Could not decode the body'); |
|
83 | } |
||
84 | |||
85 | 2 | foreach ($this->requiredFields as $field) { |
|
86 | 2 | if (!array_key_exists($field, $data)) { |
|
87 | 1 | throw new UnknownResponseException($response, "Missing required field: '{$field}'"); |
|
88 | } |
||
89 | 2 | } |
|
90 | 1 | } |
|
91 | |||
92 | /** |
||
93 | * Returns a Middleware handler functions for this class |
||
94 | * |
||
95 | * @return Closure |
||
96 | */ |
||
97 | public static function middleware() |
||
103 | } |
||
104 |