1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace NotificationChannels\Pushover; |
4
|
|
|
|
5
|
|
|
class PushoverReceiver |
6
|
|
|
{ |
7
|
|
|
protected $key; |
8
|
|
|
protected $token; |
9
|
|
|
protected $devices = []; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* PushoverReceiver constructor. |
13
|
|
|
* @param $key User or group key. |
14
|
|
|
*/ |
15
|
12 |
|
protected function __construct($key) |
16
|
|
|
{ |
17
|
12 |
|
$this->key = $key; |
18
|
12 |
|
} |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Create new Pushover receiver with an user key. |
22
|
|
|
* |
23
|
|
|
* @param $userKey Pushover user key. |
24
|
|
|
* @return PushoverReceiver |
25
|
|
|
*/ |
26
|
12 |
|
public static function withUserKey($userKey) |
27
|
|
|
{ |
28
|
12 |
|
return new static($userKey); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Create new Pushover receiver with a group key. |
33
|
|
|
* |
34
|
|
|
* @param $groupKey Pushover group key. |
35
|
|
|
* @return PushoverReceiver |
36
|
|
|
*/ |
37
|
2 |
|
public static function withGroupKey($groupKey) |
38
|
|
|
{ |
39
|
|
|
// This has exactly the same behaviour as an user key, so we |
40
|
|
|
// will use the same factory method as for the user key. |
41
|
2 |
|
return self::withUserKey($groupKey); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Send the message to a specific device. |
46
|
|
|
* |
47
|
|
|
* @param array|string $device |
48
|
|
|
* @return PushoverReceiver |
49
|
|
|
*/ |
50
|
6 |
|
public function toDevice($device) |
51
|
|
|
{ |
52
|
6 |
|
if (is_array($device)) { |
53
|
1 |
|
$this->devices = array_merge($device, $this->devices); |
54
|
|
|
|
55
|
1 |
|
return $this; |
56
|
|
|
} |
57
|
5 |
|
$this->devices[] = $device; |
58
|
|
|
|
59
|
5 |
|
return $this; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Set the application token. |
64
|
|
|
* |
65
|
|
|
* @param $token |
66
|
|
|
* @return PushoverReceiver |
67
|
|
|
*/ |
68
|
2 |
|
public function withApplicationToken($token) |
69
|
|
|
{ |
70
|
2 |
|
$this->token = $token; |
71
|
|
|
|
72
|
2 |
|
return $this; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Get array representation of Pushover receiver. |
77
|
|
|
* |
78
|
|
|
* @return array |
79
|
|
|
*/ |
80
|
12 |
|
public function toArray() |
81
|
|
|
{ |
82
|
12 |
|
return array_merge([ |
83
|
12 |
|
'user' => $this->key, |
84
|
12 |
|
'device' => implode(',', $this->devices), |
85
|
12 |
|
], $this->token ? ['token' => $this->token] : []); |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|