CountriesListComposer::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 1
Bugs 0 Features 0
Metric Value
dl 0
loc 13
ccs 0
cts 5
cp 0
rs 9.4285
c 1
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\Models\Visitor;
4
use Arcanesoft\Tracker\Support\DateRange;
5
use Arcanesoft\Tracker\ViewComposers\AbstractViewComposer;
6
use Carbon\Carbon;
7
use Illuminate\Contracts\View\View;
8
use Illuminate\Support\Collection;
9
10
/**
11
 * Class     CountriesListComposer
12
 *
13
 * @package  Arcanesoft\Tracker\ViewComposers\Dashboard
14
 * @author   ARCANEDEV <[email protected]>
15
 */
16
class CountriesListComposer extends AbstractViewComposer
17
{
18
    /* -----------------------------------------------------------------
19
     |  Constants
20
     | -----------------------------------------------------------------
21
     */
22
23
    const VIEW = 'tracker::admin._composers.dashboard.countries-ratio-list';
24
25
    /* -----------------------------------------------------------------
26
     |  Main Methods
27
     | -----------------------------------------------------------------
28
     */
29
30
    /**
31
     * Compose the view.
32
     *
33
     * @param  \Illuminate\Contracts\View\View  $view
34
     */
35
    public function compose(View $view)
36
    {
37
        /**
38
         * @var  \Carbon\Carbon                  $start
39
         * @var  \Carbon\Carbon                  $end
40
         * @var  \Illuminate\Support\Collection  $range
41
         */
42
        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...
43
44
        $view->with('countriesRatio', $this->calculateCountriesPercentage(
45
            $this->getCountriesCountFromSessions($start, $end)
46
        ));
47
    }
48
49
    /* -----------------------------------------------------------------
50
     |  Other Methods
51
     | -----------------------------------------------------------------
52
     */
53
54
    /**
55
     * Get the countries count from sessions.
56
     *
57
     * @param  \Carbon\Carbon  $start
58
     * @param  \Carbon\Carbon  $end
59
     *
60
     * @return \Illuminate\Support\Collection
61
     */
62
    private function getCountriesCountFromSessions(Carbon $start, Carbon $end)
63
    {
64
        return $this->getVisitorsFilteredByDateRange($start, $end)
65
            ->transform(function (Visitor $visitor) {
66
                return $visitor->hasGeoip()
67
                    ? [
68
                        'code'  => $visitor->geoip->iso_code,
69
                        'geoip' => $visitor->geoip,
70
                    ]
71
                    : [
72
                        'code'  => 'undefined',
73
                        'geoip' => null,
74
                    ];
75
            })
76
            ->groupBy('code')
77
            ->transform(function (Collection $items, $key) {
78
                return ($key === 'undefined')
79
                    ? [
80
                        'code'  => null,
81
                        'name'  => trans('tracker::generals.undefined'),
82
                        'count' => $items->count(),
83
                    ]
84
                    : [
85
                        'code'  => $key,
86
                        'name'  => $items->first()['geoip']->country,
87
                        'count' => $items->count(),
88
                    ];
89
            });
90
    }
91
92
    /**
93
     * Calculate countries percentage.
94
     *
95
     * @param  \Illuminate\Support\Collection  $countries
96
     *
97
     * @return \Illuminate\Support\Collection
98
     */
99
    private function calculateCountriesPercentage(Collection $countries)
100
    {
101
        $total = $countries->sum('count');
102
103
        return $countries->transform(function ($item) use ($total) {
104
            return $item + [
105
                'percentage' => round(($item['count'] / $total) * 100, 2)
106
            ];
107
        });
108
    }
109
}
110