Passed
Push — master ( bf5028...3a1f66 )
by Radu
03:00
created

Statistics::start()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
namespace WebServCo\Framework\Cli\Runner;
3
4
final class Statistics
5
{
6
    protected $duration; // <seconds>.<microseconds>
7
    protected $memoryPeakUsage; // K
8
    protected $timeStart;
9
    protected $timeFinish;
10
    protected $timeZone;
11
12
    public function __construct()
13
    {
14
        $this->timeZone = date_default_timezone_get();
15
    }
16
17
    public function finish()
18
    {
19
        $this->timeFinish = $this->createCurrentTimeObject();
20
        $this->duration = $this->timeFinish->format("U.u") - $this->timeStart->format("U.u");
21
        $this->memoryPeakUsage = memory_get_peak_usage(true) / 1024;
22
    }
23
24
    public function getDuration()
25
    {
26
        return $this->duration;
27
    }
28
29
    public function getMemoryPeakUsage()
30
    {
31
        return $this->memoryPeakUsage;
32
    }
33
34
    public function start()
35
    {
36
        $this->timeStart = $this->createCurrentTimeObject();
37
    }
38
39
    protected function createCurrentTimeObject()
40
    {
41
        $dateTime = \DateTime::createFromFormat('U.u', (string) microtime(true));
42
        if (!($dateTime instanceof \DateTime)) {
43
            throw new \WebServCo\Framework\Exceptions\ApplicationException(
44
                'Error initializing DateTime object'
45
            );
46
        }
47
        $dateTime->setTimezone(new \DateTimeZone($this->timeZone));
48
        return $dateTime;
49
    }
50
}
51