prepareVisitorsData()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 0
cts 8
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 6
nc 1
nop 3
crap 2
1
<?php namespace Arcanesoft\Tracker\ViewComposers\Dashboard;
2
3
use Arcanesoft\Tracker\Models\Visitor;
4
use Arcanesoft\Tracker\Models\VisitorActivity;
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
 * @TODO: Translate the date axis
18
 */
19
class LatestThirtyDaysVisitsAndVisitorsComposer extends AbstractViewComposer
20
{
21
    /* -----------------------------------------------------------------
22
     |  Constants
23
     | -----------------------------------------------------------------
24
     */
25
26
    const VIEW = 'tracker::admin._composers.dashboard.latest-thirty-days-visits-and-visitors-chart';
27
28
    /* -----------------------------------------------------------------
29
     |  Properties
30
     | -----------------------------------------------------------------
31
     */
32
33
    /**
34
     * Date format.
35
     *
36
     * @var string
37
     */
38
    protected $format = 'M-d';
39
40
    /* -----------------------------------------------------------------
41
     |  Main Methods
42
     | -----------------------------------------------------------------
43
     */
44
45
    /**
46
     * Compose the view.
47
     *
48
     * @param  \Illuminate\Contracts\View\View  $view
49
     */
50
    public function compose(View $view)
51
    {
52
        /**
53
         * @var  \Carbon\Carbon                  $start
54
         * @var  \Carbon\Carbon                  $end
55
         * @var  \Illuminate\Support\Collection  $range
56
         */
57
        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...
58
59
        $view->with('thirtyDaysRange', $range->map(function (Carbon $date) {
60
            return $date->format($this->format);
61
        }));
62
63
        $view->with('latestVisitsByThirtyDays',   $this->prepareVisitsData($start, $end, $range));
64
        $view->with('latestVisitorsByThirtyDays', $this->prepareVisitorsData($start, $end, $range));
65
    }
66
67
    /* -----------------------------------------------------------------
68
     |  Other Methods
69
     | -----------------------------------------------------------------
70
     */
71
72
    /**
73
     * Prepare the visitors data.
74
     *
75
     * @param  \Carbon\Carbon                  $start
76
     * @param  \Carbon\Carbon                  $end
77
     * @param  \Illuminate\Support\Collection  $range
78
     *
79
     * @return \Illuminate\Support\Collection
80
     */
81
    public function prepareVisitorsData(Carbon $start, Carbon $end, Collection $range)
82
    {
83
        $visitors = $this->getVisitorsFilteredByDateRange($start, $end)
84
            ->groupBy(function (Visitor $visitor) {
85
                return $visitor->created_at->format($this->format);
86
            });
87
88
        return $range->map(function (Carbon $date) use ($visitors) {
89
            return $visitors->get($date->format($this->format), new Collection)->count();
90
        });
91
    }
92
93
    /**
94
     * Prepare the visits data.
95
     *
96
     * @param  \Carbon\Carbon                  $start
97
     * @param  \Carbon\Carbon                  $end
98
     * @param  \Illuminate\Support\Collection  $range
99
     *
100
     * @return \Illuminate\Support\Collection
101
     */
102
    private function prepareVisitsData(Carbon $start, Carbon $end, $range)
103
    {
104
105
        $visits = $this->getVisitsFilteredByDateRange($start, $end)
106
            ->groupBy(function (VisitorActivity $visit) {
107
                return $visit->created_at->format($this->format);
108
            });
109
110
        return $range->map(function (Carbon $date) use ($visits) {
111
            return $visits->get($date->format($this->format), new Collection)->count();
112
        });
113
    }
114
}
115