Lines
100.00%
10 / 10
Methods
100.00%
2 / 2
Classes
100.00%
1 / 1
| Name | Lines | Methods | CRAP | ||||
|---|---|---|---|---|---|---|---|
| __construct | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | ||
| withLock | 100.00% | 9 / 9 | 100.00% | 1 / 1 | 2 | ||
| 28 | final class SqliteLock implements Lock | |
| 29 | { | |
| 30 | /** | |
| 31 | * @param SqliteConnection $connection | |
| 32 | */ | |
| 33 | public function __construct(protected readonly SqliteConnection $connection) {} | |
| 34 | ||
| 35 | /** | |
| 36 | * Run the callback inside a write transaction. | |
| 37 | * | |
| 38 | * @template TReturn | |
| 39 | * | |
| 40 | * @param callable(): TReturn $callback | |
| 41 | * @param string $name Accepted for signature parity and ignored. | |
| 42 | * @return TReturn | |
| 43 | * @throws ConnectionException | |
| 44 | * @throws \Throwable | |
| 45 | */ | |
| 46 | #[\Override] | |
| 47 | public function withLock(callable $callback, string $name): mixed | |
| 48 | { | |
| 49 | if ($this->connection->transactionLevel() > 0) { | |
| 50 | throw new ConnectionException( | |
| 51 | 'SqliteLock requires a transaction-free connection: an open transaction ' | |
| 52 | . 'would nest as a savepoint, taking no RESERVED lock and serializing nothing. ' | |
| 53 | . 'Commit or roll back the outer transaction before acquiring the lock.', | |
| 54 | ); | |
| 55 | } | |
| 56 | ||
| 57 | return $this->connection->transaction(function () use ($callback): mixed { | |
| 58 | return $callback(); | |
| 59 | }); | |
| 60 | } | |
| 61 | } |