| 1 |
<?php |
| 2 |
/** |
| 3 |
* Manages the ongoing state of the preloader. |
| 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 |
use SolidWP\Performance\Storage\Contracts\Storage; |
| 16 |
use SolidWP\Performance\Storage\Exceptions\InvalidKeyException; |
| 17 |
use SolidWP\Performance\Timer\Exceptions\InvalidTimerNameException; |
| 18 |
use SolidWP\Performance\Timer\Exceptions\NoActiveTimerException; |
| 19 |
use SolidWP\Performance\Timer\Timer; |
| 20 |
|
| 21 |
/** |
| 22 |
* Manages the ongoing state of the preloader. |
| 23 |
* |
| 24 |
* @package SolidWP\Performance |
| 25 |
*/ |
| 26 |
class State { |
| 27 |
|
| 28 |
private const STORAGE_KEY = 'swpsp_preload_state'; |
| 29 |
private const TIMER_NAME = 'preload_timer'; |
| 30 |
public const ID_PREFIX = 'swpsp_preload'; |
| 31 |
|
| 32 |
/** |
| 33 |
* @var Storage |
| 34 |
*/ |
| 35 |
private Storage $storage; |
| 36 |
|
| 37 |
/** |
| 38 |
* @var Timer |
| 39 |
*/ |
| 40 |
private Timer $timer; |
| 41 |
|
| 42 |
/** |
| 43 |
* @param Storage $storage The Storage System. |
| 44 |
* @param Timer $timer The timer. |
| 45 |
*/ |
| 46 |
public function __construct( Storage $storage, Timer $timer ) { |
| 47 |
$this->storage = $storage; |
| 48 |
$this->timer = $timer; |
| 49 |
} |
| 50 |
|
| 51 |
/** |
| 52 |
* Mark the preloader as started. |
| 53 |
* |
| 54 |
* @param string $source The source that started the preloader. |
| 55 |
* @param bool $force Whether we are force preloading the entire site. |
| 56 |
* |
| 57 |
* @throws InvalidArgumentException If an invalid source is provided. |
| 58 |
* @return void |
| 59 |
*/ |
| 60 |
public function start( string $source, bool $force = false ) { |
| 61 |
if ( ! Source::is_valid( $source ) ) { |
| 62 |
throw new InvalidArgumentException( 'Invalid $source argument' ); |
| 63 |
} |
| 64 |
|
| 65 |
$this->timer->start( self::TIMER_NAME ); |
| 66 |
|
| 67 |
$entry = new State_Entry( $this->id(), $source, Status::RUNNING, $force ); |
| 68 |
|
| 69 |
$this->set( $entry ); |
| 70 |
} |
| 71 |
|
| 72 |
/** |
| 73 |
* Get the state entry. |
| 74 |
* |
| 75 |
* @throws InvalidKeyException If an invalid storage key is used. |
| 76 |
* @throws InvalidArgumentException If an invalid source or status is provided. |
| 77 |
* |
| 78 |
* @return State_Entry |
| 79 |
*/ |
| 80 |
public function get(): State_Entry { |
| 81 |
return $this->storage->get( self::STORAGE_KEY ) ?? new State_Entry( |
| 82 |
$this->id(), |
| 83 |
Source::detect_source(), |
| 84 |
Status::IDLE |
| 85 |
); |
| 86 |
} |
| 87 |
|
| 88 |
/** |
| 89 |
* Mark the preloader as canceled. |
| 90 |
* |
| 91 |
* @throws InvalidArgumentException If an invalid source or status is provided. |
| 92 |
* @throws InvalidKeyException If an invalid storage key is used. |
| 93 |
* |
| 94 |
* @return bool |
| 95 |
*/ |
| 96 |
public function cancel(): bool { |
| 97 |
$this->timer->clear( self::TIMER_NAME ); |
| 98 |
|
| 99 |
$entry = $this->get(); |
| 100 |
|
| 101 |
$entry->status = Status::CANCELED; |
| 102 |
$entry->source = Source::detect_source(); |
| 103 |
|
| 104 |
return $this->set( $entry ); |
| 105 |
} |
| 106 |
|
| 107 |
/** |
| 108 |
* Mark the preloader as complete. |
| 109 |
* |
| 110 |
* @throws InvalidArgumentException If an invalid source or status is provided. |
| 111 |
* @throws InvalidKeyException If an invalid storage key is used. |
| 112 |
* @throws InvalidTimerNameException If an invalid timer name is provided. |
| 113 |
* @throws NoActiveTimerException If the timer hasn't been started yet. |
| 114 |
* |
| 115 |
* @return bool |
| 116 |
*/ |
| 117 |
public function complete(): bool { |
| 118 |
$entry = $this->get(); |
| 119 |
|
| 120 |
$entry->status = Status::COMPLETED; |
| 121 |
$entry->duration = $this->timer->stop( self::TIMER_NAME )->format(); |
| 122 |
|
| 123 |
return $this->set( $entry ); |
| 124 |
} |
| 125 |
|
| 126 |
/** |
| 127 |
* Mark a preloader as failed. |
| 128 |
* |
| 129 |
* @throws InvalidArgumentException If an invalid source or status is provided. |
| 130 |
* @throws InvalidKeyException If an invalid storage key is used. |
| 131 |
* |
| 132 |
* @return bool |
| 133 |
*/ |
| 134 |
public function fail(): bool { |
| 135 |
$this->timer->clear( self::TIMER_NAME ); |
| 136 |
|
| 137 |
$entry = $this->get(); |
| 138 |
|
| 139 |
$entry->status = Status::FAILED; |
| 140 |
|
| 141 |
return $this->set( $entry ); |
| 142 |
} |
| 143 |
|
| 144 |
/** |
| 145 |
* Set the current state. |
| 146 |
* |
| 147 |
* @param State_Entry $entry The state entry. |
| 148 |
* |
| 149 |
* @throws InvalidKeyException If an invalid storage key is provided. |
| 150 |
* |
| 151 |
* @return bool |
| 152 |
*/ |
| 153 |
private function set( State_Entry $entry ): bool { |
| 154 |
return $this->storage->set( self::STORAGE_KEY, $entry ); |
| 155 |
} |
| 156 |
|
| 157 |
/** |
| 158 |
* Generate a unique ID for the current preloader. |
| 159 |
* |
| 160 |
* @return string |
| 161 |
*/ |
| 162 |
private function id(): string { |
| 163 |
return uniqid( self::ID_PREFIX ); |
| 164 |
} |
| 165 |
} |
| 166 |
|