Ejemplo n.º 1
0
 /**
  * {@inheritdoc}
  */
 public static function deserialize(array $data) : QueryMessage
 {
     $keys = ['id', 'type', 'timestamp', 'meta_data', 'payload_type', 'payload'];
     foreach ($keys as $key) {
         if (!isset($data[$key])) {
             $message = sprintf('Invalid serialization data: %s', VarPrinter::toString($data));
             throw new DomainException($message);
         }
     }
     if ($data['type'] !== MessageType::QUERY) {
         $message = sprintf('Invalid message type: %s', $data['type']);
         throw new DomainException($message);
     }
     /** @var MessageId $id */
     $id = MessageId::fromString($data['id']);
     /** @var DateTime $timestamp */
     $timestamp = DateTime::fromString($data['timestamp']);
     /** @var MetaData $metaData */
     $metaData = MetaData::create($data['meta_data']);
     /** @var Type $payloadType */
     $payloadType = Type::create($data['payload_type']);
     /** @var string $payloadClass */
     $payloadClass = $payloadType->toClassName();
     assert(Validate::implementsInterface($payloadClass, Query::class), sprintf('Unable to deserialize: %s', $payloadClass));
     /** @var Query $payload */
     $payload = $payloadClass::fromArray($data['payload']);
     return new static($id, $timestamp, $payload, $metaData);
 }
Ejemplo n.º 2
0
 /**
  * {@inheritdoc}
  */
 public function compareTo($object)
 {
     if ($this === $object) {
         return 0;
     }
     assert(Test::areSameType($this, $object), sprintf('Comparison requires instance of %s', static::class));
     return $this->timestamp->compareTo($object->timestamp);
 }
Ejemplo n.º 3
0
 /**
  * {@inheritdoc}
  */
 public function execute(Command $command)
 {
     try {
         $timetamp = DateTime::now();
         $messageId = MessageId::generate();
         $metaData = new MetaData();
         $this->pipe(new DomainCommandMessage($messageId, $timetamp, $command, $metaData));
     } catch (Exception $exception) {
         throw CommandException::create($exception->getMessage(), $exception);
     }
 }
Ejemplo n.º 4
0
 /**
  * {@inheritdoc}
  */
 public function fetch(Query $query)
 {
     try {
         $timetamp = DateTime::now();
         $messageId = MessageId::generate();
         $metaData = new MetaData();
         $data = $this->pipe(new DomainQueryMessage($messageId, $timetamp, $query, $metaData));
     } catch (Exception $exception) {
         throw QueryException::create($exception->getMessage(), $exception);
     }
     return $data;
 }
Ejemplo n.º 5
0
 public function setUp()
 {
     $thingId = ThingId::fromString('014ec11d-2f21-4d33-a624-5df1196a4f85');
     $thingType = Type::create(Thing::class);
     $messageId = MessageId::fromString('014ec11e-4343-49cd-9b7a-cdd4ced5cedc');
     $timestamp = DateTime::fromString('2015-01-01T13:12:31.045234[America/Chicago]');
     $payload = new ThingHappenedEvent('foo', 'bar');
     $metaData = new MetaData(['ip_address' => '127.0.0.1']);
     $sequence = 0;
     $this->eventMessage = new DomainEventMessage($thingId, $thingType, $messageId, $timestamp, $payload, $metaData, $sequence);
     $this->storedEvent = new StoredEvent($this->eventMessage);
 }
Ejemplo n.º 6
0
 /**
  * Converts a value from its database representation to its PHP representation
  *
  * @param mixed            $value    The value to convert
  * @param AbstractPlatform $platform The currently used database platform
  *
  * @return mixed
  *
  * @throws ConversionException When the conversion fails
  */
 public function convertToPHPValue($value, AbstractPlatform $platform)
 {
     if (empty($value)) {
         return null;
     }
     if ($value instanceof DateTime) {
         return $value;
     }
     try {
         $dateTime = DateTime::fromString($value);
     } catch (Exception $exception) {
         throw ConversionException::conversionFailed($value, static::TYPE_NAME);
     }
     return $dateTime;
 }
Ejemplo n.º 7
0
 public function test_that_event_is_dispatched_to_attached_service()
 {
     $dispatcher = $this->container->get('event.dispatcher');
     $dispatcher->attachService('test.subscriber', TestSubscriber::class);
     $dispatcher = new FrozenDispatcher($dispatcher);
     $thingId = ThingId::fromString('014ec11d-2f21-4d33-a624-5df1196a4f85');
     $thingType = Type::create(Thing::class);
     $messageId = MessageId::fromString('014ec11e-4343-49cd-9b7a-cdd4ced5cedc');
     $timestamp = DateTime::fromString('2015-01-01T13:12:31.045234[America/Chicago]');
     $payload = new ThingHappenedEvent('foo', 'bar');
     $metaData = new MetaData();
     $sequence = 0;
     $eventMessage = new DomainEventMessage($thingId, $thingType, $messageId, $timestamp, $payload, $metaData, $sequence);
     $dispatcher->dispatch($eventMessage);
     $this->assertTrue($this->container->get('test.subscriber')->thingHappened());
 }
