Skip to content

Commit bbc13cf

Browse files
committed
Rewrite locker API
1 parent 7b7d502 commit bbc13cf

File tree

8 files changed

+63
-63
lines changed

8 files changed

+63
-63
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ composer require cybercog/php-db-locker
3535
$dbConnection = new PDO($dsn, $username, $password);
3636

3737
$locker = new \Cog\DbLocker\Postgres\PostgresAdvisoryLocker();
38-
$lockId = \Cog\DbLocker\Postgres\PostgresLockId::fromKeyValue('user', '4');
38+
$lockId = \Cog\DbLocker\Postgres\PostgresLockKey::create('user', '4');
3939

4040
$dbConnection->beginTransaction();
4141
$lock = $locker->acquireSessionLevelLockHandler(
@@ -58,7 +58,7 @@ $dbConnection->commit();
5858
$dbConnection = new PDO($dsn, $username, $password);
5959

6060
$locker = new \Cog\DbLocker\Postgres\PostgresAdvisoryLocker();
61-
$lockId = \Cog\DbLocker\Postgres\PostgresLockId::fromKeyValue('user', '4');
61+
$lockId = \Cog\DbLocker\Postgres\PostgresLockKey::create('user', '4');
6262

6363
try {
6464
$lock = $locker->acquireSessionLevelLockHandler(

src/Postgres/LockHandle/PostgresSessionLevelLockHandle.php renamed to src/Postgres/LockHandle/SessionLevelLockHandle.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,20 @@
1515

1616
use Cog\DbLocker\Postgres\Enum\PostgresLockAccessModeEnum;
1717
use Cog\DbLocker\Postgres\PostgresAdvisoryLocker;
18-
use Cog\DbLocker\Postgres\PostgresLockId;
18+
use Cog\DbLocker\Postgres\PostgresLockKey;
1919
use PDO;
2020

2121
/**
2222
* @internal
2323
*/
24-
final class PostgresSessionLevelLockHandle
24+
final class SessionLevelLockHandle
2525
{
2626
private bool $isReleased = false;
2727

2828
public function __construct(
2929
private readonly PDO $dbConnection,
3030
private readonly PostgresAdvisoryLocker $locker,
31-
public readonly PostgresLockId $lockId,
31+
public readonly PostgresLockKey $lockId,
3232
public readonly PostgresLockAccessModeEnum $accessMode,
3333
public readonly bool $wasAcquired,
3434
) {}

src/Postgres/LockHandle/PostgresTransactionLevelLockHandle.php renamed to src/Postgres/LockHandle/TransactionLevelLockHandle.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
/**
1717
* @internal
1818
*/
19-
final class PostgresTransactionLevelLockHandle
19+
final class TransactionLevelLockHandle
2020
{
2121
public function __construct(
2222
public readonly bool $wasAcquired,

src/Postgres/PostgresAdvisoryLocker.php

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616
use Cog\DbLocker\Postgres\Enum\PostgresLockAccessModeEnum;
1717
use Cog\DbLocker\Postgres\Enum\PostgresLockLevelEnum;
1818
use Cog\DbLocker\Postgres\Enum\PostgresLockWaitModeEnum;
19-
use Cog\DbLocker\Postgres\LockHandle\PostgresSessionLevelLockHandle;
20-
use Cog\DbLocker\Postgres\LockHandle\PostgresTransactionLevelLockHandle;
19+
use Cog\DbLocker\Postgres\LockHandle\SessionLevelLockHandle;
20+
use Cog\DbLocker\Postgres\LockHandle\TransactionLevelLockHandle;
2121
use LogicException;
2222
use PDO;
2323

@@ -30,11 +30,11 @@ final class PostgresAdvisoryLocker
3030
*/
3131
public function acquireTransactionLevelLockHandler(
3232
PDO $dbConnection,
33-
PostgresLockId $postgresLockId,
33+
PostgresLockKey $postgresLockId,
3434
PostgresLockWaitModeEnum $waitMode = PostgresLockWaitModeEnum::NonBlocking,
3535
PostgresLockAccessModeEnum $accessMode = PostgresLockAccessModeEnum::Exclusive,
36-
): PostgresTransactionLevelLockHandle {
37-
return new PostgresTransactionLevelLockHandle(
36+
): TransactionLevelLockHandle {
37+
return new TransactionLevelLockHandle(
3838
wasAcquired: $this->acquireLock(
3939
$dbConnection,
4040
$postgresLockId,
@@ -50,7 +50,7 @@ public function acquireTransactionLevelLockHandler(
5050
*/
5151
public function acquireTransactionLevelLock(
5252
PDO $dbConnection,
53-
PostgresLockId $postgresLockId,
53+
PostgresLockKey $postgresLockId,
5454
PostgresLockWaitModeEnum $waitMode = PostgresLockWaitModeEnum::NonBlocking,
5555
PostgresLockAccessModeEnum $accessMode = PostgresLockAccessModeEnum::Exclusive,
5656
): bool {
@@ -71,11 +71,11 @@ public function acquireTransactionLevelLock(
7171
*/
7272
public function acquireSessionLevelLockHandler(
7373
PDO $dbConnection,
74-
PostgresLockId $postgresLockId,
74+
PostgresLockKey $postgresLockId,
7575
PostgresLockWaitModeEnum $waitMode = PostgresLockWaitModeEnum::NonBlocking,
7676
PostgresLockAccessModeEnum $accessMode = PostgresLockAccessModeEnum::Exclusive,
77-
): PostgresSessionLevelLockHandle {
78-
return new PostgresSessionLevelLockHandle(
77+
): SessionLevelLockHandle {
78+
return new SessionLevelLockHandle(
7979
$dbConnection,
8080
$this,
8181
$postgresLockId,
@@ -97,7 +97,7 @@ public function acquireSessionLevelLockHandler(
9797
*/
9898
public function acquireSessionLevelLock(
9999
PDO $dbConnection,
100-
PostgresLockId $postgresLockId,
100+
PostgresLockKey $postgresLockId,
101101
PostgresLockWaitModeEnum $waitMode = PostgresLockWaitModeEnum::NonBlocking,
102102
PostgresLockAccessModeEnum $accessMode = PostgresLockAccessModeEnum::Exclusive,
103103
): bool {
@@ -115,7 +115,7 @@ public function acquireSessionLevelLock(
115115
*/
116116
public function releaseSessionLevelLock(
117117
PDO $dbConnection,
118-
PostgresLockId $postgresLockId,
118+
PostgresLockKey $postgresLockId,
119119
PostgresLockAccessModeEnum $accessMode = PostgresLockAccessModeEnum::Exclusive,
120120
): bool {
121121
$sql = match ($accessMode) {
@@ -151,7 +151,7 @@ public function releaseAllSessionLevelLocks(
151151

152152
private function acquireLock(
153153
PDO $dbConnection,
154-
PostgresLockId $postgresLockId,
154+
PostgresLockKey $postgresLockId,
155155
PostgresLockLevelEnum $level,
156156
PostgresLockWaitModeEnum $waitMode = PostgresLockWaitModeEnum::NonBlocking,
157157
PostgresLockAccessModeEnum $accessMode = PostgresLockAccessModeEnum::Exclusive,

src/Postgres/PostgresLockId.php renamed to src/Postgres/PostgresLockKey.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
use InvalidArgumentException;
1717

18-
final class PostgresLockId
18+
final class PostgresLockKey
1919
{
2020
private const DB_INT32_VALUE_MIN = -2_147_483_648;
2121
private const DB_INT32_VALUE_MAX = 2_147_483_647;
@@ -39,20 +39,20 @@ private function __construct(
3939
}
4040
}
4141

42-
public static function fromKeyValue(
43-
string $key,
42+
public static function create(
43+
string $namespace,
4444
string $value = '',
4545
): self {
4646
return new self(
47-
classId: self::convertStringToSignedInt32($key),
47+
classId: self::convertStringToSignedInt32($namespace),
4848
objectId: self::convertStringToSignedInt32($value),
4949
// TODO: Do we need to sanitize it?
5050
// TODO: Do we need to omit ":" on end if no value is passed
51-
humanReadableValue: "$key:$value",
51+
humanReadableValue: "$namespace:$value",
5252
);
5353
}
5454

55-
public static function fromIntKeys(
55+
public static function createFromInternalIds(
5656
int $classId,
5757
int $objectId,
5858
): self {

test/Integration/AbstractIntegrationTestCase.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
namespace Cog\Test\DbLocker\Integration;
1515

1616
use Cog\DbLocker\Postgres\Enum\PostgresLockAccessModeEnum;
17-
use Cog\DbLocker\Postgres\PostgresLockId;
17+
use Cog\DbLocker\Postgres\PostgresLockKey;
1818
use PDO;
1919
use PHPUnit\Framework\TestCase;
2020

@@ -44,7 +44,7 @@ protected function initPostgresPdoConnection(): PDO
4444

4545
protected function assertPgAdvisoryLockExistsInConnection(
4646
PDO $dbConnection,
47-
PostgresLockId $postgresLockId,
47+
PostgresLockKey $postgresLockId,
4848
PostgresLockAccessModeEnum $mode = PostgresLockAccessModeEnum::Exclusive,
4949
): void {
5050
$row = $this->findPostgresAdvisoryLockInConnection(
@@ -63,7 +63,7 @@ protected function assertPgAdvisoryLockExistsInConnection(
6363

6464
protected function assertPgAdvisoryLockMissingInConnection(
6565
PDO $dbConnection,
66-
PostgresLockId $postgresLockId,
66+
PostgresLockKey $postgresLockId,
6767
PostgresLockAccessModeEnum $mode = PostgresLockAccessModeEnum::Exclusive,
6868
): void {
6969
$row = $this->findPostgresAdvisoryLockInConnection(
@@ -95,7 +95,7 @@ protected function assertPgAdvisoryLocksCount(
9595

9696
private function findPostgresAdvisoryLockInConnection(
9797
PDO $dbConnection,
98-
PostgresLockId $postgresLockId,
98+
PostgresLockKey $postgresLockId,
9999
PostgresLockAccessModeEnum $mode,
100100
): object | null {
101101
$statement = $dbConnection->prepare(

0 commit comments

Comments
 (0)