| 1 |
<?php |
| 2 |
/** |
| 3 |
* A factory to create an atomic lock instance. |
| 4 |
* |
| 5 |
* @package SolidWP\Performance |
| 6 |
*/ |
| 7 |
|
| 8 |
declare( strict_types=1 ); |
| 9 |
|
| 10 |
namespace SolidWP\Performance\Lock; |
| 11 |
|
| 12 |
use SolidWP\Performance\Container; |
| 13 |
use SolidWP\Performance\Lock\Contracts\Blockable_Lock; |
| 14 |
use SolidWP\Performance\Lock\Contracts\Lock; |
| 15 |
|
| 16 |
/** |
| 17 |
* A factory to create an atomic lock instance. |
| 18 |
* |
| 19 |
* @package SolidWP\Performance |
| 20 |
*/ |
| 21 |
final class Lock_Factory { |
| 22 |
|
| 23 |
public const RANDOM_OWNER_LENGTH = 16; |
| 24 |
|
| 25 |
/** |
| 26 |
* @var Container |
| 27 |
*/ |
| 28 |
private Container $container; |
| 29 |
|
| 30 |
/** |
| 31 |
* @param Container $container The container. |
| 32 |
*/ |
| 33 |
public function __construct( Container $container ) { |
| 34 |
$this->container = $container; |
| 35 |
} |
| 36 |
|
| 37 |
/** |
| 38 |
* Make an atomic lock instance. |
| 39 |
* |
| 40 |
* @param string $name The unique lock name. |
| 41 |
* @param int $expiration The expiration in seconds. |
| 42 |
* @param string $owner The owner of the lock. |
| 43 |
* |
| 44 |
* @return Blockable_Lock |
| 45 |
*/ |
| 46 |
public function make( string $name, int $expiration, string $owner = '' ): Blockable_Lock { |
| 47 |
if ( ! strlen( $owner ) ) { |
| 48 |
$owner = wp_generate_password( self::RANDOM_OWNER_LENGTH, false ); |
| 49 |
} |
| 50 |
|
| 51 |
$entry = new Lock_Entry( $name, $expiration, $owner ); |
| 52 |
|
| 53 |
// Configure the Lock Driver to accept the entry. |
| 54 |
$this->container->when( Lock::class ) |
| 55 |
->needs( Lock_Entry::class ) |
| 56 |
->give( $entry ); |
| 57 |
|
| 58 |
return $this->container->get( Blockable_Lock::class ); |
| 59 |
} |
| 60 |
} |
| 61 |
|