forked from dvdoug/BoxPacker
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
143 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<?php | ||
|
||
/** | ||
* Box packing (3D bin packing, knapsack problem). | ||
* | ||
* @author Doug Wright | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace DVDoug\BoxPacker; | ||
|
||
use DVDoug\BoxPacker\Exception\TimeoutException; | ||
|
||
class DefaultTimeoutChecker implements TimeoutChecker | ||
{ | ||
private float $startTime; | ||
|
||
public function __construct(readonly private float $timeout) | ||
{ | ||
} | ||
|
||
public function start(?float $startTime = null): void | ||
{ | ||
$this->startTime = $startTime ?? \microtime(true); | ||
} | ||
|
||
public function throwOnTimeout(?float $currentTime = null, string $message = 'Exceeded the timeout'): void | ||
{ | ||
$spentTime = ($currentTime ?? \microtime(true)) - $this->startTime; | ||
$isTimeout = $spentTime >= $this->timeout; | ||
if ($isTimeout) { | ||
throw new TimeoutException($message, $spentTime, $this->timeout); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
|
||
/** | ||
* Box packing (3D bin packing, knapsack problem). | ||
* | ||
* @author Doug Wright | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace DVDoug\BoxPacker\Exception; | ||
|
||
use RuntimeException; | ||
|
||
/** | ||
* Exception used when the timeout occurred | ||
*/ | ||
class TimeoutException extends RuntimeException | ||
{ | ||
public function __construct(string $message, private readonly float $spentTime, private readonly float $timeout) | ||
{ | ||
parent::__construct($message); | ||
} | ||
|
||
public function getTimeout(): float | ||
{ | ||
return $this->timeout; | ||
} | ||
|
||
public function getSpentTime(): float | ||
{ | ||
return $this->spentTime; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php | ||
|
||
/** | ||
* Box packing (3D bin packing, knapsack problem). | ||
* | ||
* @author Doug Wright | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace DVDoug\BoxPacker; | ||
|
||
use DVDoug\BoxPacker\Exception\TimeoutException; | ||
|
||
interface TimeoutChecker | ||
{ | ||
public function __construct(float $timeout); | ||
|
||
public function start(?float $startTime = null): void; | ||
|
||
/** | ||
* @throws TimeoutException | ||
*/ | ||
public function throwOnTimeout(?float $currentTime = null, string $message = 'Exceeded the timeout'): void; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters