Passed
Push — master ( 65709d...74d483 )
by bader
04:24 queued 10s
created

Lists::operatingSystems()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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