Completed
Push — master ( c58f84...b8cb99 )
by Felix
02:35
created

RedisStorage::get()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 9
ccs 6
cts 6
cp 1
rs 9.6666
cc 2
eloc 5
nc 2
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 1
    public function __construct(Client $client)
29
    {
30 1
        $this->client = $client;
31 1
    }
32
33
    /**
34
     * @param Password $password
35
     * @param bool $allowOverwrite
36
     */
37 2
    public function store(Password $password, $allowOverwrite = false)
38
    {
39 2
        if (!$allowOverwrite && $this->get($password->getId())) {
40 1
            throw new PhPsstException('The ID already exists', PhPsstException::ID_IS_ALREADY_TAKEN);
41
        }
42 1
        $this->client->set($password->getId(), $password->getJson());
43 1
    }
44
45
    /**
46
     * @param $key
47
     * @return Password|null
48
     */
49 1
    public function get($key)
50
    {
51 1
        $password = null;
52 1
        if (($passwordData = $this->client->get($key))) {
53 1
            $password = $this->getPasswordFromJson($passwordData);
54 1
        }
55
56 1
        return $password;
57
    }
58
59
    /**
60
     * @param Password $password
61
     */
62 1
    public function delete(Password $password)
63
    {
64 1
        $this->client->del($password->getId());
65 1
    }
66
}
67