Completed
Pull Request — master (#5)
by
unknown
01:54
created

VcrHandler::isBinary()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 0
cts 3
cp 0
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 6
1
<?php
2
namespace Dshafik\GuzzleHttp;
3
4
use \GuzzleHttp\Psr7\Response;
5
6
/**
7
 * guzzlehttp-vcr middleware
8
 *
9
 * Records and automatically replays responses on subsequent requests
10
 * for unit testing
11
 * 
12
 * @package Dshafik\GuzzleHttp
13
 */
14
class VcrHandler
15
{
16
    const CONFIG_ONLY_ENCODE_BINARY = 'only_encode_binary';
17
18
    /**
19
     * @var string
20
     */
21
    protected $cassette;
22
23
    /**
24
     * Configuration values
25
     *
26
     * @var array
27
     */
28
    protected static $config;
29
30
    /**
31
     * @param string $cassette fixture path 
32
     * @return \GuzzleHttp\HandlerStack
33
     */
34 5
    public static function turnOn($cassette, array $config = null)
35
    {
36 5
        if (! is_null($config)) {
37
            static::$config = $config;
38
        }
39
40 5
        if (!file_exists($cassette)) {
41 4
            $handler = \GuzzleHttp\HandlerStack::create();
42 4
            $handler->after('allow_redirects', new static($cassette), 'vcr_recorder');
43 4
            return $handler;
44
        } else {
45 5
            $responses = self::decodeResponses($cassette);
46
47 5
            $queue = [];
48 5
            $class = new \ReflectionClass(\GuzzleHttp\Psr7\Response::class);
49 5
            foreach ($responses as $response) {
50 5
                $queue[] = $class->newInstanceArgs($response);
51
            }
52
53 5
            return \GuzzleHttp\HandlerStack::create(new \GuzzleHttp\Handler\MockHandler($queue));
54
        }
55
    }
56
57
    /**
58
     * Constructor
59
     * 
60
     * @param string $cassette fixture path
61
     */
62 4
    protected function __construct($cassette)
63
    {
64 4
        $this->cassette = $cassette;
65 4
    }
66
67
    /**
68
     * Returns configuration value by name
69
     *
70
     * @param string $name Name of configuration value
71
     * 
72
     * @return null|mixed
73
     */
74 5
    protected static function getConfig($name = null)
75
    {
76 5
        if (is_null($name)) {
77
            return static::$config;
78
        }
79
80 5
        if (! static::$config || ! isset(static::$config[$name])) {
0 ignored issues
show
Bug Best Practice introduced by
The expression static::$config of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
81 5
            return null;
82
        }
83
84
        return $config[$name];
0 ignored issues
show
Bug introduced by
The variable $config does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
85
    }
86
87
    /**
88
     * Returns True if response content is binary
89
     *
90
     * @param GuzzleHttp\Psr7\Response $response Response object
91
     * 
92
     * @return boolean
93
     */
94
    protected static function isBinary(Response $response)
95
    {
96
        return strpos($response->getContentType(), 'application/x-gzip') !== false
0 ignored issues
show
Bug introduced by
The method getContentType() does not seem to exist on object<GuzzleHttp\Psr7\Response>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
97
            || $response->getHeader('Content-Transfer-Encoding') == 'binary';
98
    }
99
100
    /**
101
     * Resolve an object Response from given value. 
102
     * If value is already a Response returns value.
103
     * If value is an array attempts to create a Response object
104
     * otherwise throws an exception.
105
     *
106
     * @param Response|array $value
107
     * 
108
     * @throws \InvalidArgumentException
109
     * 
110
     * @return void
111
     */
112 5
    protected static function ensureResponse($value)
113
    {
114 5
        if ($value instanceof Response) {
115 4
            return $value;
116
        }
117
118 5
        if (! is_array($value)) {
119
            throw new \InvalidArgumentException('Invalid value for response');
120
        }
121
122 5
        return new Response(
123 5
            $value['status'],
124 5
            $value['headers'],
125 5
            $value['body'],
126 5
            $value['version'],
127 5
            $value['reason']
128
        );
129
    }
130
131
    /**
132
     * Encode body content from given response.
133
     *
134
     * @param Response|array $response Response value
135
     * 
136
     * @return string
137
     */
138 4 View Code Duplication
    protected static function encodeBodyFrom($response)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
139
    {
140 4
        $response = static::ensureResponse($response);
141 4
        $body = (string) $response->getBody();
142
143 4
        if (static::getConfig(self::CONFIG_ONLY_ENCODE_BINARY)
144 4
            && ! static::isBinary($response)
145
        ) {
146
            return $body;
147
        }
148
149 4
        return \base64_encode($body);
150
    }
151
152
    /**
153
     * Decode body content from given response.
154
     *
155
     * @param Response|array $response Response value
156
     * 
157
     * @return string
158
     */
159 5 View Code Duplication
    protected static function decodeBodyFrom($response)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
160
    {
161 5
        $response = static::ensureResponse($response);
162 5
        $body = (string) $response->getBody();
163
164 5
        if (static::getConfig(self::CONFIG_ONLY_ENCODE_BINARY)
165 5
            && ! static::isBinary($response)
166
        ) {
167
            return $body;
168
        }
169
170 5
        return \base64_decode($body);
171
    }
172
173
    /**
174
     * Decodes every responses body from base64
175
     *
176
     * @param $cassette
177
     * @return array
178
     */
179 5
    protected static function decodeResponses($cassette)
180
    {
181 5
        $responses = json_decode(file_get_contents($cassette), true);
182
183 5
        array_walk(
184
            $responses, function (&$response) {
185 5
                $response['body'] = static::decodeBodyFrom($response);
186 5
            }
187
        );
188
189 5
        return $responses;
190
    }
191
192
    /**
193
     * Handle the request/response
194
     *
195
     * @param callable $handler
196
     * @return \Closure
197
     */
198 4
    public function __invoke(callable $handler)
199
    {
200
        return function (\Psr\Http\Message\RequestInterface $request, array $config) use ($handler) {
201 4
            return $handler($request, $config)->then(
202
                function (\Psr\Http\Message\ResponseInterface $response) use ($request) {
203 4
                    $responses = [];
204 4
                    if (file_exists($this->cassette)) {
205
                        //No need to base64 decode body of response here.
206 2
                        $responses = json_decode(file_get_contents($this->cassette), true);
207
                    }
208 4
                    $cassette = $response->withAddedHeader('X-VCR-Recording', time());
209 4
                    $responses[] = [
210 4
                        'status' =>  $cassette->getStatusCode(),
211 4
                        'headers' => $cassette->getHeaders(),
212 4
                        'body' => static::encodeBodyFrom($cassette),
0 ignored issues
show
Documentation introduced by
$cassette is of type object<Psr\Http\Message\ResponseInterface>, but the function expects a object<GuzzleHttp\Psr7\Response>|array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
213 4
                        'version' => $cassette->getProtocolVersion(),
214 4
                        'reason' => $cassette->getReasonPhrase()
215
                    ];
216
                    
217 4
                    file_put_contents($this->cassette, json_encode($responses, JSON_PRETTY_PRINT));
218 4
                    return $response;
219 4
                },
220 4
                function (\Exception $reason) {
221
                    return new \GuzzleHttp\Promise\RejectedPromise($reason);
222 4
                }
223
            );
224 4
        };
225
    }
226
}
227