Completed
Push — master ( 11fe7a...491c50 )
by Casper
02:52
created

PushoverReceiver::withGroupKey()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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