public function add(AggregateContainer $container) { $aggregateRootId = $container->aggregateRootId(); $aggregate = $container->aggregate(); if (!isset($this->containers[$aggregateRootId])) { $objectHash = spl_object_hash($aggregate); if (isset($this->aggregateRootIds[$objectHash])) { throw new AggregatePersistedWithMultipleIdsException(); } $this->containers[$aggregateRootId] = $container; $this->aggregateRootIds[$objectHash] = $aggregateRootId; $container->onSetAggregate(function ($newAggregateInstance) use($aggregateRootId) { $newObjectHash = spl_object_hash($newAggregateInstance); $this->aggregateRootIds[$newObjectHash] = $aggregateRootId; }); } elseif ($aggregate !== $this->containers[$aggregateRootId]->aggregate()) { throw new AggregateIdCollisionException(); } }