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

PushoverReceiver::toArray()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 7
ccs 3
cts 3
cp 1
rs 9.4285
c 1
b 0
f 0
cc 1
eloc 4
nc 1
nop 0
crap 1
1
<?php
2
namespace NotificationChannels\Pushover;
3
4
5
class PushoverReceiver
0 ignored issues
show
Coding Style introduced by
Since you have declared the constructor as private, maybe you should also declare the class as final.
Loading history...
6
{
7
    protected $key;
8
    protected $devices = [];
9
10
    /**
11
     * PushoverReceiver constructor.
12
     * @param  $key  User or group key.
13
     */
14 9
    private 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 9
        return new static($userKey);
27
    }
28
29
    /**
30
     * Create new Pushover receiver with a group key.
31
     *
32
     * @param  $groupKey  Pushover group key.
33
     * @return PushoverReceiver
34
     */
35 1
    public static function withGroupKey($groupKey) {
36
        // This has exactly the same behaviour as an user key, so we
37
        // will use the same factory method as for the user key.
38 1
        return self::withUserKey($groupKey);
39
    }
40
41
    /**
42
     * Send the message to a specific device.
43
     *
44
     * @param  array|string  $device
45
     * @return PushoverReceiver
46
     */
47 5
    public function toDevice($device) {
48 5
        if (is_array($device)) {
49 1
            $this->devices = array_merge($device, $this->devices);
50
51 1
            return $this;
52
        }
53 4
        $this->devices[] = $device;
54
55 4
        return $this;
56
    }
57
58
    /**
59
     * Get array representation of Pushover receiver.
60
     *
61
     * @return array
62
     */
63 9
    public function toArray()
64
    {
65
        return [
66 9
            'user' => $this->key,
67 9
            'device' => implode(',', $this->devices)
68
        ];
69
    }
70
71
}