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
Pull Request — master (#7)
by Choraimy
11:33
created

PushwooshMessage::associate()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

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