AuthenticatedVisitorsRatioComposer::compose()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 0
cts 5
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 1
crap 2
1
<?php namespace Arcanesoft\Tracker\ViewComposers\Dashboard;
2
3
use Arcanesoft\Tracker\Support\DateRange;
4
use Arcanesoft\Tracker\ViewComposers\AbstractViewComposer;
5
use Illuminate\Contracts\View\View;
6
7
/**
8
 * Class     AuthenticatedVisitorsRatioComposer
9
 *
10
 * @package  Arcanesoft\Tracker\ViewComposers\Dashboard
11
 * @author   ARCANEDEV <[email protected]>
12
 */
13
class AuthenticatedVisitorsRatioComposer extends AbstractViewComposer
14
{
15
    /* -----------------------------------------------------------------
16
     |  Constants
17
     | -----------------------------------------------------------------
18
     */
19
20
    const VIEW = 'tracker::admin._composers.dashboard.authenticated-visitors-ratio-chart';
21
22
    /* -----------------------------------------------------------------
23
     |  Main Methods
24
     | -----------------------------------------------------------------
25
     */
26
27
    /**
28
     * Compose the view.
29
     *
30
     * @param  \Illuminate\Contracts\View\View  $view
31
     */
32
    public function compose(View $view)
33
    {
34
        /**
35
         * @var  \Carbon\Carbon                  $start
36
         * @var  \Carbon\Carbon                  $end
37
         * @var  \Illuminate\Support\Collection  $range
38
         */
39
        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...
40
41
        $view->with('authenticatedVisitorsRatio', $this->getAuthenticatedVisitorsRatio(
42
            $this->getVisitorsFilteredByDateRange($start, $end)
43
        ));
44
    }
45
46
    /* -----------------------------------------------------------------
47
     |  Other Methods
48
     | -----------------------------------------------------------------
49
     */
50
51
    /**
52
     * Get the authenticated visitors ratio.
53
     *
54
     * @param  \Illuminate\Support\Collection  $visitors
55
     *
56
     * @return \Illuminate\Support\Collection
57
     */
58
    private function getAuthenticatedVisitorsRatio($visitors)
59
    {
60
        $ratio = [
61
            'authenticated' => 0,
62
            'guest'         => 0,
63
        ];
64
65
        foreach ($visitors as $visitor) {
66
            $visitor->hasUser() ? $ratio['authenticated'] += 1 : $ratio['guest'] += 1;
67
        }
68
69
        return collect($ratio)->transform(function ($count, $key) {
70
            return [
71
                'name'  => trans("tracker::visitors.$key"),
72
                'count' => $count,
73
            ];
74
        });
75
    }
76
}
77