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

DefaultTimeoutChecker   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 19
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 4
eloc 6
c 1
b 0
f 1
dl 0
loc 19
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 2 1
A throwOnTimeout() 0 6 2
A start() 0 3 1
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