Menu


Commands


EventBus


Other Packages

Papertrail

Command Bus Handlers


Command handlers are where you can load in the dependancies you need to perform a change to an aggregate. All handlers will have at least the aggregates repository injected.

Some guidelines

  • Handlers are used to load dependencies for the domain (injecting into aggregates)
  • Handlers are used to turn primitive types into domain types (new SubscriptionId($command->getSubscriptionId()))
  • Handlers can load aggregates from repositories perform an action and then save the aggregate

Example


final class StartCompanyTrailHandler
{
    public function __construct(
        SubscriptionRepository $subscriptionRepository, 
        CardPaymentGateWay $cardPaymentGateWay
    ) {
        $this->subscriptionRepository = $subscriptionRepository;
        $this->cardPaymentGateWay = $cardPaymentGateWay;
    }
    
    public function handle(StartCompanyTrail $command)
    {
        $subscription = Subscription::startTrail(
            new SubscriptionId($command->getSubscriptionId()),
            new CompanyId($command->getCompanyId()),
            $this->cardPaymentGateWay
        );
        
        $this->subscriptionRepository->save($subscription);
    }
}