1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace plunner\Listeners\Optimise; |
4
|
|
|
|
5
|
|
|
use plunner\Events\Optimise\OkEvent; |
6
|
|
|
use Illuminate\Queue\InteractsWithQueue; |
7
|
|
|
use Illuminate\Contracts\Queue\ShouldQueue; |
8
|
|
|
|
9
|
|
|
class OkListener |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* Create the event listener. |
13
|
|
|
* |
14
|
|
|
* @return void |
|
|
|
|
15
|
|
|
*/ |
16
|
2 |
|
public function __construct() |
17
|
|
|
{ |
18
|
|
|
// |
19
|
2 |
|
} |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Handle the event. |
23
|
|
|
* |
24
|
|
|
* @param OkEvent $event |
25
|
|
|
* @return void |
26
|
|
|
*/ |
27
|
2 |
|
public function handle(OkEvent $event) |
28
|
|
|
{ |
29
|
|
|
// |
30
|
2 |
|
\Log::info('Meeting correctly optimised (company id = '.$event->getCompany()->id.')'); |
31
|
2 |
|
$event->getCompany()->fresh(); |
32
|
|
|
//send email to company |
33
|
2 |
|
self::sendCompanyEmail($event->getCompany()->email); |
34
|
|
|
//send emails to employees |
35
|
2 |
|
$employees = $event->getCompany()->employees()->with('meetings')->get(); |
36
|
2 |
|
foreach($employees as $employee) |
37
|
2 |
|
self::sendEmployeeEmail($employee->email, $employee->meetings); |
38
|
2 |
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @param string $email |
42
|
|
|
*/ |
43
|
2 |
|
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
|
2 |
|
}); |
49
|
2 |
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @param string $email |
53
|
|
|
* @param \Illuminate\Support\Collection $meetings |
54
|
|
|
*/ |
55
|
|
|
static private function sendEmployeeEmail($email, $meetings) |
56
|
|
|
{ |
57
|
2 |
|
\Mail::queue('emails.optimise.ok.employee', ['meetings' => $meetings], function ($message) use($email) { |
58
|
|
|
$message->from(config('mail.from.address'), config('mail.from.name')); |
59
|
|
|
$message->to($email)->subject('Meetings of next week'); |
60
|
2 |
|
}); |
61
|
2 |
|
} |
62
|
|
|
} |
63
|
|
|
|
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.