Skip to content

Commit d21e00c

Browse files
committed
Event dispatching simplified (thanks @stevebauman)
1 parent ec2dcb8 commit d21e00c

File tree

4 files changed

+19
-20
lines changed

4 files changed

+19
-20
lines changed

src/Client.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -723,8 +723,7 @@ public function createFolder(string $folder_path, bool $expunge = true, bool $ut
723723

724724
$folder = $this->getFolderByPath($folder_path, true);
725725
if($status && $folder) {
726-
$event = $this->getEvent("folder", "new");
727-
$event::dispatch($folder);
726+
$this->dispatch("folder", "new", $folder);
728727
}
729728

730729
return $folder;
@@ -755,8 +754,7 @@ public function deleteFolder(string $folder_path, bool $expunge = true): array {
755754
$status = $this->getConnection()->deleteFolder($folder->path)->validatedData();
756755
if ($expunge) $this->expunge();
757756

758-
$event = $this->getEvent("folder", "deleted");
759-
$event::dispatch($folder);
757+
$this->dispatch("folder", "deleted", $folder);
760758

761759
return $status;
762760
}

src/Folder.php

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -287,8 +287,7 @@ public function move(string $new_name, bool $expunge = true): array {
287287
if ($expunge) $this->client->expunge();
288288

289289
$folder = $this->client->getFolder($new_name);
290-
$event = $this->getEvent("folder", "moved");
291-
$event::dispatch($this, $folder);
290+
$this->dispatch("folder", "moved", $this, $folder);
292291

293292
return $status;
294293
}
@@ -383,8 +382,7 @@ public function delete(bool $expunge = true): array {
383382

384383
if ($expunge) $this->client->expunge();
385384

386-
$event = $this->getEvent("folder", "deleted");
387-
$event::dispatch($this);
385+
$this->dispatch("folder", "deleted", $this);
388386

389387
return $status;
390388
}
@@ -494,8 +492,7 @@ public function idle(callable $callback, int $timeout = 300): void {
494492
$message->setSequence($sequence);
495493
$callback($message);
496494

497-
$event = $this->getEvent("message", "new");
498-
$event::dispatch($message);
495+
$this->dispatch("message", "new", $message);
499496
}
500497
}
501498
}

src/Message.php

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1096,8 +1096,7 @@ protected function fetchNewMail(Folder $folder, int $next_uid, string $event, bo
10961096
}
10971097

10981098
$message = $folder->query()->getMessage($sequence_id, null, $this->sequence);
1099-
$event = $this->getEvent("message", $event);
1100-
$event::dispatch($this, $message);
1099+
$this->dispatch("message", $event, $this, $message);
11011100

11021101
return $message;
11031102
}
@@ -1131,8 +1130,7 @@ public function delete(bool $expunge = true, ?string $trash_path = null, bool $f
11311130
}
11321131
if ($expunge) $this->client->expunge();
11331132

1134-
$event = $this->getEvent("message", "deleted");
1135-
$event::dispatch($this);
1133+
$this->dispatch("message", "deleted", $this);
11361134

11371135
return $status;
11381136
}
@@ -1155,8 +1153,7 @@ public function restore(bool $expunge = true): bool {
11551153
$status = $this->unsetFlag("Deleted");
11561154
if ($expunge) $this->client->expunge();
11571155

1158-
$event = $this->getEvent("message", "restored");
1159-
$event::dispatch($this);
1156+
$this->dispatch("message", "restored", $this);
11601157

11611158
return $status;
11621159
}
@@ -1186,8 +1183,7 @@ public function setFlag(array|string $flag): bool {
11861183
}
11871184
$this->parseFlags();
11881185

1189-
$event = $this->getEvent("flag", "new");
1190-
$event::dispatch($this, $flag);
1186+
$this->dispatch("flag", "new", $this, $flag);
11911187

11921188
return (bool)$status;
11931189
}
@@ -1218,8 +1214,7 @@ public function unsetFlag(array|string $flag): bool {
12181214
}
12191215
$this->parseFlags();
12201216

1221-
$event = $this->getEvent("flag", "deleted");
1222-
$event::dispatch($this, $flag);
1217+
$this->dispatch("flag", "deleted", $this, $flag);
12231218

12241219
return (bool)$status;
12251220
}

src/Traits/HasEvents.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,4 +74,13 @@ public function getEvents(): array {
7474
return $this->events;
7575
}
7676

77+
/**
78+
* Dispatch a specific event.
79+
* @throws EventNotFoundException
80+
*/
81+
public function dispatch(string $section, string $event, mixed ...$args): void {
82+
$event = $this->getEvent($section, $event);
83+
$event::dispatch(...$args);
84+
}
85+
7786
}

0 commit comments

Comments
 (0)