Example #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);
 }
Example #2
0
 /**
  * Registers a command handler
  *
  * @param string         $commandClass The full command class name
  * @param CommandHandler $handler      The command handler
  *
  * @return void
  *
  * @throws DomainException When the command class is not valid
  */
 public function registerHandler(string $commandClass, CommandHandler $handler)
 {
     if (!Validate::implementsInterface($commandClass, Command::class)) {
         $message = sprintf('Invalid command class: %s', $commandClass);
         throw new DomainException($message);
     }
     $type = Type::create($commandClass)->toString();
     $this->handlers[$type] = $handler;
 }
Example #3
0
 /**
  * Registers a query handler
  *
  * @param string $queryClass  The full query class name
  * @param string $serviceName The handler service name
  *
  * @return void
  *
  * @throws DomainException When the query class is not valid
  */
 public function registerHandler(string $queryClass, string $serviceName)
 {
     if (!Validate::implementsInterface($queryClass, Query::class)) {
         $message = sprintf('Invalid query class: %s', $queryClass);
         throw new DomainException($message);
     }
     $type = Type::create($queryClass)->toString();
     $this->handlers[$type] = $serviceName;
 }
Example #4
0
 /**
  * {@inheritdoc}
  */
 public function deserialize(string $state) : Serializable
 {
     $data = json_decode($state, true);
     $keys = ['@', '$'];
     foreach ($keys as $key) {
         if (!isset($data[$key])) {
             $message = sprintf('Invalid serialization format: %s', $state);
             throw new DomainException($message);
         }
     }
     $class = ClassName::full($data['@']);
     assert(Validate::implementsInterface($class, Serializable::class), sprintf('Unable to deserialize: %s', $class));
     return $class::deserialize($data['$']);
 }
 /**
  * Registers a subscriber service to handle events
  *
  * The subscriber class must implement:
  * Novuso\Common\Domain\Messaging\Event\EventSubscriber
  *
  * @param string $className The subscriber class name
  * @param string $serviceId The subscriber service ID
  *
  * @return void
  */
 public function registerService(string $className, string $serviceId)
 {
     assert(Validate::implementsInterface($className, EventSubscriber::class), sprintf('Invalid subscriber class: %s', $className));
     /** @var EventSubscriber $className The subscriber class name */
     foreach ($className::eventRegistration() as $eventType => $params) {
         if (is_string($params)) {
             $this->addHandlerService($eventType, $serviceId, $params);
         } elseif (is_string($params[0])) {
             $priority = isset($params[1]) ? (int) $params[1] : 0;
             $this->addHandlerService($eventType, $serviceId, $params[0], $priority);
         } else {
             foreach ($params as $handler) {
                 $priority = isset($handler[1]) ? (int) $handler[1] : 0;
                 $this->addHandlerService($eventType, $serviceId, $handler[0], $priority);
             }
         }
     }
 }
Example #6
0
 /**
  * Creates collection of comparable items
  *
  * If a type is not provided, the item type is dynamic.
  *
  * The item type must be a fully-qualified class name that implements:
  * `Novuso\System\Type\Comparable`
  *
  * @param string|null $itemType The item type
  *
  * @return SortedSet
  */
 public static function comparable(string $itemType = null) : SortedSet
 {
     assert(Validate::isNull($itemType) || Validate::implementsInterface($itemType, Comparable::class), sprintf('%s expects $itemType to implement %s', __METHOD__, Comparable::class));
     return new static(new ComparableComparator(), $itemType);
 }
Example #7
0
 /**
  * @dataProvider invalidImplementsProvider
  */
 public function test_that_implements_interface_returns_false_for_invalid_value($value, $interface)
 {
     $this->assertFalse(Validate::implementsInterface($value, $interface));
 }