BlablacarRedisStorage::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
ccs 3
cts 3
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 2
crap 1
1
<?php
2
3
namespace Xsolve\SalesforceClient\Storage;
4
5
use Blablacar\Redis\Client;
6
use Xsolve\SalesforceClient\Security\Token\TokenInterface;
7
8
class BlablacarRedisStorage implements TokenStorageInterface
9
{
10
    const DEFAULT_KEY = 'salesforce_token';
11
12
    /**
13
     * @var Client
14
     */
15
    protected $client;
16
17
    /**
18
     * @var string
19
     */
20
    protected $key;
21
22 5
    public function __construct(Client $client, string $key = self::DEFAULT_KEY)
23
    {
24 5
        $this->client = $client;
25 5
        $this->key = $key;
26 5
    }
27
28
    /**
29
     * @return TokenInterface
30
     *
31
     * @throws \LogicException if the token is not set
32
     */
33 2
    public function get(): TokenInterface
34
    {
35 2
        $token = $this->client->get($this->key);
36
37 2
        if (null === $token) {
38 1
            throw new \LogicException('No token has been set');
39
        }
40
41 1
        return unserialize($token);
42
    }
43
44 2
    public function has(): bool
45
    {
46 2
        return (bool) $this->client->exists($this->key);
47
    }
48
49 1
    public function save(TokenInterface $token)
50
    {
51 1
        $this->client->set($this->key, serialize($token));
52 1
    }
53
}
54