EmailTraining   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 66
Duplicated Lines 9.09 %

Coupling/Cohesion

Components 0
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 7
lcom 0
cbo 3
dl 6
loc 66
ccs 0
cts 35
cp 0
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A handle() 0 11 3
A makeDueDatePretty() 0 4 1
A sendEmail() 6 28 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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