StopTimer   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 12
dl 0
loc 35
rs 10
c 0
b 0
f 0
wmc 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A closure() 0 9 2
A __invoke() 0 9 1
1
<?php
2
/**
3
 * @codingStandardsIgnoreStart
4
 *
5
 * @author       Barney Hanlon <[email protected]>
6
 * @copyright    Barney Hanlon 2017
7
 * @license      https://opensource.org/licenses/MIT
8
 *
9
 * @codingStandardsIgnoreEnd
10
 */
11
12
namespace Shrikeh\GuzzleMiddleware\TimerLogger\Handler;
13
14
use Exception;
15
use GuzzleHttp\Promise\PromiseInterface;
16
use Psr\Http\Message\RequestInterface;
17
use Psr\Http\Message\ResponseInterface;
18
use Shrikeh\GuzzleMiddleware\TimerLogger\Handler\Traits\HandlerStaticConstructorTrait;
19
20
/**
21
 * Class StartHandler.
22
 */
23
final class StopTimer
24
{
25
    use HandlerStaticConstructorTrait;
26
27
    /**
28
     * @param RequestInterface $request The Request to stop timing
29
     * @param array            $options An ignorable list of options
30
     * @param PromiseInterface $promise A Promise to fulfill
31
     */
32
    public function __invoke(
33
        RequestInterface $request,
34
        array $options,
35
        PromiseInterface $promise
36
    ) {
37
        $closure = $this->closure($request);
38
        $promise->then(
39
            $closure,
40
            $closure
41
        );
42
    }
43
44
    /**
45
     * @param RequestInterface $request The Request being timed
46
     *
47
     * @return callable|\Closure
48
     */
49
    private function closure(RequestInterface $request)
50
    {
51
        $exceptionHandler = $this->exceptionHandler;
52
53
        return function (ResponseInterface $response) use ($request, $exceptionHandler) {
54
            try {
55
                $this->responseTimeLogger->stop($request, $response);
56
            } catch (Exception $e) {
57
                $exceptionHandler->handle($e);
58
            }
59
        };
60
    }
61
}
62