Message::setTokens()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
crap 1
1
<?php
2
3
/*
4
 * This file is part of the MobileNotif package.
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
namespace LinkValue\MobileNotif\Model;
11
12
/**
13
 * Message
14
 * Definition of message for push notification.
15
 *
16
 * @author  Jamal Youssefi <[email protected]>
17
 * @author  Valentin Coulon <[email protected]>
18
 */
19
abstract class Message
20
{
21
    /**
22
     * The devices token.
23
     *
24
     * @var array
25
     */
26
    protected $tokens;
27
28
    /**
29
     * Constructor.
30
     */
31 106
    public function __construct()
32
    {
33 106
        $this->tokens = array();
34 106
    }
35
36
    /**
37
     * Get the Payload message to push
38
     *
39
     * @return array
40
     */
41
    abstract public function getPayload();
42
43
    /**
44
     * Get the Payload message to push converted in json 
45
     *
46
     * @return string json payload message
47
     */
48 4
    public function getPayloadAsJson()
49
    {
50 4
        return json_encode($this->getPayload());
51
    }
52
53
    /**
54
     * Get the list token devices.
55
     *
56
     * @return array
57
     */
58 10
    public function getTokens()
59
    {
60 10
        return $this->tokens;
61
    }
62
63
    /**
64
     * Set the list of token devices.
65
     *
66
     * @param array $tokens
67
     *
68
     * @return self
69
     */
70 4
    public function setTokens(array $tokens)
71
    {
72 4
        $this->tokens = array_unique($tokens);
73
74 4
        return $this;
75
    }
76
77
    /**
78
     * Add a token device.
79
     *
80
     * @param string $token
81
     *
82
     * @return self
83
     */
84 10
    public function addToken($token)
85
    {
86 10
        if (!in_array($token, $this->tokens)) {
87 10
            $this->tokens[] = $token;
88 10
        }
89
90 10
        return $this;
91
    }
92
}
93