Completed
Pull Request — master (#25)
by
unknown
04:51
created

Lists::operatingSystems()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 2
nc 2
nop 2
dl 0
loc 5
rs 10
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($this->keys->primary)->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
     * top/lows operating systems
58
     *
59
     * @param int $limit
60
     * @param bool $isLow
61
     * @return mixed
62
     */
63
    public function operatingSystems($limit = -1, $isLow = false)
64
    {
65
        $range = $isLow ? 'zrange' : 'zrevrange';
66
67
        return $this->redis->$range($this->keys->visits . "_OSes:{$this->keys->id}", 0, $limit, 'WITHSCORES');
68
    }
69
70
    /**
71
     * top/lows languages
72
     *
73
     * @param int $limit
74
     * @param bool $isLow
75
     * @return mixed
76
     */
77
    public function languages($limit = -1, $isLow = false)
78
    {
79
        $range = $isLow ? 'zrange' : 'zrevrange';
80
81
        return $this->redis->$range($this->keys->visits . "_languages:{$this->keys->id}", 0, $limit, 'WITHSCORES');
82
    }
83
84
    /**
85
     * Fetch lowest subjects.
86
     *
87
     * @param int $limit
88
     * @return \Illuminate\Support\Collection|array
89
     */
90
    public function low($limit = 5)
91
    {
92
        return $this->top($limit, true);
93
    }
94
95
96
    /**
97
     * @param $limit
98
     * @param $visitsKey
99
     * @param bool $isLow
100
     * @return mixed
101
     */
102
    protected function getVisitsIds($limit, $visitsKey, $isLow = false)
103
    {
104
        $range = $isLow ? 'zrange' : 'zrevrange';
105
106
        return array_map('intval', $this->redis->$range($visitsKey, 0, $limit - 1));
107
    }
108
109
    /**
110
     * @param $cacheKey
111
     * @param $visitsIds
112
     * @return mixed
113
     */
114
    protected function freshList($cacheKey, $visitsIds)
115
    {
116
        if (count($visitsIds)) {
117
            $this->redis->del($cacheKey);
118
119
            return ($this->subject)::whereIn($this->keys->primary, $visitsIds)
120
                ->get()
121
                ->sortBy(function ($subject) use ($visitsIds) {
122
                    return array_search($subject->{$this->keys->primary}, $visitsIds);
123
                })->each(function ($subject) use ($cacheKey) {
124
                    $this->redis->rpush($cacheKey, serialize($subject));
125
                });
126
        }
127
128
        return [];
129
    }
130
131
    /**
132
     * @param $limit
133
     * @param $cacheKey
134
     * @return \Illuminate\Support\Collection|array
135
     */
136
    protected function cachedList($limit, $cacheKey)
137
    {
138
        return collect(
139
            array_map('unserialize', $this->redis->lrange($cacheKey, 0, $limit - 1))
140
        );
141
    }
142
}
143