Webhook::getUsername()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 0
1
<?php
2
declare(strict_types=1);
3
4
namespace DiscordWebhook;
5
6
use DiscordWebhook\Generator\PayloadGenerator;
7
use Doctrine\Common\Collections\ArrayCollection;
8
use GuzzleHttp\Client;
9
use RuntimeException;
10
use SplFileInfo;
11
12
/**
13
 * Class Webhook
14
 *
15
 * @author Scrummer <[email protected]>
16
 * @package DiscordWebhook
17
 */
18
class Webhook
19
{
20
    /**
21
     * @var Client[]|ArrayCollection
22
     */
23
    private $clients;
24
25
    /**
26
     * @var null|string
27
     */
28
    private $username;
29
30
    /**
31
     * @var null|string
32
     */
33
    private $avatar;
34
35
    /**
36
     * @var null|string
37
     */
38
    private $message;
39
40
    /**
41
     * @var null|bool
42
     */
43
    private $isTts;
44
45
    /**
46
     * @var null|SplFileInfo
47
     */
48
    private $file;
49
50
    /**
51
     * @var Embed[]|ArrayCollection
52
     */
53
    private $embeds;
54
55
    /**
56
     * @var PayloadGenerator
57
     */
58
    private $payloadGenerator;
59
60
    /**
61
     * Constructor.
62
     *
63
     * @param array $url
64
     */
65
    public function __construct(array $url)
66
    {
67
        $this->embeds = new ArrayCollection();
68
        $this->clients = new ArrayCollection();
69
        $this->payloadGenerator = new PayloadGenerator();
70
71
        foreach ($url as $webhook) {
72
            $this->clients->add(new Client([
73
                'base_uri' => $webhook
74
            ]));
75
        }
76
    }
77
78
    /**
79
     * Send the Webhook
80
     *
81
     * @return bool
82
     */
83
    public function send(): bool
84
    {
85
        /** @var int[] $responseCodes */
86
        $responseCodes = [];
87
        $payload = $this->payloadGenerator->generate($this);
88
89
        $this->clients->forAll(static function (int $key, Client $client) use ($payload) {
90
            $responseCodes[] = $client->request(
0 ignored issues
show
Comprehensibility Best Practice introduced by
$responseCodes was never initialized. Although not strictly required by PHP, it is generally a good practice to add $responseCodes = array(); before regardless.
Loading history...
91
                'POST',
92
                '',
93
                $payload
94
            )->getStatusCode();
95
        });
96
97
        foreach ($responseCodes as $responseCode) {
98
            if (!in_array($responseCode, [200, 201, 202, 204], true)) {
99
                return false;
100
            }
101
        }
102
103
        return true;
104
    }
105
106
    /**
107
     * @return string|null
108
     */
109
    public function getUsername(): ?string
110
    {
111
        return $this->username;
112
    }
113
114
    /**
115
     * @param string|null $username
116
     *
117
     * @return Webhook
118
     */
119
    public function setUsername(?string $username): Webhook
120
    {
121
        $this->username = $username;
122
123
        return $this;
124
    }
125
126
    /**
127
     * @return string|null
128
     */
129
    public function getAvatar(): ?string
130
    {
131
        return $this->avatar;
132
    }
133
134
    /**
135
     * @param string|null $avatar
136
     *
137
     * @return Webhook
138
     */
139
    public function setAvatar(?string $avatar): Webhook
140
    {
141
        $this->avatar = $avatar;
142
143
        return $this;
144
    }
145
146
    /**
147
     * @return string|null
148
     */
149
    public function getMessage(): ?string
150
    {
151
        return $this->message;
152
    }
153
154
    /**
155
     * @param string|null $message
156
     *
157
     * @return Webhook
158
     */
159
    public function setMessage(?string $message): Webhook
160
    {
161
        $this->message = $message;
162
163
        return $this;
164
    }
165
166
    /**
167
     * @return bool|null
168
     */
169
    public function getIsTts(): ?bool
170
    {
171
        return $this->isTts;
172
    }
173
174
    /**
175
     * @param bool|null $isTts
176
     *
177
     * @return Webhook
178
     */
179
    public function setIsTts(?bool $isTts): Webhook
180
    {
181
        $this->isTts = $isTts;
182
183
        return $this;
184
    }
185
186
    /**
187
     * @return SplFileInfo|null
188
     */
189
    public function getFile(): ?SplFileInfo
190
    {
191
        return $this->file;
192
    }
193
194
    /**
195
     * @param SplFileInfo|null $file
196
     *
197
     * @return Webhook
198
     */
199
    public function setFile(?SplFileInfo $file): Webhook
200
    {
201
        $this->file = $file;
202
203
        return $this;
204
    }
205
206
    /**
207
     * Add an embed to the message.
208
     *
209
     * @param Embed $embed
210
     *
211
     * @return Webhook
212
     */
213
    public function addEmbed(Embed $embed): Webhook
214
    {
215
        if ($this->embeds->count() >= Embed::CONFIG_MAX_COUNT) {
216
            throw new RuntimeException(sprintf('Maximum amount of embeds reached for this message. Discord allows only %d embeds.', Embed::CONFIG_MAX_COUNT));
217
        }
218
219
        $this->embeds->add($embed);
220
221
        return $this;
222
    }
223
224
    /**
225
     * @return array|null
226
     */
227
    public function getEmbeds(): ?array
228
    {
229
        return $this->embeds->isEmpty() ? null : $this->embeds->toArray();
230
    }
231
}
232