Completed
Push — master ( 824977...b4976e )
by ARCANEDEV
04:42
created

getReferersCountFromSessionActivities()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 19
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 19
c 0
b 0
f 0
ccs 0
cts 13
cp 0
rs 9.4285
cc 2
eloc 13
nc 1
nop 2
crap 6
1
<?php namespace Arcanesoft\Tracker\ViewComposers\Dashboard;
2
3
use Arcanesoft\Tracker\Models\SessionActivity;
4
use Arcanesoft\Tracker\Support\DateRange;
5
use Arcanesoft\Tracker\ViewComposers\AbstractViewComposer;
6
use Illuminate\Contracts\View\View;
7
use Illuminate\Support\Collection;
8
9
/**
10
 * Class     ReferersListComposer
11
 *
12
 * @package  Arcanesoft\Tracker\ViewComposers\Dashboard
13
 * @author   ARCANEDEV <[email protected]>
14
 */
15
class ReferersListComposer extends AbstractViewComposer
16
{
17
    /* ------------------------------------------------------------------------------------------------
18
     |  Constants
19
     | ------------------------------------------------------------------------------------------------
20
     */
21
    const VIEW = 'tracker::foundation._composers.dashboard.referers-ratio-list';
22
23
    /* ------------------------------------------------------------------------------------------------
24
     |  Main Functions
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('referersRatio', $this->calculateLanguagesPercentage(
42
            $this->getReferersCountFromSessionActivities($start, $end)
43
        ));
44
    }
45
46
    /* ------------------------------------------------------------------------------------------------
47
     |  Other Functions
48
     | ------------------------------------------------------------------------------------------------
49
     */
50
    /**
51
     * Get the languages count from sessions.
52
     *
53
     * @param  \Carbon\Carbon  $start
54
     * @param  \Carbon\Carbon  $end
55
     *
56
     * @return \Illuminate\Support\Collection
57
     */
58
    private function getReferersCountFromSessionActivities($start, $end)
59
    {
60
        return $this->getVisitsFilteredByDateRange($start, $end)
61
            ->transform(function (SessionActivity $visit) {
62
                return [
63
                    'name' => $visit->hasReferer()
64
                        ? $visit->referer->domain->name
65
                        : trans('tracker::referers.undefined'),
66
                ];
67
            })
68
            ->groupBy('name')
69
            ->transform(function (Collection $items, $key) {
70
                return [
71
                    'name'  => $key,
72
                    'count' => $items->count(),
73
                ];
74
            })
75
            ->sortByDesc('count');
76
    }
77
78
    /**
79
     * Calculate the referers percentage.
80
     *
81
     * @param  \Illuminate\Support\Collection  $referers
82
     *
83
     * @return \Illuminate\Support\Collection
84
     */
85
    private function calculateLanguagesPercentage($referers)
86
    {
87
        $total = $referers->sum('count');
88
89
        return $referers->transform(function ($item) use ($total) {
90
            return $item + [
91
                'percentage' => round(($item['count'] / $total) * 100, 2)
92
            ];
93
        });
94
    }
95
}
96