| 1 |
<?php |
| 2 |
|
| 3 |
declare (strict_types=1); |
| 4 |
namespace WindPress\WindPress\Core\Scanner; |
| 5 |
|
| 6 |
use RuntimeException; |
| 7 |
class BuildState |
| 8 |
{ |
| 9 |
private const OPTION = 'windpress_scan_build_state'; |
| 10 |
public static function get(): array |
| 11 |
{ |
| 12 |
$state = get_option(self::OPTION, \false); |
| 13 |
if (!is_array($state) || empty($state['generation'])) { |
| 14 |
$initial = ['generation' => wp_generate_uuid4(), 'last_full_build' => null]; |
| 15 |
if ($state === \false) { |
| 16 |
add_option(self::OPTION, $initial, '', \false); |
| 17 |
} else { |
| 18 |
update_option(self::OPTION, $initial, \false); |
| 19 |
} |
| 20 |
$state = get_option(self::OPTION, \false); |
| 21 |
if (!is_array($state) || empty($state['generation'])) { |
| 22 |
throw new RuntimeException(__('Unable to initialize the scan generation.', 'windpress')); |
| 23 |
} |
| 24 |
} |
| 25 |
return $state; |
| 26 |
} |
| 27 |
public static function matches(string $generation): bool |
| 28 |
{ |
| 29 |
return hash_equals(self::get()['generation'], $generation); |
| 30 |
} |
| 31 |
public static function complete_full_build(): void |
| 32 |
{ |
| 33 |
$state = ['generation' => wp_generate_uuid4(), 'last_full_build' => (int) round(microtime(\true) * 1000)]; |
| 34 |
if (!update_option(self::OPTION, $state, \false)) { |
| 35 |
throw new RuntimeException(__('Unable to save the scan generation.', 'windpress')); |
| 36 |
} |
| 37 |
} |
| 38 |
public static function acquire(): ?string |
| 39 |
{ |
| 40 |
return \WindPress\WindPress\Core\Scanner\ScanLock::acquire('cache-publication'); |
| 41 |
} |
| 42 |
public static function release(string $token): void |
| 43 |
{ |
| 44 |
\WindPress\WindPress\Core\Scanner\ScanLock::release('cache-publication', $token); |
| 45 |
} |
| 46 |
} |
| 47 |
|