Completed
Pull Request — master (#22)
by
unknown
02:38
created

PushoverReceiver   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 91
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 8
c 1
b 0
f 0
lcom 2
cbo 0
dl 0
loc 91
ccs 22
cts 22
cp 1
rs 10

6 Methods

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