Completed
Pull Request — develop (#7)
by Romain
01:41
created

DataBuilder::getData()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 1
1
<?php
2
namespace ker0x\Push\Adapter\Fcm\Message;
3
4
use ker0x\Push\Adapter\Fcm\Message\Exception\InvalidDataException;
5
6
class DataBuilder
7
{
8
9
    /**
10
     * An array of data.
11
     *
12
     * @var array
13
     */
14
    protected $data = [];
15
16
    /**
17
     * Setter for data.
18
     *
19
     * @param string $key The data key.
20
     * @param string|bool|int $value The data value.
21
     * @return \ker0x\Push\Adapter\Fcm\Message\DataBuilder
22
     */
23
    public function addData($key, $value)
24
    {
25
        if (is_bool($value)) {
26
            $value = ($value) ? 'true' : 'false';
27
        }
28
        $this->data[$key] = (string)$value;
29
30
        return $this;
31
    }
32
33
    /**
34
     * Getter for data.
35
     *
36
     * @param string $key The key we want to get.
37
     * @return mixed
38
     * @throws \ker0x\Push\Adapter\Fcm\Message\Exception\InvalidDataException
39
     */
40
    public function getData($key)
41
    {
42
        if (!array_key_exists($key, $this->data)) {
43
            throw InvalidDataException::invalidKey($key);
44
        }
45
46
        return $this->data[$key];
47
    }
48
49
    /**
50
     * Return all data.
51
     *
52
     * @return array|null
53
     */
54
    public function getAllData()
55
    {
56
        return $this->data;
57
    }
58
59
    /**
60
     * Remove the data with the key $key.
61
     *
62
     * @param string $key The key we want to remove.
63
     * @return \ker0x\Push\Adapter\Fcm\Message\DataBuilder
64
     */
65
    public function removeData($key)
66
    {
67
        unset($this->data[$key]);
68
69
        return $this;
70
    }
71
72
    /**
73
     * Remove all data
74
     *
75
     * @return void
76
     */
77
    public function removeAllData()
78
    {
79
        $this->data = [];
80
    }
81
}
82