HistoryMiddleware   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 30
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 18
dl 0
loc 30
rs 10
c 0
b 0
f 0
wmc 2

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A __invoke() 0 21 1
1
<?php
2
3
namespace Sludio\HelperBundle\Guzzle\GuzzleHttp\Middleware;
4
5
use GuzzleHttp\Promise\RejectedPromise;
6
use Psr\Http\Message\RequestInterface;
7
use Sludio\HelperBundle\Guzzle\GuzzleHttp\History\History;
8
9
class HistoryMiddleware
10
{
11
    private $container;
12
13
    public function __construct(History $container)
14
    {
15
        $this->container = $container;
16
    }
17
18
    public function __invoke(callable $handler)
19
    {
20
        return function(RequestInterface $request, array $options) use ($handler) {
21
            return $handler($request, $options)->then(function($response) use ($request, $options) {
22
                $this->container->mergeInfo($request, [
23
                    'response' => $response,
24
                    'error' => null,
25
                    'options' => $options,
26
                    'info' => [],
27
                ]);
28
29
                return $response;
30
            }, function($reason) use ($request, $options) {
31
                $this->container->mergeInfo($request, [
32
                    'response' => null,
33
                    'error' => $reason,
34
                    'options' => $options,
35
                    'info' => [],
36
                ]);
37
38
                return new RejectedPromise($reason);
39
            });
40
        };
41
    }
42
}
43