ProcessingTimeReporter::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
/**
4
 * This file is part of cloak.
5
 *
6
 * (c) Noritaka Horio <[email protected]>
7
 *
8
 * This source file is subject to the MIT license that is bundled
9
 * with this source code in the file LICENSE.
10
 */
11
12
namespace cloak\reporter;
13
14
use cloak\event\AnalyzeStartEvent;
15
use cloak\event\AnalyzeStopEvent;
16
use cloak\writer\ConsoleWriter;
17
use Zend\Console\ColorInterface as Color;
18
use PHPExtra\EventManager\EventManager;
19
use \DateTimeImmutable;
20
21
22
/**
23
 * Class ProcessingTimeReporter
24
 * @package cloak\reporter
25
 */
26
class ProcessingTimeReporter
27
    implements Reporter, AnalyzeStartEventListener, AnalyzeStopEventListener
28
{
29
30
    /**
31
     * @var \cloak\writer\ConsoleWriter
32
     */
33
    private $console;
34
35
    /**
36
     * @var float
37
     */
38
    private $startAt;
39
40
41
    public function __construct()
42
    {
43
        $this->console = new ConsoleWriter();
44
    }
45
46
    /**
47
     * @param \cloak\event\AnalyzeStartEvent $event
48
     */
49
    public function onAnalyzeStart(AnalyzeStartEvent $event)
50
    {
51
        $sendAt = $event->getSendAt();
52
        $this->start($sendAt);
53
    }
54
55
    /**
56
     * @param \cloak\event\AnalyzeStopEvent $event
57
     */
58
    public function onAnalyzeStop(AnalyzeStopEvent $event)
59
    {
60
        $this->finish();
61
    }
62
63
    /**
64
     * @param DateTimeImmutable $startAt
65
     */
66
    private function start(DateTimeImmutable $startAt)
67
    {
68
        $this->console->writeEOL();
69
        $this->writeStartDateTime($startAt);
70
71
        $this->startAt = microtime(true);
72
    }
73
74
    private function finish()
75
    {
76
        $endAt = microtime(true);
77
        $runningTime = round($endAt - $this->startAt, 5);
78
79
        $this->console->writeText("Code Coverage Finished in ");
80
        $this->console->writeText($runningTime, Color::CYAN);
81
        $this->console->writeText(" seconds");
82
        $this->console->writeEOL();
83
    }
84
85
    /**
86
     * @param DateTimeImmutable $startAt
87
     */
88
    private function writeStartDateTime(DateTimeImmutable $startAt)
89
    {
90
        $formatStartTime = $startAt->format('j F Y \a\t H:i');
91
92
        $this->console->writeText('Code Coverage Started: ');
93
        $this->console->writeText($formatStartTime, Color::CYAN);
94
        $this->console->writeEOL();
95
    }
96
97
    /**
98
     * @param EventManager $eventManager
99
     */
100
    public function registerTo(EventManager $eventManager)
101
    {
102
        $eventManager->add($this);
103
    }
104
105
}
106