41 lines
		
	
	
		
			774 B
		
	
	
	
		
			PHP
		
	
	
	
	
	
		
		
			
		
	
	
			41 lines
		
	
	
		
			774 B
		
	
	
	
		
			PHP
		
	
	
	
	
	
| 
								 | 
							
								<?php
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								namespace NoccyLabs\React\CommandBus;
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								use React\Promise\Deferred;
							 | 
						||
| 
								 | 
							
								use React\Promise\Promise;
							 | 
						||
| 
								 | 
							
								use React\Promise\PromiseInterface;
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								/**
							 | 
						||
| 
								 | 
							
								 *
							 | 
						||
| 
								 | 
							
								 */
							 | 
						||
| 
								 | 
							
								class Command
							 | 
						||
| 
								 | 
							
								{
							 | 
						||
| 
								 | 
							
								    /** @var string $name The command name */
							 | 
						||
| 
								 | 
							
								    private string $name;
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    /** @var callable $handler The handler */
							 | 
						||
| 
								 | 
							
								    private $handler;
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    public function __construct(string $name, callable $handler)
							 | 
						||
| 
								 | 
							
								    {
							 | 
						||
| 
								 | 
							
								        $this->name = $name;
							 | 
						||
| 
								 | 
							
								        $this->handler = $handler;
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    public function getName(): string
							 | 
						||
| 
								 | 
							
								    {
							 | 
						||
| 
								 | 
							
								        return $this->name;
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    public function call(Context $context): PromiseInterface
							 | 
						||
| 
								 | 
							
								    {
							 | 
						||
| 
								 | 
							
								        return new Promise(function (callable $resolve) use ($context) {
							 | 
						||
| 
								 | 
							
								            $resolve(call_user_func($this->handler, $context));
							 | 
						||
| 
								 | 
							
								            return;
							 | 
						||
| 
								 | 
							
								        });
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								}
							 | 
						||
| 
								 | 
							
								
							 |