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

StartTimer::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 Exception;
15
use Psr\Http\Message\RequestInterface;
16
use Shrikeh\GuzzleMiddleware\TimerLogger\Handler\ExceptionHandler\ExceptionHandlerInterface;
17
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...
18
use Shrikeh\GuzzleMiddleware\TimerLogger\ResponseTimeLogger\ResponseTimeLoggerInterface;
19
20
/**
21
 * Class StartHandler.
22
 */
23
class StartTimer
24
{
25
    /**
26
     * @var ResponseTimeLoggerInterface
27
     */
28
    private $responseTimeLogger;
29
30
    /**
31
     * @var ExceptionHandlerInterface
32
     */
33
    private $exceptionHandler;
34
35
    /**
36
     * @param ResponseTimeLoggerInterface    $responseTimeLogger A logger for logging the response start
37
     * @param ExceptionHandlerInterface|null $exceptionHandler   An optional handler for exceptions
38
     *
39
     * @return \Shrikeh\GuzzleMiddleware\TimerLogger\Handler\StartTimer
40
     */
41 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...
42
        ResponseTimeLoggerInterface $responseTimeLogger,
43
        ExceptionHandlerInterface $exceptionHandler = null
44
    ) {
45
        if (!$exceptionHandler) {
46
            $exceptionHandler = new NullExceptionHandler();
47
        }
48
49
        return new self($responseTimeLogger, $exceptionHandler);
50
    }
51
52
    /**
53
     * StartTimer constructor.
54
     *
55
     * @param ResponseTimeLoggerInterface $responseTimeLogger A logger for logging the response start
56
     * @param ExceptionHandlerInterface   $exceptionHandler   A handler for exceptions
57
     */
58
    public function __construct(
59
        ResponseTimeLoggerInterface $responseTimeLogger,
60
        ExceptionHandlerInterface $exceptionHandler
61
    ) {
62
        $this->responseTimeLogger = $responseTimeLogger;
63
        $this->exceptionHandler = $exceptionHandler;
64
    }
65
66
    /**
67
     * @param RequestInterface $request The Request to start timing
68
     */
69
    public function __invoke(RequestInterface $request)
70
    {
71
        try {
72
            $this->responseTimeLogger->start($request);
73
        } catch (Exception $e) {
74
            // Pass the exception to the handler
75
            $this->exceptionHandler->handle($e);
76
        }
77
    }
78
}
79