Message::android()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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