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

  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\Session;
4
use Arcanesoft\Tracker\Support\DateRange;
5
use Arcanesoft\Tracker\ViewComposers\AbstractViewComposer;
6
use Illuminate\Contracts\View\View;
7
8
/**
9
 * Class     AuthenticatedVisitorsRatioComposer
10
 *
11
 * @package  Arcanesoft\Tracker\ViewComposers\Dashboard
12
 * @author   ARCANEDEV <[email protected]>
13
 */
14
class AuthenticatedVisitorsRatioComposer extends AbstractViewComposer
15
{
16
    /* ------------------------------------------------------------------------------------------------
17
     |  Constants
18
     | ------------------------------------------------------------------------------------------------
19
     */
20
    const VIEW = 'tracker::foundation._composers.dashboard.authenticated-visitors-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
        $visitors = $this->filterVisitors($start, $end);
41
42
        $view->with('authenticatedVisitorsRatio', $this->getAuthenticatedVisitorsRatio($visitors));
43
    }
44
45
    /* ------------------------------------------------------------------------------------------------
46
     |  Other Functions
47
     | ------------------------------------------------------------------------------------------------
48
     */
49
    /**
50
     * Get the authenticated visitors ratio.
51
     *
52
     * @param  \Illuminate\Support\Collection  $visitors
53
     *
54
     * @return \Illuminate\Support\Collection
55
     */
56
    private function getAuthenticatedVisitorsRatio($visitors)
57
    {
58
        $ratio = [
59
            'authenticated' => 0,
60
            'guest'         => 0,
61
        ];
62
63
        foreach ($visitors as $visitor) {
64
            $visitor->hasUser() ? $ratio['authenticated'] += 1 : $ratio['guest'] += 1;
65
        }
66
67
        return collect($ratio)->transform(function ($count, $key) {
68
            return [
69
                'name'  => trans("tracker::users.$key"),
70
                'count' => $count,
71
            ];
72
        });
73
    }
74
75
    /**
76
     * Get the filtered visitors.
77
     *
78
     * @param  \Carbon\Carbon  $start
79
     * @param  \Carbon\Carbon  $end
80
     *
81
     * @return \Illuminate\Support\Collection
82
     */
83
    private function filterVisitors($start, $end)
84
    {
85
        return $this->getCachedVisitors()->filter(function (Session $session) use ($start, $end) {
86
            return $session->updated_at->between($start, $end);
87
        });
88
    }
89
}
90