Skip to content

Commit

Permalink
Merge pull request #3 from felixsand/cleaner-code-improvement
Browse files Browse the repository at this point in the history
Breakup ::getPasswordFromJson() to improve readability
  • Loading branch information
Felix Sandström authored Dec 30, 2018
2 parents e196e8a + 1ff32c7 commit 87f4440
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 7 deletions.
18 changes: 11 additions & 7 deletions src/PhPsst/Storage/Storage.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/**
* PhPsst.
*
* @copyright Copyright (c) 2016 Felix Sandström
* @copyright Copyright (c) 2018 Felix Sandström
* @license MIT
*/

Expand All @@ -22,12 +22,8 @@ abstract public function delete(Password $password): void;
public function getPasswordFromJson(string $jsonData): ?Password
{
$password = null;
if (($jsonObject = json_decode($jsonData))
&& !empty($jsonObject->id)
&& !empty($jsonObject->password)
&& !empty($jsonObject->ttl)
&& !empty($jsonObject->views)
) {
$jsonObject = json_decode($jsonData);
if ($jsonObject && $this->hasValidJsonData($jsonObject)) {
$password = new Password($jsonObject->id, $jsonObject->password, $jsonObject->ttl, $jsonObject->views);
if ($jsonObject->ttl < time()) {
$this->delete($password);
Expand All @@ -37,4 +33,12 @@ public function getPasswordFromJson(string $jsonData): ?Password

return $password;
}

private function hasValidJsonData(\stdClass $jsonObject): bool
{
return !empty($jsonObject->id)
&& !empty($jsonObject->password)
&& !empty($jsonObject->ttl)
&& !empty($jsonObject->views);
}
}
2 changes: 2 additions & 0 deletions tests/unit/Storage/StorageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class StorageTest extends TestCase
{
/**
* @covers ::getPasswordFromJson
* @covers ::hasValidJsonData
*/
public function testGetPasswordFromJson()
{
Expand All @@ -37,6 +38,7 @@ public function testGetPasswordFromJson()

/**
* @covers ::getPasswordFromJson
* @covers ::hasValidJsonData
*/
public function testDeleteOnExpired()
{
Expand Down

0 comments on commit 87f4440

Please sign in to comment.