Passed
Push — master ( b1a631...fb75b4 )
by Divine Niiquaye
03:00
created

ProfileRoute::getRoute()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of Flight Routing.
7
 *
8
 * PHP version 7.1 and above required
9
 *
10
 * @author    Divine Niiquaye Ibok <[email protected]>
11
 * @copyright 2019 Biurad Group (https://biurad.com/)
12
 * @license   https://opensource.org/licenses/BSD-3-Clause License
13
 *
14
 * For the full copyright and license information, please view the LICENSE
15
 * file that was distributed with this source code.
16
 */
17
18
namespace Flight\Routing;
19
20
use ArrayIterator;
21
use Flight\Routing\Interfaces\RouteInterface;
22
use IteratorAggregate;
23
use Traversable;
24
25
/**
26
 * @author Divine Niiquaye Ibok <[email protected]>
27
 */
28
final class ProfileRoute implements IteratorAggregate
29
{
30
    /** @var null|RouteInterface */
31
    private $route;
32
33
    /** @var string */
34
    private $name;
35
36
    /** @var bool */
37
    private $matched = false;
38
39
    /** @var array<string,float|int> */
40
    private $starts = [];
41
42
    /** @var array<string,float|int> */
43
    private $ends = [];
44
45
    /** @var ProfileRoute[] */
46
    private $profiles = [];
47
48
    public function __construct(string $name = 'main', ?RouteInterface $route = null)
49
    {
50
        $this->route = $route;
51
        $this->name  = $name;
52
        $this->enter();
53
    }
54
55
    /**
56
     * Add Matched info of route
57
     *
58
     * @param string $name
59
     * @param bool   $matched
60
     */
61
    public function setMatched(string $name, bool $matched = true): void
62
    {
63
        if (!empty($this->profiles)) {
64
            foreach ($this->profiles as $profile) {
65
                if ($name === $profile->getName()) {
66
                    $profile->matched = $matched;
67
                }
68
            }
69
70
            return;
71
        }
72
73
        if ($name === $this->name) {
74
            $this->matched = $matched;
75
        }
76
    }
77
78
    /**
79
     * @return null|RouteInterface
80
     */
81
    public function getRoute(): ?RouteInterface
82
    {
83
        return $this->route;
84
    }
85
86
    /**
87
     * @return string
88
     */
89
    public function getName(): string
90
    {
91
        return $this->name;
92
    }
93
94
    /**
95
     * @return bool
96
     */
97
    public function isRoute(): bool
98
    {
99
        return $this->route instanceof RouteInterface;
100
    }
101
102
    /**
103
     * @return bool
104
     */
105
    public function isMatched(): bool
106
    {
107
        return $this->matched;
108
    }
109
110
    /**
111
     * @return ProfileRoute[]
112
     */
113
    public function getProfiles(): array
114
    {
115
        return $this->profiles;
116
    }
117
118
    /**
119
     * Add a new profiled route
120
     *
121
     * @param ProfileRoute $profile
122
     */
123
    public function addProfile(self $profile): void
124
    {
125
        $this->profiles[] = $profile;
126
    }
127
128
    /**
129
     * Returns the duration in microseconds.
130
     *
131
     * @return float
132
     */
133
    public function getDuration(): float
134
    {
135
        if (!empty($this->profiles)) {
136
            // for the root node with children, duration is the sum of all child durations
137
            $duration = 0;
138
139
            foreach ($this->profiles as $profile) {
140
                $duration += $profile->getDuration();
141
            }
142
143
            return $duration;
144
        }
145
146
        return isset($this->ends['wt']) && isset($this->starts['wt']) ? $this->ends['wt'] - $this->starts['wt'] : 0;
147
    }
148
149
    /**
150
     * Returns the memory usage in bytes.
151
     *
152
     * @return int|float
153
     */
154
    public function getMemoryUsage()
155
    {
156
        return isset($this->ends['mu']) && isset($this->starts['mu']) ? $this->ends['mu'] - $this->starts['mu'] : 0;
157
    }
158
159
    /**
160
     * Returns the peak memory usage in bytes.
161
     *
162
     * @return int|float
163
     */
164
    public function getPeakMemoryUsage()
165
    {
166
        return isset($this->ends['pmu']) && isset($this->starts['pmu']) ? $this->ends['pmu'] - $this->starts['pmu'] : 0;
167
    }
168
169
    /**
170
     * Starts the profiling.
171
     */
172
    public function enter(): void
173
    {
174
        $this->starts = [
175
            'wt'  => \microtime(true),
176
            'mu'  => \memory_get_usage(),
177
            'pmu' => \memory_get_peak_usage(),
178
        ];
179
    }
180
181
    /**
182
     * Stops the profiling.
183
     */
184
    public function leave(): void
185
    {
186
        $this->ends = [
187
            'wt'  => \microtime(true),
188
            'mu'  => \memory_get_usage(),
189
            'pmu' => \memory_get_peak_usage(),
190
        ];
191
    }
192
193
    public function reset(): void
194
    {
195
        $this->starts = $this->ends = $this->profiles = [];
196
        $this->enter();
197
    }
198
199
    /**
200
     * {@inheritdoc}
201
     */
202
    public function getIterator(): Traversable
203
    {
204
        return new ArrayIterator($this->profiles);
205
    }
206
}
207