for testing and deploying your application
for finding and fixing issues
for empowering human code reviews
<?php
/*
* This file is part of the Kreta package.
*
* (c) Beñat Espiña <[email protected]>
* (c) Gorka Laucirica <[email protected]>
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace Kreta\SharedKernel\Projection;
use Kreta\SharedKernel\Domain\Model\DomainEventCollection;
use Kreta\SharedKernel\Domain\Model\Exception;
use Kreta\SharedKernel\Domain\ReadEvent\EventHandler;
final class Projector
{
private $eventHandlers;
private static $instance = null;
public static function instance() : self
if (null === self::$instance) {
self::$instance = new self();
}
return self::$instance;
private function __construct()
$this->eventHandlers = [];
public function __clone()
throw new Exception('Clone is not supported');
public function register(array $eventHandlers)
foreach ($eventHandlers as $eventHandler) {
$this->add($eventHandler);
private function add(EventHandler $eventHandler)
$this->eventHandlers[$eventHandler->isSubscribeTo()] = $eventHandler;
public function project(DomainEventCollection $events) : void
foreach ($events->toArray() as $event) {
if (isset($this->eventHandlers[get_class($event)])) {
$this->eventHandlers[get_class($event)]->handle($event);