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

DebugMiddleware   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 63
Duplicated Lines 66.67 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 0
Metric Value
wmc 9
lcom 0
cbo 3
dl 42
loc 63
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A request() 20 20 4
B response() 22 22 4
A tap() 0 7 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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