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

RedisStorage::get()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

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