Completed
Push — master ( 487e71...41b154 )
by claudio
14:58 queued 01:24
created

OkListener   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 100
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 92.45%

Importance

Changes 7
Bugs 2 Features 0
Metric Value
wmc 8
c 7
b 2
f 0
lcom 1
cbo 3
dl 0
loc 100
ccs 49
cts 53
cp 0.9245
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A handle() 0 14 2
A sendCompanyEmail() 0 7 1
A sendEmployeeEmail() 0 8 1
A sendPushs() 0 7 2
B sendPushNotification() 0 34 1
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
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...
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