Skip to content

Commit

Permalink
Event dispatching simplified (thanks @stevebauman)
Browse files Browse the repository at this point in the history
  • Loading branch information
Webklex committed Jan 19, 2025
1 parent ec2dcb8 commit d21e00c
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 20 deletions.
6 changes: 2 additions & 4 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -723,8 +723,7 @@ public function createFolder(string $folder_path, bool $expunge = true, bool $ut

$folder = $this->getFolderByPath($folder_path, true);
if($status && $folder) {
$event = $this->getEvent("folder", "new");
$event::dispatch($folder);
$this->dispatch("folder", "new", $folder);
}

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

$event = $this->getEvent("folder", "deleted");
$event::dispatch($folder);
$this->dispatch("folder", "deleted", $folder);

return $status;
}
Expand Down
9 changes: 3 additions & 6 deletions src/Folder.php
Original file line number Diff line number Diff line change
Expand Up @@ -287,8 +287,7 @@ public function move(string $new_name, bool $expunge = true): array {
if ($expunge) $this->client->expunge();

$folder = $this->client->getFolder($new_name);
$event = $this->getEvent("folder", "moved");
$event::dispatch($this, $folder);
$this->dispatch("folder", "moved", $this, $folder);

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

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

$event = $this->getEvent("folder", "deleted");
$event::dispatch($this);
$this->dispatch("folder", "deleted", $this);

return $status;
}
Expand Down Expand Up @@ -494,8 +492,7 @@ public function idle(callable $callback, int $timeout = 300): void {
$message->setSequence($sequence);
$callback($message);

$event = $this->getEvent("message", "new");
$event::dispatch($message);
$this->dispatch("message", "new", $message);
}
}
}
Expand Down
15 changes: 5 additions & 10 deletions src/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -1096,8 +1096,7 @@ protected function fetchNewMail(Folder $folder, int $next_uid, string $event, bo
}

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

return $message;
}
Expand Down Expand Up @@ -1131,8 +1130,7 @@ public function delete(bool $expunge = true, ?string $trash_path = null, bool $f
}
if ($expunge) $this->client->expunge();

$event = $this->getEvent("message", "deleted");
$event::dispatch($this);
$this->dispatch("message", "deleted", $this);

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

$event = $this->getEvent("message", "restored");
$event::dispatch($this);
$this->dispatch("message", "restored", $this);

return $status;
}
Expand Down Expand Up @@ -1186,8 +1183,7 @@ public function setFlag(array|string $flag): bool {
}
$this->parseFlags();

$event = $this->getEvent("flag", "new");
$event::dispatch($this, $flag);
$this->dispatch("flag", "new", $this, $flag);

return (bool)$status;
}
Expand Down Expand Up @@ -1218,8 +1214,7 @@ public function unsetFlag(array|string $flag): bool {
}
$this->parseFlags();

$event = $this->getEvent("flag", "deleted");
$event::dispatch($this, $flag);
$this->dispatch("flag", "deleted", $this, $flag);

return (bool)$status;
}
Expand Down
9 changes: 9 additions & 0 deletions src/Traits/HasEvents.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,13 @@ public function getEvents(): array {
return $this->events;
}

/**
* Dispatch a specific event.
* @throws EventNotFoundException
*/
public function dispatch(string $section, string $event, mixed ...$args): void {
$event = $this->getEvent($section, $event);
$event::dispatch(...$args);
}

}

0 comments on commit d21e00c

Please sign in to comment.