Skip to content

Commit

Permalink
Merge pull request #10 from aternosorg/set-identifier
Browse files Browse the repository at this point in the history
add option to set identifier in construct and with function
  • Loading branch information
matthi4s authored Jan 14, 2025
2 parents afcded8 + e77a2b9 commit 74a0d02
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 11 deletions.
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Copyright (c) 2019-2024 Aternos GmbH
Copyright (c) 2019-2025 Aternos GmbH

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
35 changes: 28 additions & 7 deletions src/Lock.php
Original file line number Diff line number Diff line change
Expand Up @@ -244,11 +244,22 @@ public static function setDelayPerUnavailableRetry(int $delayPerRetry): void
* Create a lock
*
* @param string $key Can be anything, should describe the resource in a unique way
* @param string|null $identifier An identifier for this lock, falls back to the default identifier if null
*/
public function __construct(string $key)
public function __construct(string $key, ?string $identifier = null)
{
$this->key = $key;
$this->etcdKey = static::$prefix . $this->key;

if (static::$defaultIdentifier === null) {
static::setDefaultIdentifier();
}

if ($identifier === null) {
$this->identifier = static::$defaultIdentifier;
} else {
$this->identifier = $identifier;
}
}

/**
Expand All @@ -267,12 +278,7 @@ public function lock(bool $exclusive = false, int $time = 120, int $wait = 300,
$this->exclusive = $exclusive;
$this->time = $time;

if (static::$defaultIdentifier === null) {
static::setDefaultIdentifier();
}
if ($identifier === null) {
$this->identifier = static::$defaultIdentifier;
} else {
if ($identifier !== null) {
$this->identifier = $identifier;
}

Expand Down Expand Up @@ -326,6 +332,21 @@ public function isLocked(): bool|int
return false;
}

/**
* Set the identifier for this lock, falls back to the default identifier if null
*
* @param string|null $identifier
* @return void
*/
public function setIdentifier(?string $identifier): void
{
if ($identifier === null) {
$this->identifier = static::$defaultIdentifier;
} else {
$this->identifier = $identifier;
}
}

/**
* Get the used identifier for this lock
*
Expand Down
23 changes: 20 additions & 3 deletions test/LockTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,25 @@ public function testCreateLock(): void
$lock->break();
}

public function testConstructLockWithDefaultIdentifier(): void
{
$lock = new Lock("key");
$this->assertIsString($lock->getIdentifier());
}

public function testConstructLockWithIdentifier(): void
{
$lock = new Lock("key", "identifier");
$this->assertEquals("identifier", $lock->getIdentifier());
}

public function testSetIdentifier(): void
{
$lock = new Lock("key");
$lock->setIdentifier("identifier");
$this->assertEquals("identifier", $lock->getIdentifier());
}

/**
* @throws InvalidResponseStatusCodeException
* @throws TooManySaveRetriesException
Expand Down Expand Up @@ -327,16 +346,14 @@ public function testLockDeleteConflict(): void
*/
public function testLockFunctionUsesUniqueDefaultIdentifierIfNoIdentifierParameterIsProvided(): void
{
$lock = new Lock($this->getRandomString());

# Default identifier is not set explicitly and its default value is null.
# (We have to set it to null manually because other tests might have set the default identifier before.)
$reflection = new \ReflectionProperty(Lock::class, 'defaultIdentifier');
$reflection->setAccessible(true);
$reflection->setValue(null);
$this->assertNull($reflection->getValue());

# lock() sets the default identifier to an uniqid()
$lock = new Lock($this->getRandomString());
$lock->lock();
$defaultIdentifier = $reflection->getValue();
$this->assertIsString($defaultIdentifier);
Expand Down

0 comments on commit 74a0d02

Please sign in to comment.