1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace plunner\Listeners\Optimise; |
4
|
|
|
|
5
|
|
|
use plunner\Events\Optimise\OkEvent; |
6
|
|
|
|
7
|
|
|
class OkListener |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* Create the event listener. |
11
|
|
|
* |
12
|
|
|
* @return void |
|
|
|
|
13
|
|
|
*/ |
14
|
6 |
|
public function __construct() |
15
|
|
|
{ |
16
|
|
|
// |
17
|
6 |
|
} |
18
|
|
|
|
19
|
|
|
static private function sendPushNotification($to, $message, $title) |
|
|
|
|
20
|
|
|
{ |
21
|
|
|
// replace API |
22
|
|
|
$registrationIds = array($to); |
23
|
|
|
$msg = array |
24
|
|
|
( |
25
|
|
|
'message' => $message, |
26
|
|
|
'title' => $title, |
27
|
|
|
'vibrate' => 1, |
28
|
|
|
'sound' => 1 |
29
|
|
|
|
30
|
|
|
// you can also add images, additionalData |
31
|
|
|
); |
32
|
|
|
$fields = array |
33
|
|
|
( |
34
|
|
|
'registration_ids' => $registrationIds, |
35
|
|
|
'data' => $msg |
36
|
|
|
); |
37
|
|
|
$headers = array |
38
|
|
|
( |
39
|
|
|
'Authorization: key=' . config('app.gcm_key'), |
40
|
|
|
'Content-Type: application/json' |
41
|
|
|
); |
42
|
|
|
$ch = curl_init(); |
43
|
|
|
curl_setopt($ch, CURLOPT_URL, 'https://android.googleapis.com/gcm/send'); |
44
|
|
|
curl_setopt($ch, CURLOPT_POST, true); |
45
|
|
|
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); |
46
|
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
47
|
|
|
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); |
48
|
|
|
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields)); |
49
|
|
|
$result = curl_exec($ch); |
50
|
|
|
curl_close($ch); |
51
|
|
|
echo $result; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Handle the event. |
56
|
|
|
* |
57
|
|
|
* @param OkEvent $event |
58
|
|
|
* @return void |
59
|
|
|
*/ |
60
|
6 |
|
public function handle(OkEvent $event) |
61
|
|
|
{ |
62
|
|
|
// |
63
|
6 |
|
\Log::info('Meeting correctly optimised (company id = ' . $event->getCompany()->id . ')'); |
64
|
6 |
|
$company = $event->getCompany()->fresh(); |
65
|
|
|
//send email to company |
66
|
6 |
|
self::sendCompanyEmail($company->email); |
67
|
|
|
//send emails to employees |
68
|
|
|
$employees = $company->employees()->with(['meetings'=>function($query){ |
69
|
6 |
|
$query->where('start_time', '>=', new \DateTime()); |
70
|
6 |
|
}])->get(); |
71
|
6 |
|
foreach ($employees as $employee) |
72
|
6 |
|
self::sendEmployeeEmail($employee->email, $employee->meetings); |
73
|
6 |
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @param string $email |
77
|
|
|
*/ |
78
|
6 |
|
static private function sendCompanyEmail($email) |
79
|
|
|
{ |
80
|
|
|
\Mail::queue('emails.optimise.ok.company', [], function ($message) use ($email) { |
81
|
|
|
$message->from(config('mail.from.address'), config('mail.from.name')); |
82
|
|
|
$message->to($email)->subject('Meetings optimised'); |
83
|
6 |
|
}); |
84
|
6 |
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @param string $email |
88
|
|
|
* @param \Illuminate\Support\Collection $meetings |
89
|
|
|
*/ |
90
|
|
|
static private function sendEmployeeEmail($email, $meetings) |
91
|
|
|
{ |
92
|
6 |
|
\Mail::queue('emails.optimise.ok.employee', ['meetings' => $meetings], function ($message) use ($email) { |
93
|
|
|
$message->from(config('mail.from.address'), config('mail.from.name')); |
94
|
|
|
$message->to($email)->subject('Meetings of next week'); |
95
|
6 |
|
}); |
96
|
6 |
|
} |
97
|
|
|
} |
98
|
|
|
|
Adding a
@return
annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.