EmailTraining::handle()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 0
cts 10
cp 0
rs 9.9
c 0
b 0
f 0
cc 3
nc 2
nop 1
crap 12
1
<?php
2
3
namespace SET\Listeners;
4
5
use Carbon\Carbon;
6
use Illuminate\Contracts\Queue\ShouldQueue;
7
use Illuminate\Support\Facades\Mail;
8
use SET\Events\TrainingAssigned;
9
use SET\Setting;
10
11
/**
12
 * Class EmailTraining.
13
 */
14
class EmailTraining implements ShouldQueue
15
{
16
    /**
17
     * Email the employee about the training. Then return a notification to the user that an email was sent.
18
     *
19
     * @param TrainingAssigned $event
20
     *
21
     * @return void
22
     */
23
    public function handle(TrainingAssigned $event)
24
    {
25
        $trainingUser = $event->getTrainingUser();
26
        $user = $trainingUser->user;
27
        $training = $trainingUser->training;
28
        $dueDate = $this->makeDueDatePretty($trainingUser->due_date);
29
30
        if (is_null($trainingUser->completed_date) and $training->administrative == 0) {
31
            $this->sendEmail($user, $training, $dueDate, $trainingUser);
32
        }
33
    }
34
35
    /**
36
     * @param string $date
37
     *
38
     * @return string
39
     */
40
    private function makeDueDatePretty($date)
41
    {
42
        return Carbon::createFromFormat('Y-m-d', $date)->toFormattedDateString();
43
    }
44
45
    /**
46
     * @param $user
47
     * @param $training
48
     * @param string $dueDate
49
     * @param $trainingUser
50
     */
51
    private function sendEmail($user, $training, $dueDate, $trainingUser)
52
    {
53
        $reportAddress = Setting::get('mail_from_address', '[email protected]');
54
        $reportName = Setting::get('mail_from_name', 'SET-yourcompany');
55
        Mail::send(
56
            'emails.training',
57
            [
58
                'user'          => $user,
59
                'training'      => $training,
60
                'due_date'      => $dueDate,
61
                'trainingUser'  => $trainingUser,
62
                'reportAddress' => $reportAddress,
63
                'reportName'    => $reportName,
64
            ],
65
            function ($m) use ($user, $training, $reportAddress, $reportName) {
66
                $m->from($reportAddress, $reportName);
67
                $m->to($user->email, $user->userFullName)->subject($training->name.' was assigned to you.');
68
69
                //ATTACH FILES
70 View Code Duplication
                foreach ($training->attachments as $file) {
71
                    if (!$file->admin_only) {
72
                        $path = 'app/training_'.$file->imageable_id.'/'.$file->filename;
73
                        $m->attach(storage_path($path), ['as' => $file->filename, 'mime' => $file->mime]);
74
                    }
75
                }
76
            } // end $m function
77
        );
78
    }
79
}
80