xprt64/cqrs-es

View on GitHub
src/Dudulina/Event/EventsApplier/EventsApplierOnAggregate.php

Summary

Maintainability
A
0 mins
Test Coverage
<?php
/******************************************************************************
 * Copyright (c) 2016 Constantin Galbenu <gica.galbenu@gmail.com>             *
 ******************************************************************************/

namespace Dudulina\Event\EventsApplier;


use Dudulina\Event\EventWithMetaData;

class EventsApplierOnAggregate
{
    /**
     * @param $aggregate
     * @param EventWithMetaData[] $priorEvents
     */
    public function applyEventsOnAggregate($aggregate, $priorEvents)
    {
        foreach ($priorEvents as $event) {
            $this->applyEvent($aggregate, $event);
        }
    }

    private function applyEvent($aggregate, EventWithMetaData $eventWithMetaData)
    {
        $methodName = self::getMethodName($eventWithMetaData->getEvent());

        if (\is_callable([$aggregate, $methodName])) {
            $aggregate->$methodName($eventWithMetaData->getEvent(), $eventWithMetaData->getMetaData());
        }
    }

    public static function getMethodName($event)
    {
        $parts = explode('\\', \get_class($event));

        return 'apply' . end($parts);
    }
}