Completed
Push — master ( ebe90c...1e0a7e )
by Hannes
06:06
created

Middleware   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 3
lcom 0
cbo 2
dl 0
loc 43
ccs 13
cts 13
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A __invoke() 0 20 2
1
<?php
2
3
namespace GuzzleHttp\Profiling;
4
5
use GuzzleHttp\Exception\GuzzleException;
6
use GuzzleHttp\Exception\RequestException;
7
use Psr\Http\Message\RequestInterface;
8
use Psr\Http\Message\ResponseInterface;
9
10
class Middleware
11
{
12
    /**
13
     * @var Profiler
14
     */
15
    private $profiler;
16
17
    /**
18
     * Public constructor.
19
     *
20
     * @param Profiler $profiler
21
     */
22 5
    public function __construct(Profiler $profiler)
23
    {
24 5
        $this->profiler = $profiler;
25 5
    }
26
27
    /**
28
     * @param callable $handler
29
     *
30
     * @return callable
31
     */
32 5
    public function __invoke(callable $handler)
33
    {
34
        return function (RequestInterface $request, array $options) use ($handler) {
35
            // Set starting time.
36 4
            $start = microtime(true);
37
38 4
            return $handler($request, $options)
39
                ->then(function (ResponseInterface $response) use ($start, $request) {
40
                    // After
41 1
                    $this->profiler->add($start, microtime(true), $request, $response);
42
43 1
                    return $response;
44
                }, function (GuzzleException $exception) use ($start, $request) {
45 2
                    $response = $exception instanceof RequestException ? $exception->getResponse() : null;
46 2
                    $this->profiler->add($start, microtime(true), $request, $response);
47
48 2
                    throw $exception;
49 4
                });
50 5
        };
51
    }
52
}
53