Completed
Pull Request — master (#20)
by Dennis
02:26
created

DebugMiddleware::response()   B

Complexity

Conditions 4
Paths 1

Size

Total Lines 24
Code Lines 15

Duplication

Lines 21
Ratio 87.5 %

Importance

Changes 0
Metric Value
dl 21
loc 24
rs 8.6845
c 0
b 0
f 0
cc 4
eloc 15
nc 1
nop 0
1
<?php declare(strict_types = 1);
2
3
namespace Link0\Bunq\Middleware;
4
5
use GuzzleHttp\Middleware;
6
use GuzzleHttp\Promise\FulfilledPromise;
7
use GuzzleHttp\Promise\PromiseInterface;
8
use Psr\Http\Message\RequestInterface;
9
use Psr\Http\Message\ResponseInterface;
10
11
/**
12
 * Be able to debug request and responses
13
 *
14
 * Usage:
15
 *   $handlerStack->push(DebugMiddleware::tap(), 'debug_tap');
16
 *
17
 * All requests and responses will now be printed to STDOUT
18
 *
19
 */
20
final class DebugMiddleware
21
{
22
    /**
23
     * @return \Closure
24
     */
25 View Code Duplication
    public static function request()
1 ignored issue
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...
26
    {
27
        return function (RequestInterface $request) {
28
            echo chr(27) . '[33m' . "REQUEST: " . $request->getMethod() . ' ' . $request->getRequestTarget() . chr(27) . "[0m\n";
29
30
            foreach ($request->getHeaders() as $key => $headers) {
31
                foreach ($headers as $header) {
32
                    echo "${key}: ${header}\n";
33
                }
34
            }
35
36
            $body = (string) $request->getBody();
37
            echo $body;
38
            $json = @json_decode($body, true);
39
            if ($json === null) {
40
                $json = [];
41
            }
42
            print_r($json);
43
        };
44
    }
45
46
    /**
47
     * @return \Closure
48
     */
49
    public static function response()
50
    {
51 View Code Duplication
        return function (RequestInterface $request, $options, PromiseInterface $responsePromise) {
1 ignored issue
show
Duplication introduced by
This code seems to be duplicated across 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...
52
            return $responsePromise->then(function (ResponseInterface $response) {
53
                echo chr(27) . '[33m' . "RESPONSE: HTTP/" .
54
                    $response->getProtocolVersion() . ' ' .
55
                    $response->getStatusCode() . ' ' .
56
                    $response->getReasonPhrase() . chr(27) . "[0m\n";
57
58
                foreach ($response->getHeaders() as $key => $headers) {
59
                    foreach ($headers as $header) {
60
                        echo "${key}: ${header}\n";
61
                    }
62
                }
63
64
                $body = (string) $response->getBody();
65
                $json = @json_decode($body, true);
66
                if ($json === null) {
67
                    $json = [];
68
                }
69
                print_r($json);
70
            });
71
        };
72
    }
73
74
    /**
75
     * @return callable
76
     */
77
    public static function tap(): callable
78
    {
79
        return Middleware::tap(
80
            self::request(),
81
            self::response()
82
        );
83
    }
84
}
85