ValidGigyaResponseMiddleware::assert()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
eloc 6
nc 4
nop 1
dl 0
loc 10
ccs 7
cts 7
cp 1
crap 4
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * This file is part of graze/gigya-client
4
 *
5
 * Copyright (c) 2016 Nature Delivered Ltd. <https://www.graze.com>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 *
10
 * @license https://github.com/graze/gigya-client/blob/master/LICENSE.md
11
 * @link    https://github.com/graze/gigya-client
12
 */
13
14
namespace Graze\Gigya\Validation;
15
16
use Closure;
17
use Graze\Gigya\Exception\InvalidTimestampException;
18
use Graze\Gigya\Exception\UnknownResponseException;
19
use Psr\Http\Message\RequestInterface;
20
use Psr\Http\Message\ResponseInterface as GuzzleResponseInterface;
21
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)
46
    {
47 5
        $this->handler = $handler;
48 5
    }
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)
59
    {
60 5
        $fn = $this->handler;
61 5
        return $fn($request, $options)
62 5
            ->then(
63
                function (GuzzleResponseInterface $response) {
64 5
                    $this->assert($response);
65 1
                    return $response;
66 5
                }
67
            );
68
    }
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 2
                throw new UnknownResponseException($response, "Missing required field: '{$field}'");
88
            }
89
        }
90 1
    }
91
92
    /**
93
     * Returns a Middleware handler functions for this class
94
     *
95
     * @return Closure
96
     */
97
    public static function middleware()
98
    {
99 5
        return function (callable $handler) {
100 5
            return new static($handler);
101 5
        };
102
    }
103
}
104