Completed
Push — master ( fecc61...f6dbaa )
by ARCANEDEV
04:52
created

CountriesListComposer   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 94
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 5
dl 0
loc 94
ccs 0
cts 37
cp 0
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A compose() 0 13 1
B getCountriesCountFromSessions() 0 32 3
A calculateCountriesPercentage() 0 10 1
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 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
    const VIEW = 'tracker::foundation._composers.dashboard.countries-ratio-list';
23
24
    /* ------------------------------------------------------------------------------------------------
25
     |  Main Functions
26
     | ------------------------------------------------------------------------------------------------
27
     */
28
    /**
29
     * Compose the view.
30
     *
31
     * @param  \Illuminate\Contracts\View\View  $view
32
     */
33
    public function compose(View $view)
34
    {
35
        /**
36
         * @var  \Carbon\Carbon                  $start
37
         * @var  \Carbon\Carbon                  $end
38
         * @var  \Illuminate\Support\Collection  $range
39
         */
40
        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...
41
42
        $view->with('countriesRatio', $this->calculateCountriesPercentage(
43
            $this->getCountriesCountFromSessions($start, $end)
44
        ));
45
    }
46
47
    /* ------------------------------------------------------------------------------------------------
48
     |  Other Functions
49
     | ------------------------------------------------------------------------------------------------
50
     */
51
    /**
52
     * Get the countries count from sessions.
53
     *
54
     * @param  \Carbon\Carbon  $start
55
     * @param  \Carbon\Carbon  $end
56
     *
57
     * @return \Illuminate\Support\Collection
58
     */
59
    private function getCountriesCountFromSessions(Carbon $start, Carbon $end)
60
    {
61
        return $this->getCachedVisitors()
62
            ->filter(function (Session $visitor) use ($start, $end) {
63
                return $visitor->updated_at->between($start, $end);
64
            })
65
            ->transform(function (Session $visitor) {
66
                return is_null($visitor->geoip)
67
                    ? [
68
                        'code'  => 'undefined',
69
                        'geoip' => null,
70
                    ]
71
                    : [
72
                        'code'  => $visitor->geoip->iso_code,
73
                        'geoip' => $visitor->geoip,
74
                    ];
75
            })
76
            ->groupBy('code')
77
            ->transform(function (Collection $items, $key) {
78
                return ($key === 'undefined')
79
                    ? [
80
                        'code'  => null,
81
                        'name'  => trans('tracker::geoip.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