These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | namespace NotificationChannels\PusherPushNotifications; |
||
4 | |||
5 | use Illuminate\Support\Arr; |
||
6 | |||
7 | class Message |
||
8 | { |
||
9 | /** |
||
10 | * The device platform (iOS/Android). |
||
11 | * |
||
12 | * @var string |
||
13 | */ |
||
14 | protected $platform = 'iOS'; |
||
15 | |||
16 | /** |
||
17 | * The message title. |
||
18 | * |
||
19 | * @var string |
||
20 | */ |
||
21 | protected $title; |
||
22 | |||
23 | /** |
||
24 | * The message body. |
||
25 | * |
||
26 | * @var string |
||
27 | */ |
||
28 | protected $body; |
||
29 | |||
30 | /** |
||
31 | * The phone number the message should be sent from. |
||
32 | * |
||
33 | * @var string |
||
34 | */ |
||
35 | protected $sound = 'default'; |
||
36 | |||
37 | /** |
||
38 | * The message icon (Android). |
||
39 | * |
||
40 | * @var string |
||
41 | */ |
||
42 | protected $icon; |
||
43 | |||
44 | /** |
||
45 | * The number to display next to the app icon (iOS). |
||
46 | * |
||
47 | * @var int |
||
48 | */ |
||
49 | protected $badge; |
||
50 | |||
51 | /** |
||
52 | * Options. |
||
53 | * |
||
54 | * @var array |
||
55 | */ |
||
56 | protected $options = []; |
||
57 | |||
58 | /** |
||
59 | * @param string $body |
||
60 | */ |
||
61 | public function __construct($body = '') |
||
62 | { |
||
63 | $this->body = $body; |
||
64 | } |
||
65 | |||
66 | /** |
||
67 | * Set the platform [iOS/Android]. |
||
68 | * |
||
69 | * @param string $value |
||
70 | * |
||
71 | * @return $this |
||
72 | */ |
||
73 | public function platform($value) |
||
74 | { |
||
75 | if (!in_array($value, ['iOS', 'Android'])) { |
||
76 | throw new \InvalidArgumentException('Invalid platform provided.'); |
||
77 | } |
||
78 | |||
79 | $this->platform = $value; |
||
80 | |||
81 | return $this; |
||
82 | } |
||
83 | |||
84 | /** |
||
85 | * Set the platform to iOS. |
||
86 | * |
||
87 | * @return $this |
||
88 | */ |
||
89 | public function iOS() |
||
90 | { |
||
91 | $this->platform = 'iOS'; |
||
92 | |||
93 | return $this; |
||
94 | } |
||
95 | |||
96 | /** |
||
97 | * Set the platform to Android. |
||
98 | * |
||
99 | * @return $this |
||
100 | */ |
||
101 | public function android() |
||
102 | { |
||
103 | $this->platform = 'Android'; |
||
104 | |||
105 | return $this; |
||
106 | } |
||
107 | |||
108 | /** |
||
109 | * Set the message title. |
||
110 | * |
||
111 | * @param string $value |
||
112 | * |
||
113 | * @return $this |
||
114 | */ |
||
115 | public function title($value) |
||
116 | { |
||
117 | $this->title = $value; |
||
118 | |||
119 | return $this; |
||
120 | } |
||
121 | |||
122 | /** |
||
123 | * Set the message body. |
||
124 | * |
||
125 | * @param string $value |
||
126 | * |
||
127 | * @return $this |
||
128 | */ |
||
129 | public function body($value) |
||
130 | { |
||
131 | $this->body = $value; |
||
132 | |||
133 | return $this; |
||
134 | } |
||
135 | |||
136 | /** |
||
137 | * Set the message sound (Android). |
||
138 | * |
||
139 | * @param string $value |
||
140 | * |
||
141 | * @return $this |
||
142 | */ |
||
143 | public function sound($value) |
||
144 | { |
||
145 | $this->sound = $value; |
||
146 | |||
147 | return $this; |
||
148 | } |
||
149 | |||
150 | /** |
||
151 | * Set the message icon (Android). |
||
152 | * |
||
153 | * @param string $value |
||
154 | * |
||
155 | * @return $this |
||
156 | */ |
||
157 | public function icon($value) |
||
158 | { |
||
159 | $this->icon = $value; |
||
160 | |||
161 | return $this; |
||
162 | } |
||
163 | |||
164 | /** |
||
165 | * Set the message badge (iOS). |
||
166 | * |
||
167 | * @param int $value |
||
168 | * |
||
169 | * @return $this |
||
170 | */ |
||
171 | public function badge($value) |
||
172 | { |
||
173 | $this->badge = (int) $value; |
||
174 | |||
175 | return $this; |
||
176 | } |
||
177 | |||
178 | /** |
||
179 | * @param string $key |
||
180 | * @param string $value |
||
181 | * |
||
182 | * @return $this |
||
183 | */ |
||
184 | public function setOption($key, $value) |
||
185 | { |
||
186 | $this->options[$key] = $value; |
||
187 | |||
188 | return $this; |
||
189 | } |
||
190 | |||
191 | /** |
||
192 | * Get an array representation of the message. |
||
193 | * |
||
194 | * @return array |
||
195 | */ |
||
196 | public function toArray() |
||
197 | { |
||
198 | return $this->platform === 'iOS' |
||
199 | ? $this->toiOS() |
||
200 | : $this->toAndroid(); |
||
201 | } |
||
202 | |||
203 | /** |
||
204 | * Format the message for iOS. |
||
205 | * |
||
206 | * @return array |
||
207 | */ |
||
208 | View Code Duplication | public function toiOS() |
|
0 ignored issues
–
show
|
|||
209 | { |
||
210 | $message = [ |
||
211 | 'apns' => [ |
||
212 | 'aps' => [ |
||
213 | 'alert' => [ |
||
214 | 'title' => $this->title, |
||
215 | 'body' => $this->body, |
||
216 | ], |
||
217 | 'sound' => $this->sound, |
||
218 | 'badge' => $this->badge, |
||
219 | ], |
||
220 | ], |
||
221 | ]; |
||
222 | |||
223 | foreach ($this->options as $option => $value) { |
||
224 | Arr::set($message, $option, $value); |
||
225 | } |
||
226 | |||
227 | return $message; |
||
228 | } |
||
229 | |||
230 | /** |
||
231 | * Format the message for Android. |
||
232 | * |
||
233 | * @return array |
||
234 | */ |
||
235 | View Code Duplication | public function toAndroid() |
|
0 ignored issues
–
show
This method seems to be duplicated in your project.
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation. You can also find more detailed suggestions in the “Code” section of your repository.
Loading history...
|
|||
236 | { |
||
237 | $message = [ |
||
238 | 'gcm' => [ |
||
239 | 'notification' => [ |
||
240 | 'title' => $this->title, |
||
241 | 'body' => $this->body, |
||
242 | 'sound' => $this->sound, |
||
243 | 'icon' => $this->icon, |
||
244 | ], |
||
245 | ], |
||
246 | ]; |
||
247 | |||
248 | foreach ($this->options as $option => $value) { |
||
249 | Arr::set($message, $option, $value); |
||
250 | } |
||
251 | |||
252 | return $message; |
||
253 | } |
||
254 | } |
||
255 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.