Passed
Pull Request — master (#629)
by
unknown
14:43
created

DefaultTimeoutChecker   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 19
Duplicated Lines 0 %

Importance

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

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
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