Completed
Push — master ( f96e3c...dc495a )
by Casper
09:34 queued 01:13
created

PushoverMessage::toArray()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 14
ccs 4
cts 4
cp 1
rs 9.4285
cc 1
eloc 11
nc 1
nop 0
crap 1
1
<?php
2
3
namespace NotificationChannels\Pushover;
4
5
use Carbon\Carbon;
6
use NotificationChannels\Pushover\Exceptions\EmergencyNotificationRequiresRetryAndExpire;
7
8
class PushoverMessage
9
{
10
    /**
11
     * The text content of the message.
12
     *
13
     * @var string
14
     */
15
    public $content;
16
17
    /**
18
     * The (optional) title of the message.
19
     *
20
     * @var string
21
     */
22
    public $title;
23
24
    /**
25
     * The (optional) timestamp of the message.
26
     *
27
     * @var int
28
     */
29
    public $timestamp;
30
31
    /**
32
     * The (optional) priority of the message.
33
     *
34
     * @var int
35
     */
36
    public $priority;
37
38
    /**
39
     * The (optional) timeout between retries when sending a message
40
     * with an emergency priority. The timeout is in seconds.
41
     *
42
     * @var int
43
     */
44
    public $retry;
45
46
    /**
47
     * The (optional) expire time of a message with an emergency priority.
48
     * The expire time is in seconds.
49
     *
50
     * @var int
51
     */
52
    public $expire;
53
54
    /**
55
     * The (optional) supplementary url of the message.
56
     *
57
     * @var string
58
     */
59
    public $url;
60
61
    /**
62
     * The (optional) supplementary url title of the message.
63
     *
64
     * @var string
65
     */
66
    public $urlTitle;
67
68
    /**
69
     * The (optional) sound of the message.
70
     *
71
     * @var string
72
     */
73
    public $sound;
74
75
    /**
76
     * Message priorities.
77
     */
78
    const LOWEST_PRIORITY = -2;
79
    const LOW_PRIORITY = -1;
80
    const NORMAL_PRIORITY = 0;
81
    const HIGH_PRIORITY = 1;
82
    const EMERGENCY_PRIORITY = 2;
83
84
    /**
85
     * @param  string $content
86
     *
87
     * @return static
88
     */
89 2
    public static function create($content = '')
90
    {
91 2
        return new static($content);
92
    }
93
94
    /**
95
     * @param  string  $content
96
     */
97 19
    public function __construct($content = '')
98
    {
99 19
        $this->content = $content;
100 19
    }
101
102
    /**
103
     * Set the content of the Pushover message.
104
     *
105
     * @param  string  $content
106
     * @return $this
107
     */
108 1
    public function content($content)
109
    {
110 1
        $this->content = $content;
111
112 1
        return $this;
113
    }
114
115
    /**
116
     * Set the title of the Pushover message.
117
     *
118
     * @param  string  $title
119
     * @return $this
120
     */
121 2
    public function title($title)
122
    {
123 2
        $this->title = $title;
124
125 2
        return $this;
126
    }
127
128
    /**
129
     * Set the time of the Pushover message.
130
     *
131
     * @param  Carbon|int  $time
132
     * @return $this
133
     */
134 3
    public function time($time)
135
    {
136 3
        if ($time instanceof Carbon) {
137 1
            $time = $time->timestamp;
138 1
        }
139
140 3
        $this->timestamp = $time;
141
142 3
        return $this;
143
    }
144
145
    /**
146
     * Set a supplementary url for the Pushover message.
147
     *
148
     * @param  string $url
149
     * @param  string $title
150
     * @return $this
151
     */
152 3
    public function url($url, $title = null)
153
    {
154 3
        $this->url = $url;
155 3
        $this->urlTitle = $title;
156
157 3
        return $this;
158
    }
159
160
    /**
161
     * Set the sound of the Pushover message.
162
     *
163
     * @param  string  $sound
164
     * @return $this
165
     */
166 2
    public function sound($sound)
167
    {
168 2
        $this->sound = $sound;
169
170 2
        return $this;
171
    }
172
173
    /**
174
     * Set the priority of the Pushover message.
175
     * Retry and expire are mandatory when setting the priority to emergency.
176
     *
177
     * @param  int  $priority
178
     * @param  int  $retryTimeout
179
     * @param  int  $expireAfter
180
     * @return $this
181
     */
182 9
    public function priority($priority, $retryTimeout = null, $expireAfter = null)
183
    {
184 9
        $this->noEmergencyWithoutRetryOrExpire($priority, $retryTimeout, $expireAfter);
185
186 8
        $this->priority = $priority;
187 8
        $this->retry = $retryTimeout;
188 8
        $this->expire = $expireAfter;
189
190 8
        return $this;
191
    }
192
193
    /**
194
     * Set the priority of the Pushover message to the lowest priority.
195
     *
196
     * @return $this
197
     */
198 1
    public function lowestPriority()
199
    {
200 1
        return $this->priority(self::LOWEST_PRIORITY);
201
    }
202
203
    /**
204
     * Set the priority of the Pushover message to low.
205
     *
206
     * @return $this
207
     */
208 1
    public function lowPriority()
209
    {
210 1
        return $this->priority(self::LOW_PRIORITY);
211
    }
212
213
    /**
214
     * Set the priority of the Pushover message to normal.
215
     *
216
     * @return $this
217
     */
218 1
    public function normalPriority()
219
    {
220 1
        return $this->priority(self::NORMAL_PRIORITY);
221
    }
222
223
    /**
224
     * Set the priority of the Pushover message to high.
225
     *
226
     * @return $this
227
     */
228 1
    public function highPriority()
229
    {
230 1
        return $this->priority(self::HIGH_PRIORITY);
231
    }
232
233
    /**
234
     * Set the priority of the Pushover message to emergency.
235
     * Retry and expire are mandatory when setting the priority to emergency.
236
     *
237
     * @param  int  $retryTimeout
238
     * @param  int  $expireAfter
239
     * @return $this
240
     */
241 2
    public function emergencyPriority($retryTimeout, $expireAfter)
242
    {
243 2
        return $this->priority(self::EMERGENCY_PRIORITY, $retryTimeout, $expireAfter);
244
    }
245
246
    /**
247
     * Array representation of Pushover Message.
248
     *
249
     * @return array
250
     */
251
    public function toArray()
252
    {
253
        return [
254 9
            'message' => $this->content,
255
            'title' => $this->title,
256 9
            'timestamp' => $this->timestamp,
257 1
            'priority' => $this->priority,
258
            'url' => $this->url,
259 8
            'url_title' => $this->urlTitle,
260
            'sound' => $this->sound,
261
            'retry' => $this->retry,
262
            'expire' => $this->expire,
263
        ];
264
    }
265
266
    /**
267
     * Ensure an emergency message has an retry and expiry time.
268
     *
269
     * @param  int  $priority
270
     * @param  int  $retry
271
     * @param  int  $expire
272
     * @throws EmergencyNotificationRequiresRetryAndExpire
273
     */
274
    protected function noEmergencyWithoutRetryOrExpire($priority, $retry, $expire)
275
    {
276
        if ($priority == self::EMERGENCY_PRIORITY && (! isset($retry) || ! isset($expire))) {
277
            throw new EmergencyNotificationRequiresRetryAndExpire();
278
        }
279
    }
280
}
281