BenchmarkService::result()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 5
nc 4
nop 0
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: alex
5
 * Date: 17.11.5
6
 * Time: 13.56
7
 */
8
9
namespace San4io\RequestLogger\Services;
10
11
/**
12
 * Class TimeService
13
 * @package San4io\RequestLogger\Services
14
 */
15
class BenchmarkService
16
{
17
    /**
18
     * @var mixed
19
     */
20
    protected $startTime;
21
22
    /**
23
     * @var
24
     */
25
    protected $endTime;
26
27
    /**
28
     * @var string
29
     */
30
    protected $name;
31
32
    /**
33
     * TimeService constructor.
34
     * @param string $name
35
     */
36
    public function __construct(string $name)
37
    {
38
        $this->name = $name;
39
    }
40
41
    /**
42
     * Get result
43
     * @return mixed
44
     */
45
    public function result()
46
    {
47
        if (!$this->endTime) {
48
            $this->stop();
49
        }
50
51
        if (!$this->startTime) {
52
            $this->setDefaultStartTime();
53
        }
54
55
        return $this->endTime - $this->startTime;
56
    }
57
58
    /**
59
     * Start measure
60
     */
61
    public function start()
62
    {
63
        $this->startTime = microtime(true);
64
    }
65
66
    /**
67
     * End measure
68
     */
69
    public function stop()
70
    {
71
        $this->endTime = microtime(true);
72
    }
73
74
75
    /**
76
     * Get StartTime if such exists
77
     */
78
    protected function setDefaultStartTime()
79
    {
80
        if (defined('LARAVEL_START')) {
81
            $this->startTime = LARAVEL_START;
0 ignored issues
show
Bug introduced by
The constant San4io\RequestLogger\Services\LARAVEL_START was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
82
        } else {
83
            $this->startTime = microtime(true);
84
        }
85
    }
86
87
}