Passed
Push — master ( 020179...2cb551 )
by Doug
11:37
created

DefaultTimeoutChecker::throwOnTimeout()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 2
eloc 4
c 1
b 0
f 1
nc 2
nop 2
dl 0
loc 6
rs 10
1
<?php
2
3
/**
4
 * Box packing (3D bin packing, knapsack problem).
5
 *
6
 * @author Doug Wright
7
 */
8
declare(strict_types=1);
9
10
namespace DVDoug\BoxPacker;
11
12
use DVDoug\BoxPacker\Exception\TimeoutException;
13
14
use function microtime;
15
16
class DefaultTimeoutChecker implements TimeoutChecker
17
{
18
    private float $startTime;
19
20
    public function __construct(readonly private float $timeout)
21
    {
22
    }
23
24
    public function start(?float $startTime = null): void
25
    {
26
        $this->startTime = $startTime ?? microtime(true);
27
    }
28
29
    public function throwOnTimeout(?float $currentTime = null, string $message = 'Exceeded the timeout'): void
30
    {
31
        $spentTime = ($currentTime ?? microtime(true)) - $this->startTime;
32
        $isTimeout = $spentTime >= $this->timeout;
33
        if ($isTimeout) {
34
            throw new TimeoutException($message, $spentTime, $this->timeout);
35
        }
36
    }
37
}
38