LoginStatistics::countUsers()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 2
1
<?php
2
/**
3
 * @filename LoginStatistics.php
4
 * @touch    09/11/2016 16:28
5
 * @author   wudege <[email protected]>
6
 * @version  1.0.0
7
 */
8
9
namespace TokenAssistant;
10
11
12
use Predis\Client;
13
14
class LoginStatistics implements LoginStatisticsInterface
15
{
16
    const USER_STAT_SORTED_SET_NAMESPACE = 'user-stat:';
17
18
    /**
19
     * @var Client
20
     */
21
    private $redisClient;
22
23
    public function __construct(Client $client)
24
    {
25
        $this->redisClient = $client;
26
    }
27
28
    /**
29
     *
30
     * @author wudege <[email protected]>
31
     *
32
     * @param int $startTimestamp
33
     * @param int $endTimestamp
34
     *
35
     * @return int
36
     */
37
    public function countUsers($startTimestamp, $endTimestamp)
38
    {
39
        return $this->redisClient->zcount(static::USER_STAT_SORTED_SET_NAMESPACE, $startTimestamp, $endTimestamp);
40
    }
41
42
    /**
43
     *
44
     * @author wudege <[email protected]>
45
     *
46
     * @param $startTimestamp
47
     * @param $endTimestamp
48
     *
49
     * @return array
50
     */
51
    public function listUsers($startTimestamp, $endTimestamp)
52
    {
53
        return $this->redisClient->zrevrangebyscore(static::USER_STAT_SORTED_SET_NAMESPACE, $endTimestamp, $startTimestamp);
54
    }
55
56
    /**
57
     *
58
     * @author wudege <[email protected]>
59
     *
60
     * @param string $userId
61
     * @param int    $timestamp
62
     *
63
     * @return bool
64
     */
65
    public function refresh($userId, $timestamp)
66
    {
67
        $this->redisClient->zadd(static::USER_STAT_SORTED_SET_NAMESPACE, array($userId => $timestamp));
68
69
        return true;
70
    }
71
72
    /**
73
     *
74
     * @author wudege <[email protected]>
75
     *
76
     * @param $userId
77
     *
78
     * @return string
79
     */
80
    public function lastVisited($userId)
81
    {
82
        return $this->redisClient->zscore(static::USER_STAT_SORTED_SET_NAMESPACE, $userId);
83
    }
84
}