Completed
Push — master ( 326539...af9954 )
by Mark
04:23 queued 40s
created

IonicPushMessage   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 228
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 0

Test Coverage

Coverage 78%

Importance

Changes 14
Bugs 3 Features 4
Metric Value
wmc 21
c 14
b 3
f 4
lcom 2
cbo 0
dl 0
loc 228
ccs 39
cts 50
cp 0.78
rs 10

12 Methods

Rating   Name   Duplication   Size   Complexity  
A create() 0 4 1
A __construct() 0 4 1
A sendTo() 0 6 1
A message() 0 6 1
A sound() 0 6 1
A payload() 0 6 1
A title() 0 6 1
A getSendToType() 0 4 1
B __call() 0 18 5
A allowedAndroidOptions() 0 17 1
A allowediOSOptions() 0 13 1
B toArray() 0 31 6
1
<?php
2
3
namespace NotificationChannels\IonicPushNotifications;
4
5
class IonicPushMessage
6
{
7
    /** @var string */
8
    public $sendTo = 'tokens';
9
10
    /** @var string */
11
    public $profile;
12
13
    /** @var string */
14
    public $title = '';
15
16
    /** @var string */
17
    public $message = '';
18
19
    /** @var string */
20
    public $sound = '';
21
22
    /** @var array */
23
    public $payload = [];
24
25
    /** @var array */
26
    public $iosData = [];
27
28
    /** @var array */
29
    public $androidData = [];
30
31
    /**
32
     * @param array $data
0 ignored issues
show
Bug introduced by
There is no parameter named $data. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
33
     *
34
     * @return static
35
     */
36 3
    public static function create($profile)
37
    {
38 3
        return new static($profile);
39
    }
40
41
    /**
42
     * @param string $profile
43
     */
44 11
    public function __construct($profile)
45
    {
46 11
        $this->profile = $profile;
47 11
    }
48
49
    /**
50
     * Set the method of targeting users - tokens (default), user_ids, or emails.
51
     *
52
     * @param  string  $profile
0 ignored issues
show
Bug introduced by
There is no parameter named $profile. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
53
     *
54
     * @return $this
55
     */
56
    public function sendTo($sendTo)
57
    {
58
        $this->sendTo = $sendTo;
59
60
        return $this;
61
    }
62
63
    /**
64
     * Set the title.
65
     *
66
     * @param  string  $title
67
     *
68
     * @return $this
69
     */
70 1
    public function title($title)
71
    {
72 1
        $this->title = $title;
73
74 1
        return $this;
75
    }
76
77
    /**
78
     * Set the message.
79
     *
80
     * @param  string  $message
81
     *
82
     * @return $this
83
     */
84 3
    public function message($message)
85
    {
86 3
        $this->message = $message;
87
88 3
        return $this;
89
    }
90
91
    /**
92
     * Set the security sound to use.
93
     *
94
     * @param  string  $sound
95
     *
96
     * @return $this
97
     */
98
    public function sound($sound)
99
    {
100
        $this->sound = $sound;
101
102
        return $this;
103
    }
104
105
    /**
106
     * Send custom key/value data with your notifications.
107
     *
108
     * @param  array  $payload
109
     *
110
     * @return $this
111
     */
112
    public function payload($payload)
113
    {
114
        $this->payload = $payload;
115
116
        return $this;
117
    }
118
119
    /**
120
     * Dynamically add device specific data.
121
     *
122
     * @param string $method
123
     * @param array  $args
124
     *
125
     * @return object
126
     */
127 6
    public function __call($method, $args)
128
    {
129 6
        if (substr($method, 0, 3) == 'ios') {
130 5
            $key = snake_case(substr($method, 3));
131
132 5
            if (in_array($key, $this->allowediOSOptions())) {
133 5
                $this->iosData[$key] = $args[0];
134
            }
135 3
        } elseif (substr($method, 0, 7) == 'android') {
136 1
            $key = snake_case(substr($method, 7));
137
138 1
            if (in_array($key, $this->allowedAndroidOptions())) {
139 1
                $this->androidData[$key] = $args[0];
140
            }
141
        }
142
143 6
        return $this;
144
    }
145
146
    /**
147
     * Get the method we want to use to send messages.
148
     *
149
     * @return string
150
     */
151 3
    public function getSendToType()
152
    {
153 3
        return $this->sendTo;
154
    }
155
156
    /**
157
     * List of allowed Android options.
158
     *
159
     * @return array
160
     */
161 1
    public function allowedAndroidOptions()
162
    {
163
        return [
164 1
            'collapse_key',
165
            'content_available',
166
            'data',
167
            'delay_while_idle',
168
            'icon',
169
            'icon_color',
170
            'message',
171
            'priortiy',
172
            'sound',
173
            'tag',
174
            'time_to_live',
175
            'title',
176
        ];
177
    }
178
179
    /**
180
     * List of allowed iOS options.
181
     *
182
     * @return array
183
     */
184 5
    public function allowediOSOptions()
185
    {
186
        return [
187 5
            'message',
188
            'title',
189
            'badge',
190
            'payload',
191
            'sound',
192
            'priortiy',
193
            'expire',
194
            'content_available',
195
        ];
196
    }
197
198
    /**
199
     * @return array
200
     */
201 10
    public function toArray()
202
    {
203
        $data = [
204 10
            'profile' => $this->profile,
205
            'notification' => [
206 10
                'message' => $this->message,
207
            ],
208
        ];
209
210 10
        if (! empty($this->title)) {
211 1
            $data['notification']['title'] = $this->title;
212
        }
213
214 10
        if (! empty($this->sound)) {
215
            $data['notification']['sound'] = $this->sound;
216
        }
217
218 10
        if (! empty($this->iosData)) {
219 4
            $data['notification']['ios'] = $this->iosData;
220
        }
221
222 10
        if (! empty($this->androidData)) {
223 1
            $data['notification']['android'] = $this->androidData;
224
        }
225
226 10
        if (! empty($this->payload)) {
227
            $data['notification']['payload'] = $this->payload;
228
        }
229
230 10
        return $data;
231
    }
232
}
233