Ejemplo n.º 8
0
 public function test_that_all_events_key_subscribes_to_any_event()
 {
     $subscriber = new EventLogSubscriber();
     $this->dispatcher->attach($subscriber);
     $thingId = ThingId::fromString('014ec11d-2f21-4d33-a624-5df1196a4f85');
     $thingType = Type::create(Thing::class);
     $messageId = MessageId::fromString('014ec11e-4343-49cd-9b7a-cdd4ced5cedc');
     $timestamp = DateTime::fromString('2015-01-01T13:12:31.045234[America/Chicago]');
     $payload = new ThingHappenedEvent('foo', 'bar');
     $metaData = new MetaData();
     $sequence = 0;
     $eventMessage = new DomainEventMessage($thingId, $thingType, $messageId, $timestamp, $payload, $metaData, $sequence);
     $this->dispatcher->dispatch($eventMessage);
     $logs = $subscriber->getLogs();
     $expected = '{"message_id":"014ec11e-4343-49cd-9b7a-cdd4ced5cedc",' . '"timestamp":"2015-01-01T13:12:31.045234[America/Chicago]",' . '"event_type":"Novuso.Test.Common.Doubles.Domain.Messaging.Event.ThingHappenedEvent",' . '"event_data":{"foo":"foo","bar":"bar"},"meta_data":[],' . '"aggregate_type":"Novuso.Test.Common.Doubles.Domain.Model.Thing",' . '"aggregate_id":"014ec11d-2f21-4d33-a624-5df1196a4f85","sequence":0}';
     $this->assertSame($expected, $logs[0]);
 }
Ejemplo n.º 9
0
 /**
  * Retrieves a native DateTime instance
  *
  * @return DateTimeInterface
  */
 public function toNative()
 {
     $time = sprintf('%d.%06d', $this->timestamp(), $this->micro());
     $dateTime = NativeDateTime::createFromFormat('U.u', $time, new DateTimeZone('UTC'));
     $dateTime->setTimezone(new DateTimeZone($this->timezone->toString()));
     return $dateTime;
 }
Ejemplo n.º 10
0
 /**
  * @expectedException \AssertionError
  */
 public function test_that_compare_to_throws_exception_for_invalid_argument()
 {
     $dateTime = DateTime::create(2015, 6, 20, 16, 30, 6, 'America/Chicago');
     $dateTime->compareTo('2015-06-20T16:30:06[America/Chicago]');
 }
Ejemplo n.º 11
0
 /**
  * Records a domain event
  *
  * @param DomainEvent $domainEvent The domain event
  *
  * @return void
  */
 public function record(DomainEvent $domainEvent)
 {
     $dateTime = DateTime::now();
     $messageId = MessageId::generate();
     $metaData = new MetaData();
     $message = new DomainEventMessage($this->aggregateId, $this->aggregateType, $messageId, $dateTime, $domainEvent, $metaData, $this->nextSequence());
     $this->lastSequence = $message->sequence();
     $this->messages->add($message);
 }
Ejemplo n.º 12
0
 protected function getNextCommand()
 {
     $messageId = MessageId::fromString('014ec11f-317e-475a-9d5f-b8ae6c20aab1');
     $timestamp = DateTime::fromString('2015-01-02T10:34:12.672291[America/Chicago]');
     $metaData = new MetaData();
     $payload = new MakeThingCommand('foo', 'bar');
     return new DomainCommandMessage($messageId, $timestamp, $payload, $metaData);
 }
Ejemplo n.º 13
0
 protected function getEventMessages()
 {
     $messageId = MessageId::fromString('014ec11e-4343-49cd-9b7a-cdd4ced5cedc');
     $dateTime = DateTime::fromString('2015-01-01T13:12:31.045234[America/Chicago]');
     $payload = new ThingHappenedEvent('Leeroy Jenkins', 'ljenkins');
     $sequence = 4;
     $eventMessage1 = new DomainEventMessage($this->thingId, $this->thingType, $messageId, $dateTime, $payload, $this->metaData, $sequence);
     $messageId = MessageId::fromString('014ec11f-317e-475a-9d5f-b8ae6c20aab1');
     $dateTime = DateTime::fromString('2015-01-02T10:34:12.672291[America/Chicago]');
     $payload = new ThingHappenedEvent('John Smith', 'jsmith');
     $sequence = 5;
     $eventMessage2 = new DomainEventMessage($this->thingId, $this->thingType, $messageId, $dateTime, $payload, $this->metaData, $sequence);
     $messageId = MessageId::fromString('014ec170-e319-4875-937d-ead12b479682');
     $dateTime = DateTime::fromString('2015-01-05T14:03:31.245115[America/Chicago]');
     $payload = new ThingHappenedEvent('George Fox', 'gfox');
     $sequence = 6;
     $eventMessage3 = new DomainEventMessage($this->thingId, $this->thingType, $messageId, $dateTime, $payload, $this->metaData, $sequence);
     return [$eventMessage1, $eventMessage2, $eventMessage3];
 }
