Completed
Pull Request — master (#21)
by Shawn
02:20
created

RenewTraining::timeToRenew()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

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