/** * @param string $eventClassName * @param string $serialization * @return Event * @throws EventSourcingException when failed to deserialize */ public function deserialize($eventClassName, $serialization) { Assert::argumentNotEmpty($eventClassName, 'Event class name is required'); Assert::argumentClassExists($eventClassName, 'Event class does not exist: %class%'); Assert::argumentSubclassOf($eventClassName, 'Goodby\\EventSourcing\\Event', 'Event class must be subclass of %parent%'); $data = json_decode($serialization, true); if (json_last_error() != JSON_ERROR_NONE) { throw EventSourcingException::failedToDeserializeEvent($this->getLastErrorMessage()); } /** @var $eventClassName Event */ return $eventClassName::fromContractualData($data); }
/** * @param string $lastDispatchedEventId * @throws \Exception * @return DispatchableEvent[] */ public function dispatchableEventsSince($lastDispatchedEventId) { try { $statement = $this->connection->prepare("SELECT event_id, event_body, event_type FROM {$this->tableName} WHERE event_id > :event_id ORDER BY event_id"); $statement->bindValue(':event_id', $lastDispatchedEventId); $statement->execute(); $events = []; foreach ($statement as $result) { $events[] = new DispatchableEvent($result['event_id'], $this->deserializeEvent($result['event_type'], $result['event_body'])); } return $events; } catch (Exception $because) { throw EventSourcingException::cannotQueryDispatchableEventsSince($lastDispatchedEventId, $because); } }