Completed
Push — master ( 504a7e...883fbf )
by Aly
05:16
created

ExpoMessage   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 186
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 94.87%

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 1
dl 0
loc 186
c 0
b 0
f 0
ccs 37
cts 39
cp 0.9487
rs 10

10 Methods

Rating   Name   Duplication   Size   Complexity  
A create() 0 4 1
A __construct() 0 4 1
A body() 0 6 1
A enableSound() 0 6 1
A disableSound() 0 6 1
A badge() 0 6 1
A setTtl() 0 6 1
A setChannelId() 0 4 1
A setJsonData() 0 16 4
A toArray() 0 11 1
1
<?php
2
3
namespace NotificationChannels\ExpoPushNotifications;
4
5
use NotificationChannels\ExpoPushNotifications\Exceptions\CouldNotCreateMessage;
6
7
class ExpoMessage
8
{
9
    /**
10
     * The message body.
11
     *
12
     * @var string
13
     */
14
    protected $body;
15
16
    /**
17
     * The sound to play when the recipient receives this notification.
18
     *
19
     * @var string|null
20
     */
21
    protected $sound = 'default';
22
23
    /**
24
     * The number to display next to the push notification (iOS).
25
     * Specify zero to clear the badge.
26
     *
27
     * @var int
28
     */
29
    protected $badge = 0;
30
31
    /**
32
     * The number of seconds for which the message may be kept around for redelivery if it has not been delivered yet.
33
     *
34
     * @var int
35
     */
36
    protected $ttl = 0;
37
38
    /**
39
     * ID of the Notification Channel through which to display this notification on Android devices.
40
     *
41
     * @var string
42
     */
43
    protected $channelId = "Default";
44
45
    /**
46
     * The json data attached to the message.
47
     *
48
     * @var string
49
     */
50
    protected $jsonData = '';
51
52
    /**
53
     * Create a message with given body.
54
     *
55
     * @param string $body
56
     *
57
     * @return static
58
     */
59 1
    public static function create($body = '')
60
    {
61 1
        return new static($body);
62
    }
63
64
    /**
65
     * ExpoMessage constructor.
66
     *
67
     * @param string $body
68
     */
69 11
    public function __construct(string $body = '')
70
    {
71 11
        $this->body = $body;
72 11
    }
73
74
    /**
75
     * Set the message body.
76
     *
77
     * @param string $value
78
     *
79
     * @return $this
80
     */
81 1
    public function body(string $value)
82
    {
83 1
        $this->body = $value;
84
85 1
        return $this;
86
    }
87
88
    /**
89
     * Enable the message sound.
90
     *
91
     * @return $this
92
     */
93 1
    public function enableSound()
94
    {
95 1
        $this->sound = 'default';
96
97 1
        return $this;
98
    }
99
100
    /**
101
     * Disable the message sound.
102
     *
103
     * @return $this
104
     */
105 2
    public function disableSound()
106
    {
107 2
        $this->sound = null;
108
109 2
        return $this;
110
    }
111
112
    /**
113
     * Set the message badge (iOS).
114
     *
115
     * @param int $value
116
     *
117
     * @return $this
118
     */
119 1
    public function badge(int $value)
120
    {
121 1
        $this->badge = $value;
122
123 1
        return $this;
124
    }
125
126
    /**
127
     * Set the time to live of the notification.
128
     *
129
     * @param int $ttl
130
     *
131
     * @return $this
132
     */
133 1
    public function setTtl(int $ttl)
134
    {
135 1
        $this->ttl = $ttl;
136
137 1
        return $this;
138
    }
139
140
    /**
141
     * Set the channelId of the notification for Android devices.
142
     *
143
     * @param string $channelId
144
     */
145 1
    public function setChannelId(string $channelId)
146
    {
147 1
        $this->channelId = $channelId;
148 1
    }
149
150
    /**
151
     * Set the json Data attached to the message.
152
     *
153
     * @param array|string $data
154
     *
155
     * @return $this
156
     *
157
     * @throws CouldNotCreateMessage
158
     */
159 1
    public function setJsonData($data)
160
    {
161 1
        if (is_array($data)) {
162
            $data = json_encode($data);
163 1
        } elseif (is_string($data)) {
164 1
            @json_decode($data);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
165
166 1
            if (json_last_error() !== JSON_ERROR_NONE) {
167
                throw new CouldNotCreateMessage('Invalid json format passed to the setJsonData().');
168
            }
169
        }
170
171 1
        $this->jsonData = $data;
172
173 1
        return $this;
174
    }
175
176
    /**
177
     * Get an array representation of the message.
178
     *
179
     * @return array
180
     */
181 11
    public function toArray()
182
    {
183
        return [
184 11
            'body'      =>  $this->body,
185 11
            'sound'     =>  $this->sound,
186 11
            'badge'     =>  $this->badge,
187 11
            'ttl'       =>  $this->ttl,
188 11
            'channelId' =>  $this->channelId,
189 11
            'data'      => $this->jsonData,
190
        ];
191
    }
192
}
193