| 1 |
<?php |
| 2 |
/** |
| 3 |
* Manages reading and writing cache meta. |
| 4 |
* |
| 5 |
* @package SolidWP\Performance |
| 6 |
*/ |
| 7 |
|
| 8 |
declare( strict_types=1 ); |
| 9 |
|
| 10 |
namespace SolidWP\Performance\Page_Cache\Meta; |
| 11 |
|
| 12 |
use InvalidArgumentException; |
| 13 |
use SolidWP\Performance\Page_Cache\Meta\Contracts\Meta_Reader; |
| 14 |
use SolidWP\Performance\Page_Cache\Meta\Contracts\Meta_Writer; |
| 15 |
use SolidWP\Performance\Page_Cache\Meta\Exceptions\MetadataNotWritableException; |
| 16 |
use Throwable; |
| 17 |
|
| 18 |
/** |
| 19 |
* Manages reading and writing cache meta. |
| 20 |
* |
| 21 |
* @package SolidWP\Performance |
| 22 |
*/ |
| 23 |
final class Meta_Manager { |
| 24 |
|
| 25 |
/** |
| 26 |
* @var Meta_Reader |
| 27 |
*/ |
| 28 |
private Meta_Reader $reader; |
| 29 |
|
| 30 |
/** |
| 31 |
* @var Meta_Writer |
| 32 |
*/ |
| 33 |
private Meta_Writer $writer; |
| 34 |
|
| 35 |
/** |
| 36 |
* @var Meta_Factory |
| 37 |
*/ |
| 38 |
private Meta_Factory $factory; |
| 39 |
|
| 40 |
/** |
| 41 |
* @param Meta_Reader $reader The Meta reader. |
| 42 |
* @param Meta_Writer $writer The Meta writer. |
| 43 |
* @param Meta_Factory $factory The Meta factory. |
| 44 |
*/ |
| 45 |
public function __construct( Meta_Reader $reader, Meta_Writer $writer, Meta_Factory $factory ) { |
| 46 |
$this->reader = $reader; |
| 47 |
$this->writer = $writer; |
| 48 |
$this->factory = $factory; |
| 49 |
} |
| 50 |
|
| 51 |
/** |
| 52 |
* Read the stored metadata from the reader. |
| 53 |
* |
| 54 |
* @param string $url The URL associated with the meta. |
| 55 |
* |
| 56 |
* @return Meta|null |
| 57 |
*/ |
| 58 |
public function read( string $url ): ?Meta { |
| 59 |
try { |
| 60 |
return $this->reader->read( $url ); |
| 61 |
} catch ( Throwable $e ) { |
| 62 |
return null; |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
/** |
| 67 |
* Capture the Meta for a URL and save it. |
| 68 |
* |
| 69 |
* @param string $url The URL associated with the meta. |
| 70 |
* |
| 71 |
* @throws MetadataNotWritableException If we can't write the metadata. |
| 72 |
* @throws InvalidArgumentException If the $url is empty. |
| 73 |
* |
| 74 |
* @return void |
| 75 |
*/ |
| 76 |
public function save( string $url ): void { |
| 77 |
$meta = $this->factory->make( $url ); |
| 78 |
|
| 79 |
$this->writer->write( $meta ); |
| 80 |
} |
| 81 |
|
| 82 |
/** |
| 83 |
* Delete the current Meta's storage data. |
| 84 |
* |
| 85 |
* @param string $url The URL associated with the meta. |
| 86 |
* |
| 87 |
* @throws InvalidArgumentException If the $url is empty. |
| 88 |
* |
| 89 |
* @return void |
| 90 |
*/ |
| 91 |
public function delete( string $url ): void { |
| 92 |
$meta = $this->factory->make( $url ); |
| 93 |
|
| 94 |
$this->writer->delete( $meta ); |
| 95 |
} |
| 96 |
} |
| 97 |
|