honeybee/honeybee

View on GitHub
src/Model/Task/CreateAggregateRoot/CreateAggregateRootCommandHandler.php

Summary

Maintainability
A
55 mins
Test Coverage
<?php

namespace Honeybee\Model\Task\CreateAggregateRoot;

use Honeybee\Common\Error\RuntimeError;
use Honeybee\Model\Aggregate\AggregateRootInterface;
use Honeybee\Model\Command\AggregateRootCommandHandler;
use Honeybee\Infrastructure\Command\CommandInterface;

class CreateAggregateRootCommandHandler extends AggregateRootCommandHandler
{
    protected function doExecute(CommandInterface $create_command, AggregateRootInterface $aggregate_root)
    {
        if (!$create_command instanceof CreateAggregateRootCommand) {
            throw new RuntimeError(
                sprintf(
                    'The %s only supports events that descend from %s',
                    static::CLASS,
                    CreateAggregateRootCommand::CLASS
                )
            );
        }

        $state_machine = $this->workflow_service->getStateMachine($aggregate_root);

        return $aggregate_root->create($create_command, $state_machine);
    }
}