HistoryMiddleware::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
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