Completed
Push — master ( f12444...e4ebc6 )
by
unknown
01:52
created

ActionItemsComposer::compose()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 0
cts 9
cp 0
rs 9.9332
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 2
1
<?php
2
3
namespace SET\Http\ViewComposers;
4
5
use Carbon\Carbon;
6
use Illuminate\Support\Collection;
7
use Illuminate\View\View;
8
use SET\Training;
9
use SET\User;
10
11
class ActionItemsComposer
12
{
13
    /**
14
     * Bind data to the view.
15
     *
16
     * @param View $view
17
     *
18
     * @return void
19
     */
20
    public function compose(View $view)
21
    {
22
        $dueTraining = $this->getDueTraining();
23
        $expiringVisits = $this->getExpiringVisits();
24
        $eligibilityRenewal = $this->getEligibilityRenewal();
25
26
        $view->with('dueTraining', $dueTraining)
27
            ->with('expiringVisits', $expiringVisits)
28
            ->with('eligibilityRenewal', $eligibilityRenewal);
29
    }
30
31
    /**
32
     * @return \Illuminate\Database\Eloquent\Collection|static[]
33
     */
34
    private function getDueTraining()
35
    {
36
        return Training::with(['users', 'assignedUsers.user', 'assignedUsers' => function ($query) {
37
            //filter the assignedusers we get back
38
            $query->ActiveUsers()
39
                ->whereNull('completed_date')
40
                ->where('due_date', '<=', Carbon::now());
41
        }])
42
            ->whereHas('assignedUsers', function ($q) {
43
                //filter the training we get.
44
                $q->ActiveUsers()
45
                    ->whereNull('completed_date')
46
                    ->where('due_date', '<=', Carbon::now());
47
            })
48
            ->get();
49
    }
50
51
    /**
52
     * @return \Illuminate\Database\Eloquent\Collection|static[]
53
     */
54
    private function getExpiringVisits()
55
    {
56
        return User::with(['visits' => function ($query) {
57
            $query->whereBetween('expiration_date', [Carbon::now(), Carbon::now()->addWeek()]);
58
        }])->whereHas('visits', function ($q) {
59
            $q->whereBetween('expiration_date', [Carbon::now(), Carbon::now()->addWeek()]);
60
        })->Active()->get();
61
    }
62
63
    /**
64
     * @return Collection
65
     */
66
    private function getEligibilityRenewal()
67
    {
68
        $builtUser = new Collection();
69
        $users = User::where('inv_close', '<=', Carbon::now())->active()->get();
70
71
        foreach ($users as $user) {
72
            $calculatedDays = $this->calculateDaysToRenewClearance($user);
73
74
            $this->buildUserArray($calculatedDays, $builtUser, $user);
75
        }
76
77
        return $this->sortUserCollection($builtUser);
78
    }
79
80
    /**
81
     * @param $user
82
     *
83
     * @return int
84
     */
85
    private function calculateDaysToRenewClearance($user)
86
    {
87
        $years = 100;
88
        if ($user->clearance == 'TS' || $user->clearance == 'Int TS' || $user->clearance == 'SCI') {
89
            $years = 6;
90
        } elseif ($user->clearance == 'S' || $user->clearance == 'Int S') {
91
            $years = 10;
92
        }
93
94
        if ($user->cont_eval)
95
            $calculatedDays = Carbon::now()->diffInDays(
96
                    Carbon::createFromFormat('Y-m-d', $user->cont_eval_date)->addYears($years), false);
97
        else
98
            $calculatedDays = Carbon::now()->diffInDays(
99
                Carbon::createFromFormat('Y-m-d', $user->inv_close)->addYears($years), false);
100
101
        return $calculatedDays;
102
    }
103
104
    /**
105
     * @param int $calculatedDays
106
     * @param $user
107
     * @param $builtUser
108
     */
109
    private function buildUserArray($calculatedDays, Collection $builtUser, User $user)
110
    {
111
        if ($calculatedDays <= 90) {
112
            $builtUser->push([
113
                'id'           => $user->id,
114
                'userFullName' => $user->userFullName,
115
                'days'         => $calculatedDays,
116
            ]);
117
        }
118
    }
119
120
    /**
121
     * @param Collection $userArray
122
     */
123
    private function sortUserCollection($userArray)
124
    {
125
        return $userArray->sortByDesc(function ($array) {
126
            return $array['days'];
127
        });
128
    }
129
}
130