You are here

class EntityParameterFactory in JSON-RPC 8

Same name and namespace in other branches
  1. 2.x src/ParameterFactory/EntityParameterFactory.php \Drupal\jsonrpc\ParameterFactory\EntityParameterFactory

A factory to create loaded entities from entity type & UUID user input.

Hierarchy

Expanded class hierarchy of EntityParameterFactory

File

src/ParameterFactory/EntityParameterFactory.php, line 19

Namespace

Drupal\jsonrpc\ParameterFactory
View source
class EntityParameterFactory extends ParameterFactoryBase {

  /**
   * The entity repository.
   *
   * @var \Drupal\Core\Entity\EntityRepositoryInterface
   */
  protected $entityRepository;

  /**
   * EntityParameterFactory constructor.
   *
   * @param \Drupal\jsonrpc\ParameterDefinitionInterface $definition
   *   The parameter definition.
   * @param \JsonSchema\Validator $validator
   *   The validator to ensure the user input is valid.
   * @param \Drupal\Core\Entity\EntityRepositoryInterface $entity_repository
   *   The entity type repository to load entities by UUID.
   */
  public function __construct(ParameterDefinitionInterface $definition, Validator $validator, EntityRepositoryInterface $entity_repository) {
    parent::__construct($definition, $validator);
    $this->entityRepository = $entity_repository;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ParameterDefinitionInterface $definition, ContainerInterface $container) {
    return new static($definition, $container
      ->get('jsonrpc.schema_validator'), $container
      ->get('entity.repository'));
  }

  /**
   * {@inheritdoc}
   */
  public static function schema(ParameterDefinitionInterface $parameter_definition = NULL) {
    return [
      'type' => 'object',
      'properties' => [
        'type' => [
          'type' => 'string',
        ],
        'uuid' => [
          'type' => 'string',
        ],
      ],
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function getOutputValidator() {
    return new InstanceofValidator(EntityInterface::class);
  }

  /**
   * {@inheritdoc}
   */
  protected function doTransform($data, Context $context = NULL) {
    try {
      if ($entity = $this->entityRepository
        ->loadEntityByUuid($data['type'], $data['uuid'])) {
        return $entity;
      }
      throw JsonRpcException::fromError(Error::invalidParams('The requested entity could not be found.'));
    } catch (EntityStorageException $e) {
      throw JsonRpcException::fromError(Error::invalidParams('This entity type is not supported. Error: ' . $e
        ->getMessage()));
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
EntityParameterFactory::$entityRepository protected property The entity repository.
EntityParameterFactory::create public static function Instantiates a new instance of this class. Overrides ParameterFactoryBase::create
EntityParameterFactory::doTransform protected function
EntityParameterFactory::getOutputValidator public function
EntityParameterFactory::schema public static function An array representing the JSON Schema for acceptable input to the factory. Overrides ParameterFactoryInterface::schema
EntityParameterFactory::__construct public function EntityParameterFactory constructor. Overrides ParameterFactoryBase::__construct
ParameterFactoryBase::$definition protected property The parameter definition.
ParameterFactoryBase::$inputValidator protected property The validation class for shaper interactions.
ParameterFactoryBase::$validator protected property The schema validator to ensure the input data adheres to the expectation.
ParameterFactoryBase::getInputValidator public function