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

RedisStorage   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

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 17
cts 17
cp 1
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A store() 0 7 3
A delete() 0 4 1
A get() 0 9 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