Created
January 30, 2020 19:00
-
-
Save non-senses/6b37d7cc25a0041b21c53f831ac949ff to your computer and use it in GitHub Desktop.
Guard Clauses Snippets
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 | |
/** | |
* 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); | |
} | |
} |
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 | |
public function unlock($orderId, $userId) | |
{ | |
$redis = $this->app['cache']; | |
if (empty($this->type) || empty($orderId) || !in_array($this->type, $this->availableType)) { | |
throw new LockException("Missing/Invalid parameters. Can't unlock for type: " . $this->type . " with id: " . $orderId); | |
} | |
$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"); | |
} | |
} |
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 | |
if (!empty($result)) { | |
/* multiple lines, including the reason to be of the function */ | |
} else { | |
throw new LockException("This order wasn't locked. Can't unlock"); | |
} |
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 | |
if (empty($result)) { | |
throw new LockException("This order wasn't locked. Can't unlock"); | |
} |
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 | |
public function unlock($orderId, $userId) | |
{ | |
$redis = $this->app['cache']; | |
if (empty($this->type) || empty($orderId) || !in_array($this->type, $this->availableType)) { | |
throw new LockException("Missing/Invalid parameters. Can't unlock for type: " . $this->type . " with id: " . $orderId); | |
} | |
$result = $redis->hGet(sprintf(self::REDIS_CACHE_TEMPORARY_LOCK, $this->type), $orderId); | |
if (empty($result)) { | |
throw new LockException("This order wasn't locked. Can't unlock"); | |
} | |
// lock if it's the same user who try to unlock | |
$data = json_decode($result, true); | |
if ($userId != $data['user']['id']) { | |
throw new LockException("Unlock failed. You can't unlock something that isn't yours"); | |
} | |
$result = $redis->hDel(sprintf(self::REDIS_CACHE_TEMPORARY_LOCK, $this->type), $orderId); | |
if ($result != true) { | |
throw new LockException("Unlock failed. Please try again"); | |
} | |
return true; | |
} |
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 | |
public function unlock($orderId, $userId) | |
{ | |
if (empty($this->type)) { | |
throw new LockException("Can not unlock the order: type is mandatory."); | |
} | |
if (empty($orderId)) { | |
throw new LockException("Can not unlock the order: no orderId was provided"); | |
} | |
if (!in_array($this->type, $this->availableType)) { | |
throw new LockException("Can not unlock the order: the provided type (".$this->type.") is none of " . implode($this->availableType)); | |
} | |
$redis = $this->app['cache']; // Assign the variable once all validations passed. | |
$result = $redis->hGet(sprintf(self::REDIS_CACHE_TEMPORARY_LOCK, $this->type), $orderId); | |
if (empty($result)) { | |
throw new LockException("This order wasn't locked. Can't unlock"); | |
} | |
// lock if it's the same user who try to unlock | |
$data = json_decode($result, true); | |
if ($userId != $data['user']['id']) { | |
throw new LockException("Unlock failed. You can't unlock something that isn't yours"); | |
} | |
$result = $redis->hDel(sprintf(self::REDIS_CACHE_TEMPORARY_LOCK, $this->type), $orderId); | |
if ($result != true) { | |
throw new LockException("Unlock failed. Please try again"); | |
} | |
return true; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment