Passed
Push — master ( e196e8...87f444 )
by Felix
41s
created

Storage   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 29
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 8
lcom 0
cbo 1
dl 0
loc 29
ccs 14
cts 14
cp 1
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
store() 0 1 ?
get() 0 1 ?
delete() 0 1 ?
A getPasswordFromJson() 0 14 4
A hasValidJsonData() 0 7 4
1
<?php
2
/**
3
 * PhPsst.
4
 *
5
 * @copyright Copyright (c) 2018 Felix Sandström
6
 * @license   MIT
7
 */
8
9
namespace PhPsst\Storage;
10
11
use PhPsst\Password;
12
13
/**
14
 * @author Felix Sandström <http://github.com/felixsand>
15
 */
16
abstract class Storage
17
{
18
    abstract public function store(Password $password, bool $allowOverwrite = false): void;
19
    abstract public function get(string $key): ?Password;
20
    abstract public function delete(Password $password): void;
21
22 2
    public function getPasswordFromJson(string $jsonData): ?Password
23
    {
24 2
        $password = null;
25 2
        $jsonObject = json_decode($jsonData);
26 2
        if ($jsonObject && $this->hasValidJsonData($jsonObject)) {
27 2
            $password = new Password($jsonObject->id, $jsonObject->password, $jsonObject->ttl, $jsonObject->views);
28 2
            if ($jsonObject->ttl < time()) {
29 1
                $this->delete($password);
30 1
                $password = null;
31
            }
32
        }
33
34 2
        return $password;
35
    }
36
37 2
    private function hasValidJsonData(\stdClass $jsonObject): bool
38
    {
39 2
        return !empty($jsonObject->id)
40 2
            && !empty($jsonObject->password)
41 2
            && !empty($jsonObject->ttl)
42 2
            && !empty($jsonObject->views);
43
    }
44
}
45