Passed
Push — master ( 120ad6...d2d121 )
by bader
07:57
created

Lists::freshList()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 9
nc 2
nop 2
dl 0
loc 15
rs 9.9666
c 0
b 0
f 0
1
<?php
2
3
namespace awssat\Visits\Traits;
4
5
trait Lists
6
{
7
    /**
8
     * Fetch all time trending subjects.
9
     *
10
     * @param int $limit
11
     * @param bool $isLow
12
     * @return \Illuminate\Support\Collection|array
13
     */
14
    public function top($limit = 5, $isLow = false)
15
    {
16
        $cacheKey = $this->keys->cache($limit, $isLow);
17
        $cachedList = $this->cachedList($limit, $cacheKey);
18
        $visitsIds = $this->getVisitsIds($limit, $this->keys->visits, $isLow);
19
20
        if($visitsIds === $cachedList->pluck('id')->toArray() && ! $this->fresh) {
21
            return $cachedList;
22
        }
23
24
        return $this->freshList($cacheKey, $visitsIds);
25
    }
26
27
28
    /**
29
     * Top/low countries
30
     *
31
     * @param int $limit
32
     * @param bool $isLow
33
     * @return mixed
34
     */
35
    public function countries($limit = -1, $isLow = false)
36
    {
37
        $range = $isLow ? 'zrange' : 'zrevrange';
38
39
        return $this->redis->$range($this->keys->visits . "_countries:{$this->keys->id}", 0, $limit, 'WITHSCORES');
40
    }
41
42
    /**
43
     * top/lows refs
44
     *
45
     * @param int $limit
46
     * @param bool $isLow
47
     * @return mixed
48
     */
49
    public function refs($limit = -1, $isLow = false)
50
    {
51
        $range = $isLow ? 'zrange' : 'zrevrange';
52
53
        return $this->redis->$range($this->keys->visits . "_referers:{$this->keys->id}", 0, $limit, 'WITHSCORES');
54
    }
55
56
    /**
57
     * Fetch lowest subjects.
58
     *
59
     * @param int $limit
60
     * @return \Illuminate\Support\Collection|array
61
     */
62
    public function low($limit = 5)
63
    {
64
        return $this->top($limit, true);
65
    }
66
67
68
    /**
69
     * @param $limit
70
     * @param $visitsKey
71
     * @param bool $isLow
72
     * @return mixed
73
     */
74
    protected function getVisitsIds($limit, $visitsKey, $isLow = false)
75
    {
76
        $range = $isLow ? 'zrange' : 'zrevrange';
77
78
        return array_map('intval', $this->redis->$range($visitsKey, 0, $limit - 1));
79
    }
80
81
    /**
82
     * @param $cacheKey
83
     * @param $visitsIds
84
     * @return mixed
85
     */
86
    protected function freshList($cacheKey, $visitsIds)
87
    {
88
        if (count($visitsIds)) {
89
            $this->redis->del($cacheKey);
90
91
            return ($this->subject)::whereIn($this->keys->primary, $visitsIds)
92
                ->get()
93
                ->sortBy(function ($subject) use ($visitsIds) {
94
                    return array_search($subject->{$this->keys->primary}, $visitsIds);
95
                })->each(function ($subject) use ($cacheKey) {
96
                    $this->redis->rpush($cacheKey, serialize($subject));
97
                });
98
        }
99
100
        return [];
101
    }
102
103
    /**
104
     * @param $limit
105
     * @param $cacheKey
106
     * @return \Illuminate\Support\Collection|array
107
     */
108
    protected function cachedList($limit, $cacheKey)
109
    {
110
        return collect(
111
            array_map('unserialize', $this->redis->lrange($cacheKey, 0, $limit - 1))
112
        );
113
    }
114
}
115