Created
February 8, 2019 10:14
-
-
Save fritz-gerneth/ba900ac4d5d0a63603bb6ae9f17d8253 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class LockService | |
{ | |
public function tryAcquireLocationLock( | |
LocationLock $lock, | |
IdInterface $owner, | |
?DateTimeImmutable $expirationDate | |
): bool { | |
try { | |
$affectedRows = $this->connection->insert( | |
self::TABLE_NAME, | |
[ | |
'host' => $lock->host(), | |
'path' => $lock->path(), | |
'token' => $lock->token()->__toString(), | |
'owner' => $owner->__toString(), | |
'created_at' => date('Y-m-d H:i:s'), | |
'expires_at' => !$expirationDate ? null : $expirationDate->format('Y-m-d H:i:s'), | |
] | |
); | |
} catch (UniqueConstraintViolationException $e) { | |
return false; | |
} | |
return $affectedRows !== 0; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$locationLock = $this->locationService->tryAcquireLocationLock( | |
LocationLock::fromString($host, $path, $locationLockToken), | |
$requestId, | |
$expirationDate | |
); | |
while (false === $locationLock) { | |
$proposalId = $this->proposalIdGenerator->getUniqueId(); | |
$path = $proposalId; | |
$locationLock = $this->locationService->tryAcquireLocationLock( | |
LocationLock::fromString($host, $path, $locationLockToken), | |
$requestId, | |
$expirationDate | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment