Completed
Pull Request — master (#11)
by Barney
02:36
created

StopTimer::createFrom()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 3

Duplication

Lines 9
Ratio 100 %

Importance

Changes 0
Metric Value
dl 9
loc 9
c 0
b 0
f 0
rs 9.6666
cc 2
eloc 3
nc 2
nop 2
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 GuzzleHttp\Promise\PromiseInterface;
15
use Psr\Http\Message\RequestInterface;
16
use Psr\Http\Message\ResponseInterface;
17
use Shrikeh\GuzzleMiddleware\TimerLogger\Handler\ExceptionHandler\ExceptionHandlerInterface;
18
use Shrikeh\GuzzleMiddleware\TimerLogger\Handler\ExceptionHandler\NullExceptionHandler;
0 ignored issues
show
Bug introduced by
The type Shrikeh\GuzzleMiddleware...er\NullExceptionHandler was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
19
use Shrikeh\GuzzleMiddleware\TimerLogger\ResponseTimeLogger\ResponseTimeLoggerInterface;
20
21
/**
22
 * Class StartHandler.
23
 */
24
class StopTimer
25
{
26
    /**
27
     * @var ResponseTimeLoggerInterface
28
     */
29
    private $responseTimeLogger;
30
31
    /**
32
     * @var ExceptionHandlerInterface
33
     */
34
    private $exceptionHandler;
35
36
    /**
37
     * @param ResponseTimeLoggerInterface    $responseTimeLogger A logger for logging the response start
38
     * @param ExceptionHandlerInterface|null $exceptionHandler   An optional handler for exceptions
39
     *
40
     * @return StopTimer
41
     */
42 View Code Duplication
    public static function createFrom(
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
43
        ResponseTimeLoggerInterface $responseTimeLogger,
44
        ExceptionHandlerInterface $exceptionHandler = null
45
    ) {
46
        if (!$exceptionHandler) {
47
            $exceptionHandler = new NullExceptionHandler();
48
        }
49
50
        return new self($responseTimeLogger, $exceptionHandler);
51
    }
52
53
    /**
54
     * StopTimer constructor.
55
     *
56
     * @param ResponseTimeLoggerInterface $responseTimeLogger The logger for the Response
57
     * @param ExceptionHandlerInterface   $exceptionHandler   An exception handler
58
     */
59
    public function __construct(
60
        ResponseTimeLoggerInterface $responseTimeLogger,
61
        ExceptionHandlerInterface $exceptionHandler
62
    ) {
63
        $this->responseTimeLogger = $responseTimeLogger;
64
        $this->exceptionHandler = $exceptionHandler;
65
    }
66
67
    /**
68
     * @param RequestInterface $request The Request to stop timing
69
     * @param array            $options An ignorable list of options
70
     * @param PromiseInterface $promise A Promise to fulfill
71
     */
72
    public function __invoke(
73
        RequestInterface $request,
74
        array $options,
75
        PromiseInterface $promise
76
    ) {
77
        $promise->then(
78
            $this->onSuccess($request),
79
            $this->onFailure($request)
80
        );
81
    }
82
83
    /**
84
     * @param RequestInterface $request The Request being timed
85
     *
86
     * @return callable|\Closure
87
     */
88
    private function onSuccess(RequestInterface $request)
89
    {
90
        return function (ResponseInterface $response) use ($request) {
0 ignored issues
show
Bug Best Practice introduced by
The expression return function(...) { /* ... */ } returns the type callable which is incompatible with the documented return type Closure|callable.
Loading history...
91
            $this->responseTimeLogger->stop($request, $response);
92
        };
93
    }
94
95
    /**
96
     * @param RequestInterface $request The Request being timed
97
     *
98
     * @return callable|\Closure
99
     */
100
    private function onFailure(RequestInterface $request)
101
    {
102
        return function (ResponseInterface $response) use ($request) {
0 ignored issues
show
Bug Best Practice introduced by
The expression return function(...) { /* ... */ } returns the type callable which is incompatible with the documented return type Closure|callable.
Loading history...
103
            $this->responseTimeLogger->stop($request, $response);
104
        };
105
    }
106
}
107