Passed
Pull Request — master (#629)
by
unknown
14:43
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
class DefaultTimeoutChecker implements TimeoutChecker
15
{
16
    private float $startTime;
17
18
    public function __construct(readonly private float $timeout)
19
    {
20
    }
21
22
    public function start(?float $startTime = null): void
23
    {
24
        $this->startTime = $startTime ?? \microtime(true);
25
    }
26
27
    public function throwOnTimeout(?float $currentTime = null, string $message = 'Exceeded the timeout'): void
28
    {
29
        $spentTime = ($currentTime ?? \microtime(true)) - $this->startTime;
30
        $isTimeout = $spentTime >= $this->timeout;
31
        if ($isTimeout) {
32
            throw new TimeoutException($message, $spentTime, $this->timeout);
33
        }
34
    }
35
}
36