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

DutyUpcoming   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 101
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 4
dl 0
loc 101
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A build() 0 14 2
A generateICS() 0 53 3
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') {
1 ignored issue
show
Documentation introduced by
The property cycle does not exist on object<SET\Duty>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
46
            $subject = 'You have '.$this->duty->name." security check on $this->date.";
1 ignored issue
show
Documentation introduced by
The property name does not exist on object<SET\Duty>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
47
        } else {
48
            $subject = 'You have '.$this->duty->name." security check starting $this->date.";
1 ignored issue
show
Documentation introduced by
The property name does not exist on object<SET\Duty>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
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::where('name', 'report_address')->first();
66
67
        $rrule = '';
68
        if ($this->duty->cycle == 'weekly') {
69
            $rrule = 'RRULE:FREQ=DAILY;COUNT=5;INTERVAL=1;';
70
        } elseif ($this->duty->cycle == 'daily') {
71
            $rrule = 'RRULE:FREQ=DAILY;COUNT=1;INTERVAL=1;';
72
        }
73
74
        $filename = 'schedule.ics';
75
        $meetingDuration = (1800); // 30 minutes
76
        $time = 'T15:30:00.00';
77
        $meetingstamp = strtotime($date->toFormattedDateString().$time);
78
        $dtstart = gmdate('Ymd\THis\Z', $meetingstamp);
79
        $dtend = gmdate('Ymd\THis\Z', $meetingstamp + $meetingDuration);
80
        $todaystamp = gmdate('Ymd\THis\Z');
81
        $title = "You have $this->duty->name security check";
82
        $organizer = 'MAILTO:'.$reportAddress->secondary;
83
84
        // ICS
85
        $mail = [];
86
        $mail[0] = 'BEGIN:VCALENDAR';
87
        $mail[1] = 'PRODID:-//Microsoft Corporation//Outlook 11.0 MIMEDIR//EN';
88
        $mail[2] = 'VERSION:2.0';
89
        $mail[3] = 'CALSCALE:GREGORIAN';
90
        $mail[4] = 'METHOD:REQUEST';
91
        $mail[5] = 'BEGIN:VEVENT';
92
        $mail[6] = 'DTSTART;TZID=America/Chicago:'.$dtstart;
93
        $mail[7] = 'DTEND;TZID=America/Chicago:'.$dtend;
94
        $mail[8] = 'DTSTAMP;TZID=America/Chicago:'.$todaystamp;
95
        $mail[9] = 'UID:'.date('Ymd').'T'.date('His').'-'.rand().'@teamscci.com';
96
        $mail[10] = 'ORGANIZER;'.$organizer;
97
        $mail[11] = 'CREATED:'.$todaystamp;
98
        $mail[12] = 'LAST-MODIFIED:'.$todaystamp;
99
        $mail[14] = 'SEQUENCE:0';
100
        $mail[15] = 'STATUS:CONFIRMED';
101
        $mail[16] = 'SUMMARY:'.$title;
102
        $mail[17] = 'TRANSP:OPAQUE';
103
        $mail[18] = 'X-MICROSOFT-CDO-IMPORTANTCE:1';
104
        $mail[19] = $rrule;
105
        $mail[20] = 'END:VEVENT';
106
        $mail[21] = 'END:VCALENDAR';
107
108
        $mail = implode("\r\n", $mail);
109
        header('text/calendar');
110
        file_put_contents($filename, $mail);
111
112
        return $filename;
113
    }
114
}
115