| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | declare(strict_types=1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | namespace Yiisoft\Test\Support\EventDispatcher; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Closure; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Psr\EventDispatcher\EventDispatcherInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use Psr\EventDispatcher\StoppableEventInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use function get_class; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | final class SimpleEventDispatcher implements EventDispatcherInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |     /** @var array<int, Closure>  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |     private array $listeners; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |     /** @var object[] */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |     private array $events = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |      * @param Closure ...$listeners Functions that will handle each event. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 | 16 |  |     public function __construct(Closure ...$listeners) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 | 16 |  |         $this->listeners = $listeners; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 | 16 |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 26 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 27 | 14 |  |     public function dispatch(object $event): object | 
            
                                                                        
                            
            
                                    
            
            
                | 28 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 29 | 14 |  |         $this->events[] = $event; | 
            
                                                                        
                            
            
                                    
            
            
                | 30 | 14 |  |         foreach ($this->listeners as $listener) { | 
            
                                                                        
                            
            
                                    
            
            
                | 31 | 4 |  |             if ($event instanceof StoppableEventInterface && $event->isPropagationStopped()) { | 
            
                                                                        
                            
            
                                    
            
            
                | 32 | 2 |  |                 return $event; | 
            
                                                                        
                            
            
                                    
            
            
                | 33 |  |  |             } | 
            
                                                                        
                            
            
                                    
            
            
                | 34 | 3 |  |             $listener($event); | 
            
                                                                        
                            
            
                                    
            
            
                | 35 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 36 | 11 |  |         return $event; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 | 2 |  |     public function getEvents(): array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 | 2 |  |         return $this->events; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 | 3 |  |     public function isObjectTriggered(object $event, int $times = null): bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 | 3 |  |         return $this->processBoolResult(static fn (object $e): bool => $e === $event, $times); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 | 4 |  |     public function isClassTriggered(string $class, int $times = null): bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 | 4 |  |         return $this->processBoolResult(static fn (object $event): bool => get_class($event) === $class, $times); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 | 3 |  |     public function isInstanceOfTriggered(string $class, int $times = null): bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 | 3 |  |         return $this->processBoolResult(static fn (object $event): bool => $event instanceof $class, $times); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 | 10 |  |     private function processBoolResult(Closure $closure, ?int $times): bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 | 10 |  |         if ($times < 0) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 | 1 |  |             throw new \InvalidArgumentException('The $times argument cannot be less than zero.'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 | 9 |  |         if ($times === null) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 | 3 |  |             return $this->hasEvent($closure); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 | 6 |  |         return $times === $this->calcEvent($closure); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 | 3 |  |     private function hasEvent(Closure $closure): bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 | 3 |  |         foreach ($this->events as $event) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 | 3 |  |             if ($closure($event)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 | 3 |  |                 return true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 | 3 |  |         return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 | 6 |  |     private function calcEvent(Closure $closure): int | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 | 6 |  |         $count = 0; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 | 6 |  |         foreach ($this->events as $event) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 | 6 |  |             if ($closure($event)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 | 6 |  |                 ++$count; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 | 6 |  |         return $count; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 90 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 91 |  |  |  |