RedisStorage::delete()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
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;
0 ignored issues
show
Bug introduced by
The type Predis\Client was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
14
15
/**
16
 * @author Felix Sandström <http://github.com/felixsand>
17
 */
18
class RedisStorage extends Storage
19
{
20
    /**
21
     * @var Client
22
     */
23
    protected $client;
24
25 1
    public function __construct(Client $client)
26
    {
27 1
        $this->client = $client;
28
    }
29
30 2
    public function store(Password $password, bool $allowOverwrite = false): void
31
    {
32 2
        if (!$allowOverwrite && $this->get($password->getId())) {
33 1
            throw new PhPsstException('The ID already exists', PhPsstException::ID_IS_ALREADY_TAKEN);
34
        }
35 1
        $this->client->set($password->getId(), $password->getJson());
36 1
        $this->client->expireat($password->getId(), $password->getTtl());
37
    }
38
39 1
    public function get(string $key): ?Password
40
    {
41 1
        $password = null;
42 1
        if (($passwordData = $this->client->get($key))) {
43 1
            $password = $this->getPasswordFromJson($passwordData);
44
        }
45
46 1
        return $password;
47
    }
48
49 1
    public function delete(Password $password): void
50
    {
51 1
        $this->client->del([$password->getId()]);
52
    }
53
}
54