Completed
Push — master ( 3f7578...5307b5 )
by ARCANEDEV
04:28
created

DevicesRatioComposer::getDevicesFromSessions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 0
cts 6
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 6
nc 1
nop 2
crap 2
1
<?php namespace Arcanesoft\Tracker\ViewComposers\Dashboard;
2
3
use Arcanesoft\Tracker\Models\Device;
4
use Arcanesoft\Tracker\Support\DateRange;
5
use Arcanesoft\Tracker\ViewComposers\AbstractViewComposer;
6
use Carbon\Carbon;
7
use Illuminate\Support\Collection;
8
use Illuminate\View\View;
9
10
/**
11
 * Class     DevicesRatioComposer
12
 *
13
 * @package  Arcanesoft\Tracker\ViewComposers\Dashboard
14
 * @author   ARCANEDEV <[email protected]>
15
 */
16
class DevicesRatioComposer extends AbstractViewComposer
17
{
18
    /* ------------------------------------------------------------------------------------------------
19
     |  Constants
20
     | ------------------------------------------------------------------------------------------------
21
     */
22
    const VIEW = 'tracker::foundation._composers.dashboard.devices-ratio-chart';
23
24
    /* ------------------------------------------------------------------------------------------------
25
     |  Main Functions
26
     | ------------------------------------------------------------------------------------------------
27
     */
28
    /**
29
     * Compose the view.
30
     *
31
     * @param  \Illuminate\Contracts\View\View  $view
32
     */
33
    public function compose(View $view)
34
    {
35
        /**
36
         * @var  \Carbon\Carbon                  $start
37
         * @var  \Carbon\Carbon                  $end
38
         * @var  \Illuminate\Support\Collection  $range
39
         */
40
        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...
41
42
        $devices = $this->getDevicesFromSessions($start, $end);
43
44
        $view->with('computerDevices',  $this->getComputersCount($devices));
45
        $view->with('tabletDevices',    $this->getTabletsCount($devices));
46
        $view->with('phoneDevices',     $this->getPhonesCount($devices));
47
        $view->with('unavailableDevices', $this->getUnavailableCount($devices));
48
    }
49
50
    /* ------------------------------------------------------------------------------------------------
51
     |  Other Functions
52
     | ------------------------------------------------------------------------------------------------
53
     */
54
    /**
55
     * Get the devices from sessions.
56
     *
57
     * @param  \Carbon\Carbon  $start
58
     * @param  \Carbon\Carbon  $end
59
     *
60
     * @return \Illuminate\Support\Collection
61
     */
62
    protected function getDevicesFromSessions(Carbon $start, Carbon $end)
63
    {
64
        return $this->getCachedVisitors()
65
            ->filter(function ($session) use ($start, $end) {
66
                return $session->updated_at->between($start, $end);
67
            })
68
            ->transform(function ($session) {
69
                return $session->device;
70
            });
71
    }
72
73
    /**
74
     * Get the computers count.
75
     *
76
     * @param  \Illuminate\Support\Collection  $devices
77
     *
78
     * @return int
79
     */
80
    protected function getComputersCount(Collection $devices)
81
    {
82
        return $devices->filter(function (Device $device) {
83
            return $device->isComputer();
84
        })->count();
85
    }
86
87
    /**
88
     * Get the tablets count.
89
     *
90
     * @param  \Illuminate\Support\Collection  $devices
91
     *
92
     * @return int
93
     */
94
    protected function getTabletsCount(Collection $devices)
95
    {
96
        return $devices->filter(function (Device $device) {
97
            return $device->isTablet();
98
        })->count();
99
    }
100
101
    /**
102
     * Get the phones count.
103
     *
104
     * @param  \Illuminate\Support\Collection  $devices
105
     *
106
     * @return int
107
     */
108
    protected function getPhonesCount(Collection $devices)
109
    {
110
        return $devices->filter(function (Device $device) {
111
            return $device->isPhone();
112
        })->count();
113
    }
114
115
    /**
116
     * Get the computers count.
117
     *
118
     * @param  \Illuminate\Support\Collection  $devices
119
     *
120
     * @return int
121
     */
122
    protected function getUnavailableCount(Collection $devices)
123
    {
124
        return $devices->reject(function (Device $device) {
125
            return $device->isComputer() || $device->isTablet() || $device->isPhone();
126
        })->count();
127
    }
128
}
129