Completed
Pull Request — master (#29)
by
unknown
02:07
created

DebugMiddleware::tap()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
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\RejectedPromise;
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()
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 View Code Duplication
    public static function 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...
50
    {
51
        return function (RequestInterface $request, $options, $responsePromise) {
52
            /* @var FulfilledPromise | RejectedPromise $responsePromise */
53
            $responsePromise->then(function (ResponseInterface $response) {
54
                echo chr(27) . '[33m' . "RESPONSE: HTTP/" . $response->getProtocolVersion() . ' ' . $response->getStatusCode() . ' ' . $response->getReasonPhrase() . chr(27) . "[0m\n";
55
56
                foreach ($response->getHeaders() as $key => $headers) {
57
                    foreach ($headers as $header) {
58
                        echo "${key}: ${header}\n";
59
                    }
60
                }
61
62
                $body = (string) $response->getBody();
63
                $json = @json_decode($body, true);
64
                if ($json === null) {
65
                    $json = [];
66
                }
67
                print_r($json);
68
            });
69
        };
70
    }
71
72
    /**
73
     * @return callable
74
     */
75
    public static function tap(): callable
76
    {
77
        return Middleware::tap(
78
            self::request(),
79
            self::response()
80
        );
81
    }
82
}
83