Completed
Push — master ( 3f7578...5307b5 )
by ARCANEDEV
04:28
created

compose()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 23
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 23
ccs 0
cts 13
cp 0
rs 9.0856
c 0
b 0
f 0
cc 1
eloc 10
nc 1
nop 1
crap 2
1
<?php namespace Arcanesoft\Tracker\ViewComposers\Dashboard;
2
3
use Arcanesoft\Tracker\Models\Session;
4
use Arcanesoft\Tracker\Models\SessionActivity;
5
use Arcanesoft\Tracker\Support\DateRange;
6
use Arcanesoft\Tracker\ViewComposers\AbstractViewComposer;
7
use Carbon\Carbon;
8
use Illuminate\Contracts\View\View;
9
use Illuminate\Support\Collection;
10
11
/**
12
 * Class     LatestThirtyDaysVisitsAndVisitorsComposer
13
 *
14
 * @package  Arcanesoft\Tracker\ViewComposers\Dashboard
15
 * @author   ARCANEDEV <[email protected]>
16
 */
17
class LatestThirtyDaysVisitsAndVisitorsComposer extends AbstractViewComposer
18
{
19
    /* ------------------------------------------------------------------------------------------------
20
     |  Constants
21
     | ------------------------------------------------------------------------------------------------
22
     */
23
    const VIEW = 'tracker::foundation._composers.dashboard.latest-thirty-days-visits-and-visitors-chart';
24
25
    /* ------------------------------------------------------------------------------------------------
26
     |  Properties
27
     | ------------------------------------------------------------------------------------------------
28
     */
29
    /**
30
     * Date format.
31
     *
32
     * @var string
33
     */
34
    protected $format = 'M-d';
35
36
    /* ------------------------------------------------------------------------------------------------
37
     |  Main Functions
38
     | ------------------------------------------------------------------------------------------------
39
     */
40
    /**
41
     * Compose the view.
42
     *
43
     * @param  \Illuminate\Contracts\View\View  $view
44
     */
45
    public function compose(View $view)
46
    {
47
        /**
48
         * @var  \Carbon\Carbon                  $start
49
         * @var  \Carbon\Carbon                  $end
50
         * @var  \Illuminate\Support\Collection  $range
51
         */
52
        extract(DateRange::getCurrentMonthDaysRange($this->format));
0 ignored issues
show
Bug introduced by
\Arcanesoft\Tracker\Supp...aysRange($this->format) cannot be passed to extract() as the parameter $var_array expects a reference.
Loading history...
53
54
        $view->with('thirtyDaysRange', $range->map(function (\Carbon\Carbon $date) {
55
            return $date->format($this->format);
56
        }));
57
58
        $view->with(
59
            'latestVisitsByThirtyDays',
60
            $this->prepareVisitsData($start, $end, $range)
61
        );
62
63
        $view->with(
64
            'latestVisitorsByThirtyDays',
65
            $this->prepareVisitorsData($start, $end, $range)
66
        );
67
    }
68
69
    /* ------------------------------------------------------------------------------------------------
70
     |  Other Functions
71
     | ------------------------------------------------------------------------------------------------
72
     */
73
    /**
74
     * Prepare the visitors data.
75
     *
76
     * @param  \Carbon\Carbon                  $start
77
     * @param  \Carbon\Carbon                  $end
78
     * @param  \Illuminate\Support\Collection  $range
79
     *
80
     * @return \Illuminate\Support\Collection
81
     */
82
    public function prepareVisitorsData(Carbon $start, Carbon $end, Collection $range)
83
    {
84
        $visitors = $this->getCachedVisitors()
85
            ->filter(function (Session $visitor) use ($start, $end) {
86
                return $visitor->created_at->between($start, $end);
87
            })
88
            ->groupBy(function (Session $visitor) {
89
                return $visitor->created_at->format($this->format);
90
            });
91
92
        return $range->map(function (\Carbon\Carbon $date) use ($visitors) {
93
            return $visitors->get($date->format($this->format), new Collection)->count();
94
        });
95
    }
96
97
    /**
98
     * Prepare the visits data.
99
     *
100
     * @param  \Carbon\Carbon                  $start
101
     * @param  \Carbon\Carbon                  $end
102
     * @param  \Illuminate\Support\Collection  $range
103
     *
104
     * @return \Illuminate\Support\Collection
105
     */
106
    public function prepareVisitsData(Carbon $start, Carbon $end, $range)
107
    {
108
        $visits = $this->getCachedVisits()
109
            ->filter(function (SessionActivity $visit) use ($start, $end) {
110
                return $visit->created_at->between($start, $end);
111
            })
112
            ->groupBy(function (SessionActivity $visit) {
113
                return $visit->created_at->format($this->format);
114
            });
115
116
        return $range->map(function (\Carbon\Carbon $date) use ($visits) {
117
            return $visits->get($date->format($this->format), new Collection)->count();
118
        });
119
    }
120
}
121