Debian Codegraph @

lib/3rdParty/composer/webklex/php-imap/src/Traits/HasEvents.php

raw symbols SCIP
1 <?php
2 /*
3 * File: HasEvents.php
4 * Category: -
5 * Author: M.Goldenbaum
6 * Created: 21.09.20 22:46
7 * Updated: -
8 *
9 * Description:
10 * -
11 */
12
13 namespace Webklex\PHPIMAP\Traits;
14
15
16 use Webklex\PHPIMAP\Events\Event;
17 use Webklex\PHPIMAP\Exceptions\EventNotFoundException;
18
19 /**
20 * Trait HasEvents
21 *
22 * @package Webklex\PHPIMAP\Traits
23 */
24 trait HasEvents {
25
26 /**
27 * Event holder
28 *
29 * @var array $events
30 */
31 protected array $events = [];
32
33 /**
34 * Set a specific event
35 * @param string $section
36 * @param string $event
37 * @param mixed $class
38 */
39 public function setEvent(string $section, string $event, mixed $class): void {
40 if (isset($this->events[$section])) {
41 $this->events[$section][$event] = $class;
42 }
43 }
44
45 /**
46 * Set all events
47 * @param array $events
48 */
49 public function setEvents(array $events): void {
50 $this->events = $events;
51 }
52
53 /**
54 * Get a specific event callback
55 * @param string $section
56 * @param string $event
57 *
58 * @return Event|string
59 * @throws EventNotFoundException
60 */
61 public function getEvent(string $section, string $event): Event|string {
62 if (isset($this->events[$section])) {
63 return $this->events[$section][$event];
64 }
65 throw new EventNotFoundException();
66 }
67
68 /**
69 * Get all events
70 *
71 * @return array
72 */
73 public function getEvents(): array {
74 return $this->events;
75 }
76
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
86 }