Passed
Push — master ( 7383db...8c584c )
by Ferry
03:24
created

FCM::send()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 21
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 16
nc 2
nop 0
dl 0
loc 21
rs 9.7333
c 0
b 0
f 0
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: User
5
 * Date: 4/28/2019
6
 * Time: 10:30 PM
7
 */
8
9
namespace crocodicstudio\crudbooster\helpers;
10
11
12
class FCM
13
{
14
    private static $title;
15
    private $message;
16
    private $data;
17
    private $fields;
18
19
    public static function title($title) {
20
        static::$title = $title;
0 ignored issues
show
Bug introduced by
Since $title is declared private, accessing it with static will lead to errors in possible sub-classes; you can either use self, or increase the visibility of $title to at least protected.
Loading history...
21
        return new static;
22
    }
23
24
    public function message($message) {
25
        $this->message = $message;
26
        return $this;
27
    }
28
29
    public function data(array $data) {
30
        $this->data = $data;
31
        return $this;
32
    }
33
34
    public function tokenIOS(array $tokens) {
35
        $data['title'] = static::$title;
0 ignored issues
show
Comprehensibility Best Practice introduced by
$data was never initialized. Although not strictly required by PHP, it is generally a good practice to add $data = array(); before regardless.
Loading history...
Bug introduced by
Since $title is declared private, accessing it with static will lead to errors in possible sub-classes; you can either use self, or increase the visibility of $title to at least protected.
Loading history...
36
        $data['message'] = $this->message;
37
        $this->fields = [
38
            'registration_ids' => $tokens,
39
            'data' => $this->data,
40
            'content_available' => true,
41
            'notification' => [
42
                'sound' => 'default',
43
                'badge' => 0,
44
                'title' => trim(strip_tags(static::$title)),
45
                'body' => trim(strip_tags($this->message)),
46
            ],
47
            'priority' => 'high',
48
        ];
49
        return $this;
50
    }
51
52
    public function tokenAndroid(array $tokens) {
53
        $data['title'] = static::$title;
0 ignored issues
show
Comprehensibility Best Practice introduced by
$data was never initialized. Although not strictly required by PHP, it is generally a good practice to add $data = array(); before regardless.
Loading history...
Bug introduced by
Since $title is declared private, accessing it with static will lead to errors in possible sub-classes; you can either use self, or increase the visibility of $title to at least protected.
Loading history...
54
        $data['message'] = $this->message;
55
        $this->fields = [
56
            'registration_ids' => $tokens,
57
            'data' => $this->data,
58
            'content_available' => true,
59
            'priority' => 'high',
60
        ];
61
        return $this;
62
    }
63
64
    public function send() {
65
        $url = 'https://fcm.googleapis.com/fcm/send';
66
        $headers = [
67
            'Authorization:key='.cbConfig('GOOGLE_FCM_SERVER_KEY'),
68
            'Content-Type:application/json',
69
        ];
70
71
        if(static::$title && $this->message) {
0 ignored issues
show
Bug introduced by
Since $title is declared private, accessing it with static will lead to errors in possible sub-classes; you can either use self, or increase the visibility of $title to at least protected.
Loading history...
72
            $ch = curl_init($url);
73
            curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
0 ignored issues
show
Bug introduced by
It seems like $ch can also be of type false; however, parameter $ch of curl_setopt() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

73
            curl_setopt(/** @scrutinizer ignore-type */ $ch, CURLOPT_HTTPHEADER, $headers);
Loading history...
74
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
75
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
76
            curl_setopt($ch, CURLOPT_POST, 1);
77
            curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($this->fields));
78
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
79
            $result = curl_exec($ch);
0 ignored issues
show
Bug introduced by
It seems like $ch can also be of type false; however, parameter $ch of curl_exec() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

79
            $result = curl_exec(/** @scrutinizer ignore-type */ $ch);
Loading history...
80
            curl_close($ch);
0 ignored issues
show
Bug introduced by
It seems like $ch can also be of type false; however, parameter $ch of curl_close() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

80
            curl_close(/** @scrutinizer ignore-type */ $ch);
Loading history...
81
            return $result;
82
        }
83
        
84
        return null;
85
    }
86
87
}