Completed
Push — master ( 6e4edc...cbed4d )
by Felix
02:48
created

RedisStorage   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
c 1
b 0
f 0
lcom 1
cbo 3
dl 0
loc 50
ccs 0
cts 23
cp 0
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A store() 0 7 3
A get() 0 9 2
A delete() 0 4 1
1
<?php
2
/**
3
 * PhPsst.
4
 *
5
 * @copyright Copyright (c) 2016 Felix Sandström
6
 * @license   MIT
7
 */
8
9
namespace PhPsst\Storage;
10
11
use PhPsst\Password;
12
use PhPsst\PhPsstException;
13
use Predis\Client;
14
15
/**
16
 */
17
class RedisStorage extends Storage
18
{
19
    /**
20
     * @var Client
21
     */
22
    protected $client;
23
24
    /**
25
     * RedisStorage constructor.
26
     * @param Client $client
27
     */
28
    public function __construct(Client $client)
29
    {
30
        $this->client = $client;
31
    }
32
33
    /**
34
     * @param Password $password
35
     * @param bool $allowOverwrite
36
     */
37
    public function store(Password $password, $allowOverwrite = false)
38
    {
39
        if (!$allowOverwrite && $this->get($password->getId())) {
40
            throw new PhPsstException('The ID already exists', PhPsstException::ID_IS_ALREADY_TAKEN);
41
        }
42
        $this->client->set($password->getId(), $this->getJsonFromPassword($password));
43
    }
44
45
    /**
46
     * @param $key
47
     * @return Password|null
48
     */
49
    public function get($key)
50
    {
51
        $password = null;
52
        if (($passwordData = $this->client->get($key))) {
53
            $password = $this->getPasswordFromJson($passwordData);
54
        }
55
56
        return $password;
57
    }
58
59
    /**
60
     * @param Password $password
61
     */
62
    public function delete(Password $password)
63
    {
64
        $this->client->del($password->getId());
65
    }
66
}
67