Completed
Push — master ( 23d6c4...eb4d77 )
by Alec
06:36
created

Profiler::counter()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 2
dl 0
loc 5
ccs 3
cts 3
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * User: alec
4
 * Date: 14.10.18
5
 * Time: 2:13
6
 */
7
8
namespace AlecRabbit\Profiler;
9
10
use AlecRabbit\Profiler\Contracts\Profiler as ProfilerContract;
11
12
class Profiler implements ProfilerContract
13
{
14
    /** @var Timer[] */
15
    private $timers = [];
16
17
    /** @var Counter[] */
18
    private $counters = [];
19
20
    /**
21
     * @param null|string $name
22
     * @param string ...$suffixes
23
     * @return Counter
24
     */
25 5
    public function counter(?string $name = null, string ...$suffixes): Counter
26
    {
27 5
        $name = $this->prepName($name, $suffixes);
28
        return
29 5
            $this->counters[$name] ?? $this->counters[$name] = new Counter($name);
30
    }
31
32
    /**
33
     * @param null|string $name
34
     * @param array $suffixes
35
     * @return string
36
     */
37 7
    private function prepName(?string $name, array $suffixes): string
38
    {
39 7
        $name = $name ?? static::_DEFAULT;
40 7
        if (!empty($suffixes)) {
41 4
            return $this->formatName($name, $suffixes);
42
        }
43 6
        return $name;
44
    }
45
46
    /**
47
     * @param string $name
48
     * @param array $suffixes
49
     * @return string
50
     */
51 4
    protected function formatName(string $name, array $suffixes): string
52
    {
53
        return
54 4
            sprintf(static::_NAME_FORMAT, $name, implode(', ', $suffixes));
55
    }
56
57
    /**
58
     * @param null|string $name
59
     * @param string ...$suffixes
60
     * @return Timer
61
     */
62 5
    public function timer(?string $name = null, string ...$suffixes): Timer
63
    {
64 5
        $name = $this->prepName($name, $suffixes);
65
        return
66 5
            $this->timers[$name] ?? $this->timers[$name] = new Timer($name);
67
    }
68
69
    /**
70
     * @param bool|null $formatted
71
     * @param bool|null $extended
72
     * @param int|null $units
73
     * @param int|null $precision
74
     * @return iterable
75
     */
76 5
    public function report(
77
        ?bool $formatted = null,
78
        ?bool $extended = null,
79
        ?int $units = null,
80
        ?int $precision = null
81
    ): iterable {
82 5
        $report = [];
83 5
        foreach ($this->counters as $counter) {
84 2
            $report[static::_COUNTERS][$counter->getName()] = $counter->report($extended);
85
        }
86 5
        foreach ($this->timers as $timer) {
87 3
            $report[static::_TIMERS][$timer->getName()] = $timer->report($formatted, $extended, $units, $precision);
88
        }
89
        return
90 5
            $report;
91
    }
92
93
    /**
94
     * @return Timer[]
95
     */
96 1
    public function getTimers(): array
97
    {
98 1
        return $this->timers;
99
    }
100
101
    /**
102
     * @return Counter[]
103
     */
104 1
    public function getCounters(): array
105
    {
106 1
        return $this->counters;
107
    }
108
}
109