|
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
|
|
|
/** |
|
20
|
|
|
* Handle the event. |
|
21
|
|
|
* |
|
22
|
|
|
* @param OkEvent $event |
|
23
|
|
|
* @return void |
|
24
|
|
|
*/ |
|
25
|
6 |
|
public function handle(OkEvent $event) |
|
26
|
|
|
{ |
|
27
|
|
|
// |
|
28
|
6 |
|
\Log::info('Meeting correctly optimised (company id = ' . $event->getCompany()->id . ')'); |
|
29
|
6 |
|
$company = $event->getCompany()->fresh(); |
|
30
|
|
|
//send email to company |
|
31
|
6 |
|
self::sendCompanyEmail($company->email); |
|
32
|
|
|
//send emails to employees |
|
33
|
|
|
$employees = $company->employees()->with(['meetings'=>function($query){ |
|
34
|
6 |
|
$query->where('start_time', '>=', new \DateTime()); |
|
35
|
6 |
|
}])->get(); |
|
36
|
6 |
|
foreach ($employees as $employee) |
|
37
|
6 |
|
self::sendEmployeeEmail($employee->email, $employee->meetings); |
|
38
|
3 |
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @param string $email |
|
42
|
|
|
*/ |
|
43
|
6 |
|
static private function sendCompanyEmail($email) |
|
44
|
|
|
{ |
|
45
|
|
|
\Mail::queue('emails.optimise.ok.company', [], function ($message) use ($email) { |
|
46
|
|
|
$message->from(config('mail.from.address'), config('mail.from.name')); |
|
47
|
|
|
$message->to($email)->subject('Meetings optimised'); |
|
48
|
6 |
|
}); |
|
49
|
6 |
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @param string $email |
|
53
|
|
|
* @param \Illuminate\Support\Collection $meetings |
|
54
|
|
|
*/ |
|
55
|
6 |
|
static private function sendEmployeeEmail($email, $meetings) |
|
56
|
|
|
{ |
|
57
|
6 |
|
self::sendPushs('New meeting scheduled', $meetings[0]['title'] . ' - ' . $meetings[0]['start_time']); |
|
58
|
3 |
|
\Mail::queue('emails.optimise.ok.employee', ['meetings' => $meetings], function ($message) use ($email) { |
|
59
|
|
|
$message->from(config('mail.from.address'), config('mail.from.name')); |
|
60
|
|
|
$message->to($email)->subject('Meetings of next week'); |
|
61
|
3 |
|
}); |
|
62
|
3 |
|
} |
|
63
|
|
|
|
|
64
|
3 |
|
static private function sendPushs($title, $message) |
|
65
|
|
|
{ |
|
66
|
3 |
|
$clients = explode(';', config('app.gcm_key')); |
|
67
|
3 |
|
foreach ($clients as $client) { |
|
68
|
3 |
|
self::sendPushNotification($client, $message, $title); |
|
69
|
2 |
|
} |
|
70
|
3 |
|
} |
|
71
|
|
|
|
|
72
|
3 |
|
static private function sendPushNotification($to, $message, $title) |
|
73
|
|
|
{ |
|
74
|
|
|
// replace API |
|
75
|
3 |
|
$registrationIds = array($to); |
|
76
|
|
|
$msg = array |
|
77
|
|
|
( |
|
78
|
3 |
|
'message' => $message, |
|
79
|
3 |
|
'title' => $title, |
|
80
|
3 |
|
'vibrate' => 1, |
|
81
|
1 |
|
'sound' => 1 |
|
82
|
|
|
|
|
83
|
|
|
// you can also add images, additionalData |
|
84
|
2 |
|
); |
|
85
|
|
|
$fields = array |
|
86
|
|
|
( |
|
87
|
3 |
|
'registration_ids' => $registrationIds, |
|
88
|
1 |
|
'data' => $msg |
|
89
|
2 |
|
); |
|
90
|
|
|
$headers = array |
|
91
|
|
|
( |
|
92
|
3 |
|
'Authorization: key=' . config('app.gcm_key'), |
|
93
|
1 |
|
'Content-Type: application/json' |
|
94
|
2 |
|
); |
|
95
|
3 |
|
$ch = curl_init(); |
|
96
|
3 |
|
curl_setopt($ch, CURLOPT_URL, 'https://android.googleapis.com/gcm/send'); |
|
97
|
3 |
|
curl_setopt($ch, CURLOPT_POST, true); |
|
98
|
3 |
|
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); |
|
99
|
3 |
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
|
100
|
3 |
|
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); |
|
101
|
3 |
|
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields)); |
|
102
|
3 |
|
$result = curl_exec($ch); |
|
103
|
3 |
|
curl_close($ch); |
|
104
|
3 |
|
echo $result; |
|
105
|
3 |
|
} |
|
106
|
|
|
} |
|
107
|
|
|
|
Adding a
@returnannotation 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.