Pusher::setToken()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 1
1
<?php
2
3
namespace Tzsk\Push;
4
5
use Gomoob\Pushwoosh\Model\Notification\Android;
6
use Gomoob\Pushwoosh\Model\Notification\IOS;
7
use Gomoob\Pushwoosh\Model\Notification\Notification;
8
use Gomoob\Pushwoosh\Model\Notification\Platform;
9
use Gomoob\Pushwoosh\Model\Request\CreateMessageRequest;
10
use Tzsk\Push\Factory\PusherFactory;
11
12
class Pusher
13
{
14
    /**
15
     * Title of the message.
16
     *
17
     * @var string
18
     */
19
    protected $title = "";
20
21
    /**
22
     * Body of the message.
23
     *
24
     * @var string
25
     */
26
    protected $body = "";
27
28
    /**
29
     * Badge of message.
30
     *
31
     * @var int
32
     */
33
    protected $badge = null;
34
35
    /**
36
     * Payload of the message.
37
     *
38
     * @var array
39
     */
40
    protected $payload = [];
41
42
    /**
43
     * Icon of the message.
44
     *
45
     * @var string
46
     */
47
    protected $icon = "";
48
49
    /**
50
     * Small Icon.
51
     *
52
     * @var null
53
     */
54
    protected $smallIcon = null;
55
56
    /**
57
     * Banner of the message.
58
     *
59
     * @var string
60
     */
61
    protected $banner = "";
62
63
    /**
64
     * Sond of the message.
65
     *
66
     * @var string
67
     */
68
    protected $sound = 'default';
69
70
    /**
71
     * Priority of the message.
72
     *
73
     * @var int
74
     */
75
    protected $priority = 1;
76
77
    /**
78
     * Vibration of the message.
79
     *
80
     * @var int
81
     */
82
    protected $vibration = 1;
83
84
    /**
85
     * Icon Background Color.
86
     * @var string
87
     */
88
    protected $ibc = "#ffffff";
89
90
    /**
91
     * Pushwoosh Instance.
92
     *
93
     * @var
94
     */
95
    protected $push;
96
97
    /**
98
     * Send to devices.
99
     *
100
     * @var array
101
     */
102
    protected $tokens = [];
103
104
    /**
105
     * PushWooshManager constructor.
106
     *
107
     * @param PusherFactory $factory
108
     */
109
    public function __construct(PusherFactory $factory)
110
    {
111
        $this->push = $factory->make();
112
113
        $this->icon = config('push.settings.icon');
114
        $this->title = config('push.settings.title');
115
        $this->body = config('push.settings.body');
116
        $this->vibration = config('push.settings.vibration');
117
        $this->sound = config('push.settings.sound');
118
        $this->priority = config('push.settings.priority');
119
        $this->ibc = config('push.settings.ibc');
120
    }
121
122
    /**
123
     * @param mixed $title
124
     * @return $this
125
     */
126
    public function setTitle($title)
127
    {
128
        $this->title = $title;
129
130
        return $this;
131
    }
132
133
    /**
134
     * @param mixed $body
135
     * @return $this
136
     */
137
    public function setBody($body)
138
    {
139
        $this->body = $body;
140
141
        return $this;
142
    }
143
144
    /**
145
     * @param mixed $badge
146
     * @return $this
147
     */
148
    public function setBadge($badge)
149
    {
150
        $this->badge = $badge;
151
152
        return $this;
153
    }
154
155
    /**
156
     * @param mixed $payload
157
     * @return $this
158
     */
159
    public function setPayload(array $payload)
160
    {
161
        $this->payload = $payload;
162
163
        return $this;
164
    }
165
166
    /**
167
     * @param mixed $icon
168
     * @return $this
169
     */
170
    public function setIcon($icon)
171
    {
172
        $this->icon = $icon;
173
174
        return $this;
175
    }
176
177
    /**
178
     * @param mixed $smallIcon
179
     * @return $this
180
     */
181
    public function setSmallIcon($smallIcon)
182
    {
183
        $this->smallIcon = $smallIcon;
184
185
        return $this;
186
    }
187
188
    /**
189
     * @param mixed $banner
190
     * @return $this
191
     */
192
    public function setBanner($banner)
193
    {
194
        $this->banner = $banner;
195
196
        return $this;
197
    }
198
199
    /**
200
     * @param string $sound
201
     * @return $this
202
     */
203
    public function setSound($sound)
204
    {
205
        $this->sound = $sound;
206
207
        return $this;
208
    }
209
210
    /**
211
     * @param int $priority
212
     * @return $this
213
     */
214
    public function setPriority($priority)
215
    {
216
        $this->priority = $priority;
217
218
        return $this;
219
    }
220
221
    /**
222
     * @param int $vibration
223
     * @return $this
224
     */
225
    public function setVibration($vibration)
226
    {
227
        $this->vibration = $vibration;
228
229
        return $this;
230
    }
231
232
    /**
233
     * @param string $ibc
234
     * @return $this
235
     */
236
    public function setIbc($ibc)
237
    {
238
        $this->ibc = $ibc;
239
240
        return $this;
241
    }
242
243
    /**
244
     * Set Device token.
245
     *
246
     * @param $token
247
     * @return $this
248
     */
249
    public function setToken($token) {
250
        $tokens = array_merge($this->tokens, [$token]);
251
        $this->tokens = $tokens;
252
253
        return $this;
254
    }
255
256
    /**
257
     * Set Device token array.
258
     * 
259
     * @param array $tokens
260
     * @return $this
261
     */
262
    public function setTokens(array $tokens) {
263
        $tokens = array_merge($this->tokens, $tokens);
264
        $this->tokens = $tokens;
265
266
        return $this;
267
    }
268
269
    /**
270
     * Get Android Settings.
271
     *
272
     * @return Android
273
     */
274
    protected function getAndroidSettings() {
275
276
        $android = Android::create()
277
            ->setBadges($this->badge)
278
            ->setIbc($this->ibc)
279
            ->setCustomIcon($this->icon)
280
            ->setHeader($this->title)
281
            ->setIcon($this->smallIcon)
282
            ->setPriority($this->priority)
283
            ->setSound($this->sound)
284
            ->setVibration($this->vibration);
285
        if ($this->banner) {
286
            $android->setBanner($this->banner);
287
        }
288
289
        return $android;
290
    }
291
292
    /**
293
     * Get IOS Settings.
294
     *
295
     * @return IOS
296
     */
297
    protected function getIosSettings() {
298
        $ios = IOS::create()->setBadges($this->badge);
299
300
        return $ios;
301
    }
302
303
    /**
304
     * @param $token
305
     * @return CreateMessageRequest
306
     */
307
    protected function getPushRequest($token)
308
    {
309
        $notification = Notification::create()
310
            ->setPlatforms([
311
                Platform::ios(),
312
                Platform::android(),
313
            ])
314
            ->setDevices($token)
315
            ->setIOS($this->getIosSettings())
316
            ->setAndroid($this->getAndroidSettings())
317
            ->setData($this->payload)
318
            ->setContent($this->body);
319
        $request = CreateMessageRequest::create()->addNotification($notification);
320
321
        return $request;
322
    }
323
324
    /**
325
     * Send Push to tokens.
326
     *
327
     * @return object
328
     */
329
    public function send($message, $callback) {
330
        $this->body = $message;
331
        
332
        call_user_func($callback, $this);
333
334
        $request = $this->getPushRequest($this->tokens);
335
        $response = $this->push->createMessage($request);
336
337
        return $response;
338
    }
339
340
}
341
342