Completed
Push — master ( 5307b5...dee21b )
by ARCANEDEV
04:21
created

BrowsersRatioComposer   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 3
lcom 1
cbo 4
dl 0
loc 60
ccs 0
cts 17
cp 0
rs 10
c 1
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A compose() 0 11 1
A getBrowsersCountFromSessions() 0 18 2
1
<?php namespace Arcanesoft\Tracker\ViewComposers\Dashboard;
2
3
use Arcanesoft\Tracker\Models\Session;
4
use Arcanesoft\Tracker\Support\DateRange;
5
use Arcanesoft\Tracker\ViewComposers\AbstractViewComposer;
6
use Illuminate\Contracts\View\View;
7
8
/**
9
 * Class     BrowsersRatioComposer
10
 *
11
 * @package  Arcanesoft\Tracker\ViewComposers\Dashboard
12
 * @author   ARCANEDEV <[email protected]>
13
 */
14
class BrowsersRatioComposer extends AbstractViewComposer
15
{
16
    /* ------------------------------------------------------------------------------------------------
17
     |  Constants
18
     | ------------------------------------------------------------------------------------------------
19
     */
20
    const VIEW = 'tracker::foundation._composers.dashboard.browsers-ratio-chart';
21
22
    /* ------------------------------------------------------------------------------------------------
23
     |  Main Functions
24
     | ------------------------------------------------------------------------------------------------
25
     */
26
    /**
27
     * Compose the view.
28
     *
29
     * @param  \Illuminate\Contracts\View\View  $view
30
     */
31
    public function compose(View $view)
32
    {
33
        /**
34
         * @var  \Carbon\Carbon                  $start
35
         * @var  \Carbon\Carbon                  $end
36
         * @var  \Illuminate\Support\Collection  $range
37
         */
38
        extract(DateRange::getCurrentMonthDaysRange());
0 ignored issues
show
Bug introduced by
\Arcanesoft\Tracker\Supp...CurrentMonthDaysRange() cannot be passed to extract() as the parameter $var_array expects a reference.
Loading history...
39
40
        $view->with('browsersRatio', $this->getBrowsersCountFromSessions($start, $end));
41
    }
42
43
    /* ------------------------------------------------------------------------------------------------
44
     |  Other Functions
45
     | ------------------------------------------------------------------------------------------------
46
     */
47
    /**
48
     * Get the browsers count from sessions.
49
     *
50
     * @param  \Carbon\Carbon                  $start
51
     * @param  \Carbon\Carbon                  $end
52
     *
53
     * @return \Illuminate\Support\Collection  $range
54
     */
55
    private function getBrowsersCountFromSessions($start, $end)
56
    {
57
        return $this->getCachedVisitors()
58
            ->filter(function (Session $visitor) use ($start, $end) {
59
                return $visitor->updated_at->between($start, $end) && ! is_null($visitor->agent);
60
            })
61
            ->transform(function (Session $visitor) {
62
                return $visitor->agent;
63
            })
64
            ->groupBy('browser')
65
            ->transform(function ($items, $key) {
66
                return [
67
                    'name'  => $key,
68
                    'count' => $items->count(),
69
                ];
70
            })
71
            ->sortByDesc('count');
72
    }
73
}
74