Completed
Push — master ( ac047e...863ff8 )
by Sheela
06:46
created

SendReminders::getList()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 2
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 Illuminate\Support\Facades\Mail;
10
use SET\Events\TrainingAssigned;
11
use SET\Mail\EmailSupervisorReminder;
12
use SET\TrainingUser;
13
14
/**
15
 * Class SendReminders.
16
 */
17
class SendReminders extends Command
18
{
19
    /**
20
     * The name and signature of the console command.
21
     *
22
     * @var string
23
     */
24
    protected $signature = 'emails:reminders';
25
26
    /**
27
     * The console command description.
28
     *
29
     * @var string
30
     */
31
    protected $description = 'Sends out email reminders a week before they are due.';
32
33
    /**
34
     * @var
35
     */
36
    protected $trainingUsers;
37
38
    /**
39
     * Execute the console command.
40
     * Sends out reminder emails to all employees. Then sends a reminder to their supervisor.
41
     *
42
     * @return SendReminders
43
     */
44
    public function handle()
45
    {
46
        $this->setTrainingUsers();
47
48
        foreach ($this->trainingUsers as $trainingUser) {
49
            Event::fire(new TrainingAssigned($trainingUser));
50
        }
51
52
        $this->emailSupervisor();
53
54
        return $this;
55
    }
56
57
    /**
58
     * Email the employee's supervisor if a user has training that isn't complete yet.
59
     */
60
    public function emailSupervisor()
61
    {
62
        $supervisors = $this->getSupervisors();
63
64
        foreach ($supervisors as $supervisor) {
65
            $newNotes = new Collection();
66
67
            $this->trainingUsers->each(function ($item) use ($supervisor, $newNotes) {
68
                if ($item->user->supervisor_id == $supervisor->id) {
69
                    $newNotes->push($item);
70
                }
71
            });
72
73
            if (!$newNotes->isEmpty()) {
74
                Mail::to($supervisor)->send(new EmailSupervisorReminder($newNotes));
75
            }
76
        }
77
    }
78
79
    /**
80
     * Stores a list of incomplete training.
81
     */
82
    public function setTrainingUsers()
83
    {
84
        $this->trainingUsers = TrainingUser::with([
85
            'training', 'training.attachments', 'user', 'user.supervisor',
86
        ])
87
            ->where('due_date', '<=', Carbon::today()->addWeek())
88
            ->whereNull('completed_date')
89
            ->activeUsers()
90
            ->orderBy('due_date')
91
            ->get();
92
93
        return $this->trainingUsers;
94
    }
95
96
    /**
97
     * Get our Notes list.
98
     *
99
     * @return mixed
100
     */
101
    public function getList()
102
    {
103
        return $this->trainingUsers;
104
    }
105
106
    /**
107
     * Take a list of notes and return a list of supervisors related to those notes.
108
     *
109
     * @return Collection
110
     */
111
    private function getSupervisors()
112
    {
113
        $supervisors = new Collection();
114
        foreach ($this->trainingUsers as $trainingUser) {
115
            if ($trainingUser->user->supervisor) {
116
                $supervisors->push($trainingUser->user->supervisor);
117
            }
118
        }
119
120
        return $supervisors->unique();
121
    }
122
}
123