Completed
Push — master ( 201fcc...39022b )
by claudio
05:44
created

OkListener::sendEmployeeEmail()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1.064

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 7
ccs 3
cts 5
cp 0.6
rs 9.4286
cc 1
eloc 4
nc 1
nop 2
crap 1.064
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
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

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.

Loading history...
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