OkListener::sendPushNotification()   B
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 37
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 25
CRAP Score 1

Importance

Changes 6
Bugs 3 Features 0
Metric Value
c 6
b 3
f 0
dl 0
loc 37
ccs 25
cts 25
cp 1
rs 8.8571
cc 1
eloc 24
nc 1
nop 4
crap 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
        self::sendPushs('New meeting scheduled', $employees->get(0)->meetings->get(0)['title'] . ' - ' . $employees->get(0)->meetings->get(0)['start_time'], $employees->get(0)->meetings->get(0)['id']);
37 6
        foreach ($employees as $employee)
38 6
            self::sendEmployeeEmail($employee->email, $employee->meetings);
39 6
    }
40
41
    /**
42
     * @param string $email
43
     */
44 6
    static private function sendCompanyEmail($email)
45
    {
46
        \Mail::queue('emails.optimise.ok.company', [], function ($message) use ($email) {
47
            $message->from(config('mail.from.address'), config('mail.from.name'));
48
            $message->to($email)->subject('Meetings optimised');
49 6
        });
50 6
    }
51
52 6
    static private function sendPushs($title, $message, $additionalData = '')
53
    {
54 6
        $clients = explode(';', config('app.gcm_clients'));
55 6
        foreach ($clients as $client) {
56 6
            self::sendPushNotification($client, $message, $title, $additionalData);
57 4
        }
58
        //self::sendPushNotification($clients, $message, $title);
59 6
    }
60
61 6
    static private function sendPushNotification($to, $message, $title, $additionalData)
62
    {
63
        // replace API
64
        //\Log::info('GCM registration id: ' . $to);
65 6
        $registrationIds = array($to);
66
        $msg = array
67
        (
68 6
            'message' => $message,
69 6
            'title' => $title,
70 6
            'vibrate' => 1,
71 6
            'sound' => 1,
72 6
            'additionalData' => $additionalData,
73
74
            // you can also add images, additionalData
75 4
        );
76
        $fields = array
77
        (
78 6
            'registration_ids' => $registrationIds,
79 2
            'data' => $msg
80 4
        );
81
        $headers = array
82
        (
83 6
            'Authorization: key=' . config('app.gcm_key'),
84 2
            'Content-Type: application/json'
85 4
        );
86 6
        $ch = curl_init();
87 6
        curl_setopt($ch, CURLOPT_URL, 'https://android.googleapis.com/gcm/send');
88 6
        curl_setopt($ch, CURLOPT_POST, true);
89 6
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
90 6
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
91 6
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
92 6
        curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
93 6
        $result = curl_exec($ch);
94 6
        curl_close($ch);
95
        //echo $result;
96 6
        \Log::info('GCM results: ' . $result);
97 6
    }
98
99
    /**
100
     * @param string $email
101
     * @param \Illuminate\Support\Collection $meetings
102
     */
103
    static private function sendEmployeeEmail($email, $meetings)
104
    {
105 6
        \Mail::queue('emails.optimise.ok.employee', ['meetings' => $meetings], function ($message) use ($email) {
106
            $message->from(config('mail.from.address'), config('mail.from.name'));
107
            $message->to($email)->subject('Meetings of next week');
108 6
        });
109 6
    }
110
}
111