Skip to content

Improvement handleCommand #18

@jaapio

Description

@jaapio

At the moment all resources must extend AbstractResource and should use the contructor from that class. I think we can improve that.

In the hydrator we know what type of Resource should be create Async or sync. So we can help the user to call the wait method when in sync mode. I would suggest that we inject a commandHandler object in to the class on a property that's annotated with the @handler annotation. We will have 2 types of handlers, sync and async. In pseudo code they look like this:

final class AsyncHandler implements HandlerInterface
{
    /**
     * @var LoopInterface
     */
    private $loop;
    /**
     * @var CommandBusInterface
     */
    private $commandBus;
    
   public function __construct(CommandBusInterface $commandBus)
    {
        $this->commandBus = $commandBus;
    }
    /**
     * @param $command
     * @return CancellablePromiseInterface
     */
    protected function handleCommand($command): CancellablePromiseInterface
    {
        return $this->commandBus->handle($command);
    }
}
final class SyncHandler implements HandlerInterface
{
    /**
     * @var LoopInterface
     */
    private $loop;
    /**
     * @var CommandBusInterface
     */
    private $commandBus;
    
   public function __construct(AsyncHandler $handler)
    {

    }
    /**
     * @param $command
     * @return mixed
     */
    protected function handleCommand($command)
    {
        return wait($this->handler->handleCommand($command))
    }
}

Resource example

Class MyResource implements ResourceInterface
{
    /** @handler */
    private $handler;

   public function respository() {
        return $handler->handlerCommand(new Repository());
   }
}

I think this might even remove the need of having 2 different classes for async and sync resources. However when you want to define the return types of the methods like Repository you still need those.

Another benifit is that you are giving the control of the constructor back to the user. So he could use it in instanciate new objects that can be persisted for example.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions