Skip to content

Instantly share code, notes, and snippets.

@non-senses
Created January 30, 2020 19:09
Show Gist options
  • Save non-senses/1d4541a20e51651f0723363d87e31e5f to your computer and use it in GitHub Desktop.
Save non-senses/1d4541a20e51651f0723363d87e31e5f to your computer and use it in GitHub Desktop.
<?php
/**
* Unlock a specific type of order. Unusually unlock after the confirm has been completed
*
* @param int $orderId Id of the object you unlock
* @return bool True on success
* @throws LockException
* @throws \Exception
*/
public function unlock($orderId, $userId)
{
$redis = $this->app['cache'];
if (!empty($this->type) && !empty($orderId) && in_array($this->type, $this->availableType)) {
$result = $redis->hGet(sprintf(self::REDIS_CACHE_TEMPORARY_LOCK, $this->type), $orderId);
if (!empty($result)) {
// lock if it's the same user who try to unlock
$data = json_decode($result, true);
if ($userId == $data['user']['id']) {
$result = $redis->hDel(sprintf(self::REDIS_CACHE_TEMPORARY_LOCK, $this->type), $orderId);
if ($result == true) {
return true;
} else {
throw new LockException("Unlock failed. Please try again");
}
} else {
throw new LockException("Unlock failed. You can't unlock something that isn't yours");
}
} else {
throw new LockException("This order wasn't locked. Can't unlock");
}
} else {
throw new LockException("Missing/Invalid parameters. Can't unlock for type: " . $this->type . " with id: " . $orderId);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment