| 1 |
<?php |
| 2 |
/** |
| 3 |
* The State Entry Object. Use the State object to manipulate this. |
| 4 |
* |
| 5 |
* @package SolidWP\Performance |
| 6 |
*/ |
| 7 |
|
| 8 |
declare( strict_types=1 ); |
| 9 |
|
| 10 |
namespace SolidWP\Performance\Preload\State; |
| 11 |
|
| 12 |
use InvalidArgumentException; |
| 13 |
use SolidWP\Performance\Preload\State\Enums\Source; |
| 14 |
use SolidWP\Performance\Preload\State\Enums\Status; |
| 15 |
|
| 16 |
/** |
| 17 |
* This object represents the current state of the preloader |
| 18 |
* and is stored, retrieved and modified as the preloader changes. |
| 19 |
* |
| 20 |
* @see State |
| 21 |
* |
| 22 |
* @package SolidWP\Performance |
| 23 |
*/ |
| 24 |
final class State_Entry { |
| 25 |
|
| 26 |
/** |
| 27 |
* The unique ID of the current preloader. |
| 28 |
* |
| 29 |
* @var string |
| 30 |
*/ |
| 31 |
public string $id; |
| 32 |
|
| 33 |
/** |
| 34 |
* The source that started the preloader. |
| 35 |
* |
| 36 |
* @see Source |
| 37 |
* |
| 38 |
* @var string |
| 39 |
*/ |
| 40 |
public string $source; |
| 41 |
|
| 42 |
/** |
| 43 |
* The current status of the preloader. |
| 44 |
* |
| 45 |
* @see Status |
| 46 |
* |
| 47 |
* @var string |
| 48 |
*/ |
| 49 |
public string $status; |
| 50 |
|
| 51 |
/** |
| 52 |
* Whether we are force preloading the entire site. |
| 53 |
* |
| 54 |
* @var bool |
| 55 |
*/ |
| 56 |
public bool $force = false; |
| 57 |
|
| 58 |
/** |
| 59 |
* The human-readable duration set when the preloader is complete. |
| 60 |
* |
| 61 |
* @var string|null |
| 62 |
*/ |
| 63 |
public ?string $duration; |
| 64 |
|
| 65 |
/** |
| 66 |
* @param string $id The unique ID of the current preloader. |
| 67 |
* @param string $source The source that started the preloader, e.g. web/cli. |
| 68 |
* @param string $status The current status of the preloader. |
| 69 |
* @param bool $force Whether we are force preloading the entire site. |
| 70 |
* @param string|null $duration The human-readable duration set when the preloader is complete. |
| 71 |
* |
| 72 |
* @throws InvalidArgumentException If an invalid id, source or status is provided. |
| 73 |
*/ |
| 74 |
public function __construct( |
| 75 |
string $id, |
| 76 |
string $source, |
| 77 |
string $status, |
| 78 |
bool $force = false, |
| 79 |
?string $duration = null |
| 80 |
) { |
| 81 |
if ( empty( $id ) ) { |
| 82 |
throw new InvalidArgumentException( 'The $id argument cannot be empty.' ); |
| 83 |
} |
| 84 |
|
| 85 |
if ( ! Source::is_valid( $source ) ) { |
| 86 |
throw new InvalidArgumentException( 'Invalid $source argument.' ); |
| 87 |
} |
| 88 |
|
| 89 |
if ( ! Status::is_valid( $status ) ) { |
| 90 |
throw new InvalidArgumentException( 'Invalid $status argument.' ); |
| 91 |
} |
| 92 |
|
| 93 |
$this->id = $id; |
| 94 |
$this->source = $source; |
| 95 |
$this->status = $status; |
| 96 |
$this->force = $force; |
| 97 |
$this->duration = $duration; |
| 98 |
} |
| 99 |
} |
| 100 |
|