Completed
Push — master ( 5a5b2e...66ec7e )
by Matze
04:12
created

Gateway::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2
Metric Value
dl 0
loc 4
ccs 0
cts 0
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 2
1
<?php
2
3
namespace BrainExe\Core\Authentication\Settings;
4
5
use BrainExe\Annotations\Annotations\Inject;
6
use BrainExe\Annotations\Annotations\Service;
7
use BrainExe\Core\Redis\Predis;
8
9
/**
10
 * @Service("User.Settings.Gateway", public=false)
11
 */
12
class Gateway
13
{
14
15
    const REDIS_KEY = 'user:settings:%s';
16
17
    /**
18
     * @var Predis
19
     */
20
    private $redis;
21
22 1
    /**
23
     * @Inject("@Redis")
24 1
     * @param Predis $client
25
     */
26
    public function __construct(Predis $client)
27
    {
28
        $this->redis = $client;
29
    }
30
31
    /**
32 1
     * @param int $userId
33
     * @return string[]
34 1
     */
35
    public function getAll(int $userId) : array
36
    {
37
        return array_map('json_decode', $this->redis->hgetall($this->getKey($userId)));
38
    }
39
40
    /**
41
     * @param int $userId
42 1
     * @param string $setting
43
     * @return string
44 1
     */
45 1
    public function get(int $userId, string $setting)
46
    {
47
        return json_decode($this->redis->hget($this->getKey($userId), $setting));
48
    }
49
50
    /**
51
     * @param int $userId
52 3
     * @param string $setting
53
     * @param string $value
54 3
     */
55
    public function set(int $userId, string $setting, $value)
56
    {
57
        $this->redis->hset($this->getKey($userId), $setting, json_encode($value));
58
    }
59
60
    /**
61
     * @param int $userId
62
     * @return string
63
     */
64
    private function getKey(int $userId) : string
65
    {
66
        return sprintf(self::REDIS_KEY, $userId);
67
    }
68
}
69