Passed
Push — master ( fc83aa...c68055 )
by Radu
01:36
created

Statistics::getResult()   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 $result;
9
    protected $timeStart;
10
    protected $timeFinish;
11
    protected $timeZone;
12
13
    public function __construct()
14
    {
15
        $this->timeZone = date_default_timezone_get();
16
    }
17
18
    public function finish($result)
19
    {
20
        $this->result = $result;
21
        $this->timeFinish = $this->createCurrentTimeObject();
22
        $this->duration = $this->timeFinish->format("U.u") - $this->timeStart->format("U.u");
23
        $this->memoryPeakUsage = memory_get_peak_usage(true) / 1024;
24
    }
25
26
    public function getDuration()
27
    {
28
        return $this->duration;
29
    }
30
31
    public function getMemoryPeakUsage()
32
    {
33
        return $this->memoryPeakUsage;
34
    }
35
36
    public function getResult()
37
    {
38
        return $this->result;
39
    }
40
41
    public function start()
42
    {
43
        $this->timeStart = $this->createCurrentTimeObject();
44
    }
45
46
    protected function createCurrentTimeObject()
47
    {
48
        $dateTime = \DateTime::createFromFormat('U.u', (string) microtime(true));
49
        if (!($dateTime instanceof \DateTime)) {
50
            throw new \WebServCo\Framework\Exceptions\ApplicationException(
51
                'Error initializing DateTime object'
52
            );
53
        }
54
        $dateTime->setTimezone(new \DateTimeZone($this->timeZone));
55
        return $dateTime;
56
    }
57
}
58