LatestThirtyDaysCreatedUsersComposer   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 86
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 6
dl 0
loc 86
ccs 0
cts 24
cp 0
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A compose() 0 14 1
A prepareUsersData() 0 11 1
A getDatesRange() 0 10 2
1
<?php namespace Arcanesoft\Auth\ViewComposers\Dashboard;
2
3
use Arcanesoft\Auth\Models\User;
4
use Arcanesoft\Auth\ViewComposers\ViewComposer;
5
use Carbon\Carbon;
6
use DateInterval;
7
use DatePeriod;
8
use Illuminate\Contracts\View\View;
9
use Illuminate\Support\Collection;
10
11
/**
12
 * Class     LatestThirtyDaysCreatedUsersComposer
13
 *
14
 * @package  Arcanesoft\Auth\ViewComposers\Dashboard
15
 * @author   ARCANEDEV <[email protected]>
16
 */
17
class LatestThirtyDaysCreatedUsersComposer extends ViewComposer
18
{
19
    /* -----------------------------------------------------------------
20
     |  Constants
21
     | -----------------------------------------------------------------
22
     */
23
    const VIEW = 'auth::admin._composers.dashboard.lastest-30-days-users-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 Methods
38
     | -----------------------------------------------------------------
39
     */
40
    /**
41
     * Compose the view.
42
     *
43
     * @param  \Illuminate\Contracts\View\View  $view
44
     */
45
    public function compose(View $view)
46
    {
47
        $users = $this->prepareUsersData(
48
            $start = Carbon::now()->subMonth(1)->setTime(0, 0),
49
            $end = Carbon::now()->setTime(23, 59, 59)
50
        );
51
52
        $view->with(
53
            'latestUsersByThirtyDays',
54
            $this->getDatesRange($start, $end)->transform(function ($date) use ($users) {
55
                return $users->get($date, new Collection)->count();
56
            })
57
        );
58
    }
59
60
    /* -----------------------------------------------------------------
61
     |  Other Methods
62
     | -----------------------------------------------------------------
63
     */
64
    /**
65
     * Prepare the users data.
66
     *
67
     * @param  \Carbon\Carbon  $start
68
     * @param  \Carbon\Carbon  $end
69
     *
70
     * @return \Illuminate\Support\Collection
71
     */
72
    public function prepareUsersData(Carbon $start, Carbon $end)
73
    {
74
        return $this->getCachedUsers()
75
            ->filter(function (User $user) use ($start, $end){
76
                return $user->created_at->between($start, $end);
77
            })
78
            ->groupBy(function (User $user) {
79
                return $user->created_at->format($this->format);
80
            })
81
            ->toBase();
82
    }
83
84
    /**
85
     * Get the dates ranges.
86
     *
87
     * @param  \Carbon\Carbon  $start
88
     * @param  \Carbon\Carbon  $end
89
     *
90
     * @return \Illuminate\Support\Collection
91
     */
92
    private function getDatesRange(Carbon $start, Carbon $end)
93
    {
94
        $range = new Collection();
95
96
        foreach (new DatePeriod($start, DateInterval::createFromDateString('1 day'), $end) as $period) {
97
            $range->put($date = $period->format($this->format), $date);
98
        }
99
100
        return $range;
101
    }
102
}
103