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

RedisStorage::delete()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 2
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