Passed
Push — develop ( 8e5858...26158a )
by Alec
02:57
created

Profiler::makeCounter()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 1
dl 0
loc 6
ccs 4
cts 4
cp 1
crap 2
rs 10
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
3
namespace AlecRabbit\Tools;
4
5
use AlecRabbit\Tools\Contracts\ProfilerInterface;
6
use AlecRabbit\Tools\Contracts\Strings;
7
use AlecRabbit\Tools\Reports\Contracts\ReportableInterface;
8
use AlecRabbit\Tools\Reports\ProfilerReport;
9
use AlecRabbit\Tools\Reports\Traits\HasReport;
10
use AlecRabbit\Traits\DefaultableName;
11
use const AlecRabbit\Traits\Constants\DEFAULT_NAME;
12
13
class Profiler implements ProfilerInterface, ReportableInterface, Strings
14
{
15
    use DefaultableName, HasReport;
16
17
    /** @var Timer[] */
18
    private $timers = [];
19
20
    /** @var AbstractCounter[] */
21
    private $counters = [];
22
23
    /**
24
     * Profiler constructor.
25
     * @throws \Exception
26
     */
27 9
    public function __construct()
28
    {
29 9
        $this->counter(); // Create default counter
30 9
        $this->timer(); // Create default timer
31 9
        $this->report = (new ProfilerReport())->buildOn($this);
32
//        dump($this->timers);
33 9
    }
34
35
    /**
36
     * @param null|string $name
37
     * @param string ...$suffixes
38
     * @return AbstractCounter
39
     * @throws \Exception
40
     */
41 9
    public function counter(?string $name = null, string ...$suffixes): AbstractCounter
42
    {
43 9
        $name = $this->prepareName($name, $suffixes);
44
        return
45 9
            $this->counters[$name] ?? $this->counters[$name] = $this->makeCounter($name);
46
    }
47
48
    /**
49
     * @param null|string $name
50
     * @param array $suffixes
51
     * @return string
52
     */
53 9
    protected function prepareName(?string $name, array $suffixes): string
54
    {
55 9
        $name = $this->defaultName($name);
56 9
        if (!empty($suffixes)) {
57 2
            return $this->formatName($name, $suffixes);
58
        }
59 9
        return $name;
60
    }
61
62
    /**
63
     * @param string $name
64
     * @param array $suffixes
65
     * @return string
66
     */
67 2
    protected function formatName(string $name, array $suffixes): string
68
    {
69
        return
70 2
            sprintf(static::NAME_FORMAT, $name, implode(', ', $suffixes));
71
    }
72
73
    /**
74
     * @param string $name
75
     * @return AbstractCounter
76
     * @throws \Exception
77
     */
78 9
    protected function makeCounter(string $name): AbstractCounter
79
    {
80 9
        if (DEFAULT_NAME === $name) {
81 9
            return new SimpleCounter($name);
82
        }
83 3
        return new ExtendedCounter($name);
84
    }
85
86
    /**
87
     * @param null|string $name
88
     * @param string ...$suffixes
89
     * @return Timer
90
     * @throws \Exception
91
     */
92 9
    public function timer(?string $name = null, string ...$suffixes): Timer
93
    {
94 9
        $name = $this->prepareName($name, $suffixes);
95
        return
96 9
            $this->timers[$name] ?? $this->timers[$name] = new Timer($name);
97
    }
98
99
    /**
100
     * @return AbstractTimer[]
101
     */
102 9
    public function getTimers(): array
103
    {
104 9
        return $this->timers;
105
    }
106
107
    /**
108
     * @return AbstractCounter[]
109
     */
110 9
    public function getCounters(): array
111
    {
112 9
        return $this->counters;
113
    }
114
}
115