protected function applyValues(EmbeddedEntityEventInterface $event) { if (!is_callable([$event, 'getData'])) { throw new RuntimeError('Event instance is not support due to lack of a getData method.'); } $this->setValues($event->getData()); if (!$this->isValid()) { foreach ($this->getValidationResults() as $validation_result) { foreach ($validation_result->getViolatedRules() as $violated_rule) { foreach ($violated_rule->getIncidents() as $incident) { $errors[] = PHP_EOL . $validation_result->getSUbject()->getName() . ' - ' . $violated_rule->getName() . ' > ' . $incident->getName() . ', params: ' . var_export($incident->getParameters(), true); } } } error_log(sprintf("Corrupt event data given to %s through event %s.\nErrors:%s", $this->getType()->getPrefix(), $event->getType(), implode(PHP_EOL, $errors))); } $embedded_entity_events = new EmbeddedEntityEventList(); foreach ($event->getEmbeddedEntityEvents() as $embedded_event) { $embedded_entity_events->push($this->applyEmbeddedEntityEvent($embedded_event)); } $source_event = $event; $recorded_changes = $this->getRecordedChanges(); if (!empty($recorded_changes)) { $source_event = $event->createCopyWith(['data' => $recorded_changes, 'embedded_entity_events' => $embedded_entity_events]); } return $source_event; }
/** * Apply the given aggregate-event to it's corresponding aggregate and return the resulting source-event. * * @param EmbeddedEntityEventInterface $embedded_entity_event * @param boolean $auto_commit * * @return EmbeddedEntityEventInterface */ protected function applyEmbeddedEntityEvent(EmbeddedEntityEventInterface $embedded_entity_event, $auto_commit = true) { $attribute_name = $embedded_entity_event->getParentAttributeName(); $embedded_entity_list = $this->getValue($attribute_name); if ($embedded_entity_event instanceof EmbeddedEntityAddedEvent) { $embedded_type = $this->getEmbeddedEntityTypeFor($attribute_name, $embedded_entity_event->getEmbeddedEntityType()); $embedded_entity = $embedded_type->createEntity([], $this); $embedded_entity_list->push($embedded_entity); } elseif ($embedded_entity_event instanceof EmbeddedEntityRemovedEvent) { $embedded_entity = $this->getEmbeddedEntityFor($attribute_name, $embedded_entity_event->getEmbeddedEntityIdentifier()); $embedded_entity_list->removeItem($embedded_entity); if (!$embedded_entity) { error_log(__METHOD__ . " - Embedded entity already was removed."); return $embedded_entity_event; } } elseif ($embedded_entity_event instanceof EmbeddedEntityModifiedEvent) { $embedded_entity = $this->getEmbeddedEntityFor($attribute_name, $embedded_entity_event->getEmbeddedEntityIdentifier()); if (!$embedded_entity) { throw new RuntimeError('Unable to resolve embedded-entity for embed-event: ' . json_encode($embedded_entity_event->toArray()) . "\nAR-Id: " . $this->getIdentifier()); } if ($embedded_entity_list->getKey($embedded_entity) !== $embedded_entity_event->getPosition()) { $embedded_entity_list->moveTo($embedded_entity_event->getPosition(), $embedded_entity); } } return $embedded_entity->applyEvent($embedded_entity_event, $auto_commit); }
public function conflictsWith(EmbeddedEntityEventInterface $event, array &$conflicting_changes = []) { if ($event->getEmbeddedEntityIdentifier() !== $this->getEmbeddedEntityIdentifier()) { return false; } $conflict_detected = false; // @todo check against conflict with other modify event (resolvable) or remove (unresolvable) return $conflict_detected; }
public function conflictsWith(EmbeddedEntityEventInterface $event, array &$conflicting_changes = []) { return $event->getEmbeddedEntityIdentifier() === $this->getEmbeddedEntityIdentifier(); }
protected function getEmbeddedEntityTypeFor(EntityInterface $projection, EmbeddedEntityEventInterface $event) { $embed_attribute = $projection->getType()->getAttribute($event->getParentAttributeName()); return $embed_attribute->getEmbeddedTypeByPrefix($event->getEmbeddedEntityType()); }