Completed
Push — Framework-Dependencies-Upgrade ( 998813...01c962 )
by Sheela
07:38
created

RenewTraining::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
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 trainings.
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 15
    public function __construct()
48
    {
49 15
        parent::__construct();
50 15
        $this->trainingAdminRecord = new Collection();
51 15
    }
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
        // Add the created traininguser record to trainingAdminRecord
155 5
        $this->trainingAdminRecord->push($assignedTraining);
156 5
    }
157
158
    /**
159
     * @param $trainingUser
160
     * @param Carbon $dueDate
161
     *
162
     * @return TrainingUser
163
     */
164 5
    private function createRecord($trainingUser, $dueDate)
165
    {
166 5
        $newNote = TrainingUser::create([
0 ignored issues
show
Bug introduced by
The method create() does not exist on SET\TrainingUser. Did you maybe mean created()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
167 5
            'user_id'        => $trainingUser->user_id,
168 5
            'author_id'      => 1,
169 5
            'training_id'    => $trainingUser->training_id,
170 5
            'due_date'       => $dueDate->toDateString(),
171
            'completed_date' => null,
172
        ]);
173
174 5
        return $newNote;
175
    }
176
}
177