Ejemplo n.º 14
0
 protected function getNextQuery()
 {
     $messageId = MessageId::fromString('014ec11f-317e-475a-9d5f-b8ae6c20aab1');
     $timestamp = DateTime::fromString('2015-01-02T10:34:12.672291[America/Chicago]');
     $metaData = new MetaData();
     $payload = new GetThingQuery('014ec11d-2f21-4d33-a624-5df1196a4f85');
     return new DomainQueryMessage($messageId, $timestamp, $payload, $metaData);
 }
Ejemplo n.º 15
0
 protected function setUp()
 {
     $this->message = new EventMessage(MessageId::fromString('0150deae-68df-40ca-aea1-6b4b06aadfc3'), DateTime::fromString('2015-11-06T15:23:03[America/Chicago]'), UserRegisteredEvent::fromArray(['prefix' => null, 'first_name' => 'James', 'middle_name' => 'D', 'last_name' => 'Smith', 'suffix' => null, 'email' => '*****@*****.**']), MetaData::create(['foo' => 'bar']));
 }
Ejemplo n.º 16
0
 protected function setUp()
 {
     $this->message = new QueryMessage(MessageId::fromString('0150deae-68df-40ca-aea1-6b4b06aadfc3'), DateTime::fromString('2015-11-06T15:23:03[America/Chicago]'), UserByEmailQuery::fromArray(['email' => '*****@*****.**']), MetaData::create(['foo' => 'bar']));
 }
Ejemplo n.º 17
0
 /**
  * Retrieves a native DateTime instance
  *
  * @return DateTimeInterface
  */
 public function toNative() : DateTimeInterface
 {
     $time = sprintf('%04d-%02d-%02dT%02d:%02d:%02d', $this->year(), $this->month(), $this->day(), $this->hour(), $this->minute(), $this->second());
     return NativeDateTime::createFromFormat(static::STRING_FORMAT, $time, new DateTimeZone($this->timezone->toString()));
 }
Ejemplo n.º 18
0
 /**
  * Converts data to an event message
  *
  * @return EventMessage
  */
 public function toEventMessage()
 {
     $aggregateIdClass = Type::create($this->aggregateIdType)->toClassName();
     $aggregateId = $aggregateIdClass::fromString($this->aggregateId);
     $aggregateType = Type::create($this->aggregateType);
     $messageId = MessageId::fromString($this->messageId);
     $timestamp = DateTime::fromString($this->timestamp);
     $payload = unserialize($this->payload);
     $metaData = unserialize($this->metaData);
     $sequence = $this->sequence;
     $message = new DomainEventMessage($aggregateId, $aggregateType, $messageId, $timestamp, $payload, $metaData, $sequence);
     return $message;
 }
Ejemplo n.º 19
0
 protected function getNextEvent()
 {
     $thingId = ThingId::fromString('014ec11d-2f21-4d33-a624-5df1196a4f85');
     $thingType = Type::create(Thing::class);
     $messageId = MessageId::fromString('014ec11f-317e-475a-9d5f-b8ae6c20aab1');
     $timestamp = DateTime::fromString('2015-01-02T10:34:12.672291[America/Chicago]');
     $metaData = new MetaData();
     $payload = new ThingHappenedEvent('foo', 'bar');
     $sequence = 1;
     return new DomainEventMessage($thingId, $thingType, $messageId, $timestamp, $payload, $metaData, $sequence);
 }
Ejemplo n.º 20
0
 /**
  * @expectedException Novuso\System\Exception\DomainException
  */
 public function test_that_from_string_throws_exception_for_invalid_format()
 {
     DateTime::fromString('2015-06-20T16:30:06.032401-05:00');
 }
Ejemplo n.º 21
0
 protected function setUp()
 {
     $this->message = new CommandMessage(MessageId::fromString('0150deae-68df-40ca-aea1-6b4b06aadfc3'), DateTime::fromString('2015-11-06T15:23:03[America/Chicago]'), RegisterUserCommand::fromArray(['prefix' => null, 'first_name' => 'James', 'middle_name' => 'D', 'last_name' => 'Smith', 'suffix' => null, 'email' => '*****@*****.**', 'password' => '$2y$10$NXQfVDFu3.Tyd97bnm4TPO/jdrbgL918xeXM5USqfB.qIHFB6mgjC']), MetaData::create(['foo' => 'bar']));
 }
Ejemplo n.º 22
0
 /**
  * {@inheritdoc}
  */
 public function toArray() : array
 {
     return ['id' => $this->id->toString(), 'type' => $this->type->value(), 'timestamp' => $this->timestamp->toString(), 'meta_data' => $this->metaData->toArray(), 'payload_type' => $this->payloadType->toString(), 'payload' => $this->payload->toArray()];
 }