Skip to content

Commit 2a6893c

Browse files
committed
Fix handler
1 parent 6602c65 commit 2a6893c

File tree

4 files changed

+48
-24
lines changed

4 files changed

+48
-24
lines changed

README.md

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,9 @@ The *`getData()`* method is used to return the payload of data that can be used
346346

347347
#### Receiving multiple events in a single webhook
348348

349-
Sometimes the services will send multiple event payloads in a single webhook. In that case, you may return an array of events from the `getEvent` method and Receiver will handle them each individually.
349+
Sometimes the services will send multiple event payloads in a single webhook.
350+
351+
In this scenario you may return an array of mapped events from the `getEvent` method and Receiver will handle them each individually.
350352

351353
For example, if the payload looks like this:
352354
```json
@@ -371,10 +373,14 @@ You may return the events from the `getEvent` method like so:
371373
```php
372374
public function getEvent(): array
373375
{
374-
return [
375-
'channel_occupied',
376-
'member_added',
377-
];
376+
return $events = $this->request
377+
->collect('events')
378+
->mapToGroups(
379+
fn ($item) => [
380+
$item['name'] => $item
381+
]
382+
)
383+
->toArray();
378384
}
379385
```
380386
Receiver will then handle each event individually.

phpunit.xml.dist

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,14 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3-
xsi:noNamespaceSchemaLocation="./vendor/phpunit/phpunit/phpunit.xsd"
4-
bootstrap="vendor/autoload.php"
5-
colors="true"
6-
>
7-
<coverage>
8-
<include>
9-
<directory suffix=".php">src</directory>
10-
</include>
11-
</coverage>
12-
<testsuites>
13-
<testsuite name="default">
14-
<directory suffix="Test.php">tests</directory>
15-
</testsuite>
16-
</testsuites>
17-
</phpunit>
2+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.4/phpunit.xsd" bootstrap="vendor/autoload.php" colors="true">
3+
<coverage/>
4+
<testsuites>
5+
<testsuite name="default">
6+
<directory suffix="Test.php">tests</directory>
7+
</testsuite>
8+
</testsuites>
9+
<source>
10+
<include>
11+
<directory suffix=".php">src</directory>
12+
</include>
13+
</source>
14+
</phpunit>

phpunit.xml.dist.bak

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:noNamespaceSchemaLocation="./vendor/phpunit/phpunit/phpunit.xsd"
4+
bootstrap="vendor/autoload.php"
5+
colors="true"
6+
>
7+
<coverage>
8+
<include>
9+
<directory suffix=".php">src</directory>
10+
</include>
11+
</coverage>
12+
<testsuites>
13+
<testsuite name="default">
14+
<directory suffix="Test.php">tests</directory>
15+
</testsuite>
16+
</testsuites>
17+
</phpunit>

src/Providers/AbstractProvider.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -166,13 +166,17 @@ protected function mapWebhook(Request $request): Webhook
166166
*/
167167
protected function handle(): static
168168
{
169-
$events = Arr::wrap($this->webhook->getEvent());
169+
$events = $this->webhook->getEvent();
170170

171-
foreach($events as $event) {
171+
if(! is_array($events)) {
172+
$events = [$events => $this->webhook->getData()];
173+
}
174+
175+
foreach($events as $event => $data) {
172176
$class = $this->getClass($event);
173177

174178
if (class_exists($class)) {
175-
$class::dispatch($event, $this->webhook->getData());
179+
$class::dispatch($event, $data);
176180

177181
$this->dispatchedEvents[] = $class;
178182
}

0 commit comments

Comments
 (0)