Completed
Push — master ( 6be938...3b6271 )
by Edgar
02:01
created

Message::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 5
cts 5
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 3
nc 2
nop 1
crap 2
1
<?php
2
namespace nstdio\notymo;
3
use nstdio\notymo\exception\UnsupportedNotificationType;
4
5
/**
6
 * Class Message
7
 *
8
 * @package nstdio\notymo
9
 * @author  Edgar Asatryan <[email protected]>
10
 */
11
class Message implements MessageInterface
12
{
13
    /**
14
     * @var string Push notification message.
15
     */
16
    private $message;
17
18
    /**
19
     * @var string|array User device tokens
20
     */
21
    private $token;
22
23
    /**
24
     * @var array
25
     */
26
    private $customData = array();
27
28
    /**
29
     * @var int
30
     */
31
    private $badge = 0;
32
33
    /**
34
     * @var string Sound of notification.
35
     */
36
    private $sound;
37
38
    /**
39
     * @var int
40
     */
41
    private $type;
42
43 14
    public function __construct($type = null)
44
    {
45 14
        if ($type !== null) {
46 1
            $this->setType($type);
47 1
        }
48 14
    }
49
50 7
    public function getMessage()
51
    {
52 7
        return $this->message;
53
    }
54
55 1
    public function setMessage($message)
56
    {
57 1
        $this->message = $message;
58 1
    }
59
60 4
    public function getToken()
61
    {
62 4
        return $this->token;
63
    }
64
65 4
    public function setToken($token)
66
    {
67 4
        if (is_array($token)) {
68 2
            $this->token = $token;
69 2
        } else {
70 3
            $this->token = strval($token);
71
        }
72 4
    }
73
74 1
    public function getCustomData()
75
    {
76 1
        return $this->customData;
77
    }
78
79 1
    public function setCustomData(array $data)
80
    {
81 1
        $this->customData = $data;
82 1
    }
83
84 6
    public function getBadge()
85
    {
86 6
        return $this->badge;
87
    }
88
89 1
    public function setBadge($badge)
90
    {
91 1
        $this->badge = $badge;
92 1
    }
93
94 6
    public function getSound()
95
    {
96 6
        return $this->sound;
97
    }
98
99 1
    public function setSound($sound)
100
    {
101 1
        $this->sound = $sound;
102 1
    }
103
104 1
    public function getType()
105
    {
106 1
        return $this->type;
107
    }
108
109 3
    public function setType($type)
110
    {
111 3
        if ($type !== self::TYPE_IOS && $type !== self::TYPE_ANDROID) {
112 1
            throw new UnsupportedNotificationType("Invalid message type : $type, please use one of MessageInterface constants.");
113
        }
114 2
        $this->type = $type;
115 2
    }
116
117 3
    public function addToken($token)
118
    {
119 3
        if ($this->token === null) {
120 1
            $this->token = array();
121 1
            $this->merge($token);
122 3
        } elseif (is_string($this->token)) {
123 1
            $oldToken = $this->token;
124 1
            $this->token = array();
125 1
            array_push($this->token, $oldToken, $token);
126 3
        } elseif (is_array($this->token)) {
127 2
            $this->merge($token);
128 2
        }
129
130 3
    }
131
132 3
    public function isMultiple()
133
    {
134 3
        return is_array($this->token);
135
    }
136
137
    /**
138
     * @param $token
139
     */
140 2
    private function merge($token)
141
    {
142 2
        if (is_array($token)) {
143 1
            $this->token = array_merge($this->token, $token);
144 1
        } else {
145 2
            $this->token[] = $token;
146
        }
147
    }
148
}