DutyUpcoming::generateICS()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 54

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 54
ccs 0
cts 47
cp 0
rs 9.0036
c 0
b 0
f 0
cc 3
nc 3
nop 0
crap 12

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace SET\Mail;
4
5
use Carbon\Carbon;
6
use Illuminate\Bus\Queueable;
7
use Illuminate\Contracts\Queue\ShouldQueue;
8
use Illuminate\Mail\Mailable;
9
use Illuminate\Queue\SerializesModels;
10
use SET\Duty;
11
use SET\Setting;
12
use SET\User;
13
14
class DutyUpcoming extends Mailable implements ShouldQueue
15
{
16
    use Queueable, SerializesModels;
17
18
    public $user;
19
    public $date;
20
    public $duty;
21
22
    /**
23
     * DutyUpcoming constructor.
24
     *
25
     * @param Duty $duty
26
     * @param User $user
27
     * @param $date
28
     */
29
    public function __construct(Duty $duty, User $user, $date)
30
    {
31
        $this->duty = $duty;
32
        $this->user = $user;
33
        $this->date = $date;
34
    }
35
36
    /**
37
     * Build the message.
38
     *
39
     * @return $this
40
     */
41
    public function build()
42
    {
43
        $filename = $this->generateICS();
44
45
        if ($this->duty->cycle = 'daily') {
46
            $subject = 'You have '.$this->duty->name." security check on $this->date.";
47
        } else {
48
            $subject = 'You have '.$this->duty->name." security check starting $this->date.";
49
        }
50
51
        return $this->view('emails.duty_future')
52
            ->subject($subject)
53
            ->attach($filename, ['mime' => 'text/calendar']);
54
    }
55
56
    /**
57
     * Make our ICS file.
58
     *
59
     * @return string
60
     */
61
    private function generateICS()
62
    {
63
        $date = Carbon::createFromFormat('Y-m-d', $this->date);
64
65
        $reportAddress = Setting::get('summary_recipient', null);
66
        $recipientEmails = User::whereIn('id', $reportAddress)->get()->pluck('email');
67
68
        $rrule = '';
69
        if ($this->duty->cycle == 'weekly') {
70
            $rrule = 'RRULE:FREQ=DAILY;COUNT=5;INTERVAL=1;';
71
        } elseif ($this->duty->cycle == 'daily') {
72
            $rrule = 'RRULE:FREQ=DAILY;COUNT=1;INTERVAL=1;';
73
        }
74
75
        $filename = 'schedule.ics';
76
        $meetingDuration = (1800); // 30 minutes
77
        $time = 'T15:30:00.00';
78
        $meetingstamp = strtotime($date->toFormattedDateString().$time);
79
        $dtstart = gmdate('Ymd\THis\Z', $meetingstamp);
80
        $dtend = gmdate('Ymd\THis\Z', $meetingstamp + $meetingDuration);
81
        $todaystamp = gmdate('Ymd\THis\Z');
82
        $title = "You have $this->duty->name security check";
83
        $organizer = 'MAILTO:'.$recipientEmails;
84
85
        // ICS
86
        $mail = [];
87
        $mail[0] = 'BEGIN:VCALENDAR';
88
        $mail[1] = 'PRODID:-//Microsoft Corporation//Outlook 11.0 MIMEDIR//EN';
89
        $mail[2] = 'VERSION:2.0';
90
        $mail[3] = 'CALSCALE:GREGORIAN';
91
        $mail[4] = 'METHOD:REQUEST';
92
        $mail[5] = 'BEGIN:VEVENT';
93
        $mail[6] = 'DTSTART;TZID=America/Chicago:'.$dtstart;
94
        $mail[7] = 'DTEND;TZID=America/Chicago:'.$dtend;
95
        $mail[8] = 'DTSTAMP;TZID=America/Chicago:'.$todaystamp;
96
        $mail[9] = 'UID:'.date('Ymd').'T'.date('His').'-'.rand().'@teamscci.com';
97
        $mail[10] = 'ORGANIZER;'.$organizer;
98
        $mail[11] = 'CREATED:'.$todaystamp;
99
        $mail[12] = 'LAST-MODIFIED:'.$todaystamp;
100
        $mail[14] = 'SEQUENCE:0';
101
        $mail[15] = 'STATUS:CONFIRMED';
102
        $mail[16] = 'SUMMARY:'.$title;
103
        $mail[17] = 'TRANSP:OPAQUE';
104
        $mail[18] = 'X-MICROSOFT-CDO-IMPORTANTCE:1';
105
        $mail[19] = $rrule;
106
        $mail[20] = 'END:VEVENT';
107
        $mail[21] = 'END:VCALENDAR';
108
109
        $mail = implode("\r\n", $mail);
110
        header('text/calendar');
111
        file_put_contents($filename, $mail);
112
113
        return $filename;
114
    }
115
}
116