Timer::start()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 1
crap 1
1
<?php
2
3
/*
4
 * This file is part of the Ivory Http Adapter package.
5
 *
6
 * (c) Eric GELOEN <[email protected]>
7
 *
8
 * For the full copyright and license information, please read the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Ivory\HttpAdapter\Event\Timer;
13
14
use Ivory\HttpAdapter\Message\InternalRequestInterface;
15
16
/**
17
 * @author GeLo <[email protected]>
18
 */
19
class Timer implements TimerInterface
20
{
21
    /**
22
     * {@inheritdoc}
23
     */
24 9
    public function start(InternalRequestInterface $internalRequest)
25
    {
26
        return $internalRequest
27 9
            ->withParameter(self::START_TIME, $this->getTime())
28 9
            ->withoutParameter(self::TIME);
29
    }
30
31
    /**
32
     * {@inheritdoc}
33
     */
34 18
    public function stop(InternalRequestInterface $internalRequest)
35
    {
36 18
        if ($internalRequest->hasParameter(self::START_TIME) && !$internalRequest->hasParameter(self::TIME)) {
37 9
            return $internalRequest->withParameter(
38 9
                self::TIME,
39 9
                $this->getTime() - $internalRequest->getParameter(self::START_TIME)
40 7
            );
41
        }
42
43 9
        return $internalRequest;
44
    }
45
46
    /**
47
     * @return float
48
     */
49 18
    private function getTime()
50
    {
51 18
        return microtime(true);
52
    }
53
}
54