Lists::getSortedList()   A
last analyzed

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 4
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 array $constraints optional. filter models by attributes (where=[...])
14
     * @return \Illuminate\Support\Collection|array
15
     */
16
    public function top($limit = 5, $orderByAsc = false, $constraints = [])
17
    {
18
        if(is_array($orderByAsc)) {
19
            $constraints = $orderByAsc;
20
            $orderByAsc = false;
21
        }
22
23
        $cacheKey = $this->keys->cache($limit, $orderByAsc, $constraints);
24
25
        $cachedList = $this->cachedList($limit, $cacheKey);
26
        $visitsIds = $this->getVisitsIds($limit, $this->keys->visits, $orderByAsc);
27
28
        if($visitsIds === $cachedList->pluck($this->keys->primary)->toArray() && ! $this->fresh) {
29
            return $cachedList;
30
        }
31
32
        return $this->freshList($cacheKey, $visitsIds, $constraints);
33
    }
34
35
36
    /**
37
     * Top/low countries
38
     */
39
    public function countries($limit = -1, $orderByAsc = false)
40
    {
41
        return $this->getSortedList('countries', $limit, $orderByAsc, true);
42
    }
43
44
    /**
45
     * top/lows refs
46
     */
47
    public function refs($limit = -1, $orderByAsc = false)
48
    {
49
        return $this->getSortedList('referers', $limit, $orderByAsc, true);
50
    }
51
52
    /**
53
     * top/lows operating systems
54
     */
55
    public function operatingSystems($limit = -1, $orderByAsc = false)
56
    {
57
        return $this->getSortedList('OSes', $limit, $orderByAsc, true);
58
    }
59
60
    /**
61
     * top/lows languages
62
     */
63
    public function languages($limit = -1, $orderByAsc = false)
64
    {
65
        return $this->getSortedList('languages', $limit, $orderByAsc, true);
66
    }
67
68
69
    protected function getSortedList($name, $limit, $orderByAsc = false, $withValues = true)
70
    {
71
        return $this->connection->valueList($this->keys->visits . "_{$name}:{$this->keys->id}", $limit, $orderByAsc, $withValues);
72
    }
73
74
    /**
75
     * Fetch lowest subjects.
76
     *
77
     * @param int $limit
78
     * @param array $constraints optional
79
     * @return \Illuminate\Support\Collection|array
80
     */
81
    public function low($limit = 5, $constraints = [])
82
    {
83
        return $this->top($limit, true, $constraints);
84
    }
85
86
87
    /**
88
     * @param $limit
89
     * @param $visitsKey
90
     * @param bool $isLow
91
     * @return mixed
92
     */
93
    protected function getVisitsIds($limit, $visitsKey, $orderByAsc = false)
94
    {
95
        return array_map(function($item) {
96
            return is_numeric($item) ? intval($item) : $item;
97
        }, $this->connection->valueList($visitsKey, $limit - 1, $orderByAsc));
98
    }
99
100
    /**
101
     * @param $cacheKey
102
     * @param $visitsIds
103
     * @return mixed
104
     */
105
    protected function freshList($cacheKey, $visitsIds, $constraints = [])
106
    {
107
        if (count($visitsIds)) {
108
109
            $this->connection->delete($cacheKey);
110
111
            return ($this->subject)::whereIn($this->keys->primary, $visitsIds)
112
                ->when(count($constraints), function($query) use($constraints) {
113
                    return $query->where($constraints);
114
                })
115
                ->get()
116
                ->sortBy(function ($subject) use ($visitsIds) {
117
                    return array_search($subject->{$this->keys->primary}, $visitsIds);
118
                })->each(function ($subject) use ($cacheKey) {
119
                    $this->connection->addToFlatList($cacheKey, serialize($subject));
120
                });
121
        }
122
123
        return [];
124
    }
125
126
    /**
127
     * @param $limit
128
     * @param $cacheKey
129
     * @return \Illuminate\Support\Collection|array
130
     */
131
    protected function cachedList($limit, $cacheKey)
132
    {
133
        return Collection::make(
134
            array_map('unserialize', $this->connection->flatList($cacheKey, $limit))
135
        );
136
    }
137
}
138