GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( 2b3cf4...ebae93 )
by Choraimy
02:34
created

PushwooshMessage::identifier()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 5
ccs 3
cts 3
cp 1
crap 1
rs 10
1
<?php
2
3
namespace NotificationChannels\Pushwoosh;
4
5
use DateTimeInterface;
6
use DateTimeZone;
7
use Illuminate\Notifications\Notification;
8
use InvalidArgumentException;
9
use JsonSerializable;
10
11
class PushwooshMessage implements JsonSerializable
12
{
13
    protected $androidRootParameters;
14
    protected $campaign;
15
    protected $content;
16
    protected $identifier;
17
    protected $iosRootParameters;
18
    protected $preset;
19
    protected $recipientTimezone;
20
    protected $shortenUrl;
21
    protected $timezone;
22
    protected $throughput;
23
    protected $url;
24
    protected $when;
25
26
    /**
27
     * Create a new push message.
28
     *
29
     * @param string $content
30
     * @return void
31
     */
32 75
    public function __construct(string $content = '')
33
    {
34 75
        $this->content = $content;
35 75
        $this->recipientTimezone = false;
36 75
        $this->when = 'now';
37 75
    }
38
39
    /**
40
     * Associate the message to the given notification.
41
     *
42
     * @param \Illuminate\Notifications\Notification $notification
43
     * @return $this
44
     */
45 10
    public function associate(Notification $notification)
46
    {
47 10
        if (!$this->identifier) {
48 10
            $this->identifier = $notification->id;
49
        }
50
51 10
        return $this;
52
    }
53
54
    /**
55
     * Set the Pushwoosh campaign code.
56
     *
57
     * @param string $campaign
58
     * @return $this
59
     */
60 5
    public function campaign(string $campaign)
61
    {
62 5
        $this->campaign = $campaign;
63
64 5
        return $this;
65
    }
66
67
    /**
68
     * Set the message content.
69
     *
70
     * @param string $content
71
     * @param string|null $language
72
     * @return $this
73
     */
74 5
    public function content(string $content, string $language = null)
75
    {
76 5
        if ($language) {
77 5
            if (!is_array($this->content)) {
0 ignored issues
show
introduced by
The condition is_array($this->content) is always false.
Loading history...
78 5
                $this->content = [];
79
            }
80
81 5
            $this->content[$language] = $content;
82
        } else {
83 5
            $this->content = $content;
84
        }
85
86 5
        return $this;
87
    }
88
89
    /**
90
     * Set the delivery moment.
91
     *
92
     * @param \DateTimeInterface|string $when
93
     * @param \DateTimeZone|string|null $timezone
94
     * @return $this
95
     */
96 5
    public function deliverAt($when, $timezone = null)
97
    {
98 5
        if ($when instanceof DateTimeInterface) {
99 5
            $timezone = $when->getTimezone();
100 5
            $when = $when->format('Y-m-d H:i');
101
        }
102
103 5
        if ($timezone instanceof DateTimeZone) {
104 5
            $timezone = $timezone->getName();
105
        }
106
107 5
        $this->timezone = $timezone;
108 5
        $this->when = $when;
109
110 5
        return $this;
111
    }
112
113
    /**
114
     * Set the message identifier.
115
     *
116
     * @param string $identifier
117
     * @return $this
118
     */
119 10
    public function identifier(string $identifier)
120
    {
121 10
        $this->identifier = $identifier;
122
123 10
        return $this;
124
    }
125
126
    /**
127
     * Convert the message into something JSON serializable.
128
     *
129
     * @return array
130
     */
131 65
    public function jsonSerialize()
132
    {
133
        $payload = [
134 65
            'android_root_params' => $this->androidRootParameters,
135 65
            'campaign' => $this->campaign,
136 65
            'content' => $this->content,
137 65
            'ignore_user_timezone' => !$this->recipientTimezone,
138 65
            'ios_root_params' => $this->iosRootParameters,
139 65
            'link' => $this->url,
140 65
            'minimize_link' => $this->url ? $this->shortenUrl : null,
141 65
            'preset' => $this->preset,
142 65
            'send_date' => $this->when,
143 65
            'send_rate' => $this->throughput,
144 65
            'transactionId' => $this->identifier,
145 65
            'timezone' => $this->timezone,
146
        ];
147
148
        return array_filter($payload, function ($value) {
149 65
            return $value !== null;
150 65
        });
151
    }
152
153
    /**
154
     * Set the Pushwoosh preset code.
155
     *
156
     * @param string $preset
157
     * @return $this
158
     */
159 5
    public function preset(string $preset)
160
    {
161 5
        $this->preset = $preset;
162
163 5
        return $this;
164
    }
165
166
    /**
167
     * Throttle the message rollout.
168
     *
169
     * @param int $limit
170
     * @return $this
171
     */
172 5
    public function throttle(int $limit)
173
    {
174 5
        $this->throughput = max(100, min($limit, 1000));
175
176 5
        return $this;
177
    }
178
179
    /**
180
     * Set the URL the message should link to.
181
     *
182
     * @param string $url
183
     * @param bool $shorten
184
     * @return $this
185
     */
186 5
    public function url(string $url, bool $shorten = true)
187
    {
188 5
        $this->shortenUrl = $shorten;
189 5
        $this->url = $url;
190
191 5
        return $this;
192
    }
193
194
    /**
195
     * Add a root level parameter.
196
     *
197
     * @param string $key
198
     * @param mixed $value
199
     * @param string|null $platform
200
     * @return $this
201
     */
202 15
    public function with(string $key, $value, string $platform = null)
203
    {
204 15
        if (!in_array($platform, [null, 'ios', 'android'])) {
205
            throw new InvalidArgumentException("Invalid platform {$platform}");
206
        }
207
208 15
        if (($platform ?: 'android') === 'android') {
209 10
            $this->androidRootParameters[$key] = $value;
210
        }
211
212 15
        if (($platform ?: 'ios') === 'ios') {
213 10
            $this->iosRootParameters[$key] = $value;
214
        }
215
216 15
        return $this;
217
    }
218
219
    /**
220
     * Respect the recipients' timezone when delivering.
221
     *
222
     * @return $this
223
     */
224 5
    public function useRecipientTimezone()
225
    {
226 5
        $this->recipientTimezone = true;
227
228 5
        return $this;
229
    }
230
}
231