Completed
Push — master ( dee21b...fecc61 )
by ARCANEDEV
04:51
created

getLanguagesCountFromSessions()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 18
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 18
ccs 0
cts 12
cp 0
rs 9.4285
c 1
b 0
f 0
cc 2
eloc 12
nc 1
nop 2
crap 6
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     LanguagesListComposer
10
 *
11
 * @package  Arcanesoft\Tracker\ViewComposers\Dashboard
12
 * @author   ARCANEDEV <[email protected]>
13
 */
14
class LanguagesListComposer extends AbstractViewComposer
15
{
16
    /* ------------------------------------------------------------------------------------------------
17
     |  Constants
18
     | ------------------------------------------------------------------------------------------------
19
     */
20
    const VIEW = 'tracker::foundation._composers.dashboard.languages-ratio-list';
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
        $languages = $this->getLanguagesCountFromSessions($start, $end);
41
42
        $view->with('languagesRatio',  $this->calculateLanguagesPercentage($languages));
43
    }
44
45
    /* ------------------------------------------------------------------------------------------------
46
     |  Other Functions
47
     | ------------------------------------------------------------------------------------------------
48
     */
49
    /**
50
     * Get the languages count from sessions.
51
     *
52
     * @param  \Carbon\Carbon  $start
53
     * @param  \Carbon\Carbon  $end
54
     *
55
     * @return \Illuminate\Support\Collection
56
     */
57
    private function getLanguagesCountFromSessions($start, $end)
58
    {
59
        return $this->getCachedVisitors()
60
            ->filter(function (Session $visitor) use ($start, $end) {
61
                return $visitor->updated_at->between($start, $end) && ! is_null($visitor->language);
62
            })
63
            ->transform(function (Session $visitor) {
64
                return $visitor->language;
65
            })
66
            ->groupBy('preference')
67
            ->transform(function ($items, $key) {
68
                return [
69
                    'name'  => $key,
70
                    'count' => $items->count(),
71
                ];
72
            })
73
            ->sortByDesc('count');
74
    }
75
76
    /**
77
     * Calculate the languages percentage.
78
     *
79
     * @param  \Illuminate\Support\Collection  $languages
80
     *
81
     * @return \Illuminate\Support\Collection  $languages
82
     */
83
    private function calculateLanguagesPercentage($languages)
84
    {
85
        $total = $languages->sum('count');
86
87
        return $languages->transform(function ($item) use ($total) {
88
            return $item + [
89
                'percentage' => round(($item['count'] / $total) * 100, 2)
90
            ];
91
        });
92
    }
93
}
94