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