Skip to content

Instantly share code, notes, and snippets.

@fritz-gerneth
Created February 8, 2019 10:14
Show Gist options
  • Save fritz-gerneth/ba900ac4d5d0a63603bb6ae9f17d8253 to your computer and use it in GitHub Desktop.
Save fritz-gerneth/ba900ac4d5d0a63603bb6ae9f17d8253 to your computer and use it in GitHub Desktop.
<?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;
}
}
<?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