Completed
Pull Request — master (#13)
by Casper
06:08
created

PushoverReceiver::toDevice()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 11
ccs 6
cts 6
cp 1
rs 9.4285
c 1
b 0
f 0
cc 2
eloc 6
nc 2
nop 1
crap 2
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 9
    protected function __construct($key)
15
    {
16 9
        $this->key = $key;
17 9
    }
18
19
    /**
20
     * Create new Pushover receiver with an user key.
21
     *
22
     * @param  $userKey  Pushover user key.
23
     * @return PushoverReceiver
24
     */
25 9
    public static function withUserKey($userKey)
26
    {
27 9
        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 1
    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 1
        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 5
    public function toDevice($device)
50
    {
51 5
        if (is_array($device)) {
52 1
            $this->devices = array_merge($device, $this->devices);
53
54 1
            return $this;
55
        }
56 4
        $this->devices[] = $device;
57
58 4
        return $this;
59
    }
60
61
    /**
62
     * Get array representation of Pushover receiver.
63
     *
64
     * @return array
65
     */
66 9
    public function toArray()
67
    {
68
        return [
69 9
            'user' => $this->key,
70 9
            'device' => implode(',', $this->devices),
71
        ];
72
    }
73
}
74