Completed
Pull Request — master (#92)
by D.
18:36 queued 15:21
created

RenewTraining::getList()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
namespace SET\Console\Commands;
4
5
use Carbon\Carbon;
6
use Illuminate\Console\Command;
7
use Illuminate\Database\Eloquent\Collection;
8
use Illuminate\Support\Facades\DB;
9
use Illuminate\Support\Facades\Event;
10
use Illuminate\Support\Facades\Log;
11
use SET\Events\TrainingAssigned;
12
use SET\TrainingUser;
13
14
class RenewTraining extends Command
15
{
16
    /**
17
     * The number of days to renew a training before it expires.
18
     *
19
     * @var int
20
     */
21
    protected $offset = 30;
22
23
    /**
24
     * List of all renewed notes.
25
     *
26
     * @var Collection
27
     */
28
    protected $trainingAdminRecord;
29
30
    /**
31
     * The name and signature of the console command.
32
     *
33
     * @var string
34
     */
35
    protected $signature = 'training:renew';
36
37
    /**
38
     * The console command description.
39
     *
40
     * @var string
41
     */
42
    protected $description = 'Renews training before it expires.';
43
44
    /**
45
     * Create a new command instance.
46
     */
47 17
    public function __construct()
48
    {
49 17
        parent::__construct();
50 17
        $this->trainingAdminRecord = new Collection();
51 17
    }
52
53
    /**
54
     * Execute the console command.
55
     *
56
     * @return RenewTraining
57
     */
58 15
    public function handle()
59
    {
60 15
        $trainingUsers = TrainingUser::with('user', 'training')
61 15
            ->where('due_date', '<', Carbon::today())
62 15
            ->RenewableTrainings()  // Training is renewed (positive renews_in)
63 15
            ->activeUsers()
64 15
            ->orderBy(DB::raw('CASE WHEN completed_date IS NULL THEN 0 ELSE 1 END'))
65 15
            ->orderBy('completed_date', 'desc')
66 15
            ->get()
67 15
            ->unique(function ($item) {
68 13
                return $item['user_id'].'-'.$item['training_id'];
69 15
            });
70
71 15
        foreach ($trainingUsers as $trainingUser) {
72 13
            if (!$this->renewedAlready($trainingUser) && $this->timeToRenew($trainingUser)) {
73 13
                $this->processRenewal($trainingUser);
74
            }
75
        }
76
77 15
        return $this;
78
    }
79
80 15
    public function getList()
81
    {
82 15
        return $this->trainingAdminRecord->sortBy('userFullName');
83
    }
84
85
    /**
86
     * Check if training note has been renewed already.
87
     *
88
     * @param $trainingUser
89
     *
90
     * @return bool
91
     */
92 13
    private function renewedAlready($trainingUser)
93
    {
94 13
        if (is_null($trainingUser->completed_date)) {
95 6
            return true;
96
        }
97
98
        // Where due_date is in future or withing past renewal period
99 8
        $trainingRecord = TrainingUser::where('training_id', $trainingUser->training_id)
100 8
            ->where('user_id', $trainingUser->user_id)
101 8
            ->where('due_date', '>', Carbon::today()->subDays($trainingUser->training->renews_in))
102 8
            ->get();
103
104 8
        return !$trainingRecord->isEmpty();
105
    }
106
107
    /**
108
     * Check if the training is past the renews_in value.
109
     *
110
     * @param $trainingUser
111
     *
112
     * @return bool
113
     */
114 7
    private function timeToRenew($trainingUser)
115
    {
116 7
        if ($trainingUser->training->renews_in == 0) {
117
            return false;
118
        }
119 7
        if (!$trainingUser->completed_date) {
120
            return false;
121
        }
122
123 7
        $today = Carbon::today();
124
125 7
        $renewalDate = Carbon::createFromFormat('Y-m-d', $trainingUser->completed_date)
126 7
            ->addDays($trainingUser->training->renews_in)
127 7
            ->subDays($this->offset);
128
129 7
        return !($renewalDate >= $today);
130
    }
131
132
    /**
133
     * Generate a new Training note that will be due $this->offset days from now.
134
     *
135
     * @param $trainingUser
136
     */
137 5
    private function processRenewal($trainingUser)
138
    {
139 5
        $dueDate = Carbon::createFromFormat('Y-m-d', $trainingUser->completed_date)
140 5
            ->addDays($trainingUser->training->renews_in);
141
142 5
        $assignedTraining = $this->createRecord($trainingUser, $dueDate);
143
144
        //Email user of new training is due
145 5
        Event::fire(new TrainingAssigned($assignedTraining));
146 5
        Log::info('User training renewal notification.', [
147 5
            'user_id'       => $assignedTraining->user_id,
148 5
            'user_name'     => $assignedTraining->user->userFullName,
149 5
            'training_id'   => $assignedTraining->training_id,
150 5
            'training_name' => $assignedTraining->training->name,
151 5
            'due_date'      => $assignedTraining->due_date,
152
        ]);
153
154
        //
155 5
        $this->trainingAdminRecord->push([
156 5
            'name'     => $assignedTraining->user->userFullName,
157 5
            'training' => $assignedTraining->training->name,
158 5
            'due_date' => $dueDate->toDateString(),
159
        ]);
160 5
    }
161
162
    /**
163
     * @param $trainingUser
164
     * @param Carbon $dueDate
165
     *
166
     * @return TrainingUser
167
     */
168 5
    private function createRecord($trainingUser, $dueDate)
169
    {
170 5
        $newNote = TrainingUser::create([
171 5
            'user_id'        => $trainingUser->user_id,
172 5
            'author_id'      => 1,
173 5
            'training_id'    => $trainingUser->training_id,
174 5
            'due_date'       => $dueDate->toDateString(),
175
            'completed_date' => null,
176
        ]);
177
178 5
        return $newNote;
179
    }
180
}
181