Skip to content

Commit

Permalink
refactor EntityListResponse
Browse files Browse the repository at this point in the history
  • Loading branch information
dbosen committed Aug 20, 2024
1 parent eed2317 commit 8a07c23
Showing 1 changed file with 42 additions and 6 deletions.
48 changes: 42 additions & 6 deletions modules/thunder_gqls/src/Wrappers/EntityListResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,62 @@

namespace Drupal\thunder_gqls\Wrappers;

use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use Drupal\Core\Entity\Query\QueryInterface;
use Drupal\graphql\GraphQL\Buffers\EntityBuffer;
use GraphQL\Deferred;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
* The thunder entity list response class.
*/
class EntityListResponse implements EntityListResponseInterface {
class EntityListResponse implements EntityListResponseInterface, ContainerInjectionInterface {

/**
* The query interface.
*
* @var \Drupal\Core\Entity\Query\QueryInterface
*/
protected $query;
protected QueryInterface $query;

/**
* The entity buffer.
*
* @var \Drupal\graphql\GraphQL\Buffers\EntityBuffer
*/
private EntityBuffer $buffer;

/**
* EntityListResponse constructor.
*
* @param \Drupal\Core\Entity\Query\QueryInterface|\Drupal\graphql\GraphQL\Buffers\EntityBuffer $parameter

Check failure on line 33 in modules/thunder_gqls/src/Wrappers/EntityListResponse.php

View workflow job for this annotation

GitHub Actions / drupal-coder (drupal)

[drupal-coder (drupal)] modules/thunder_gqls/src/Wrappers/EntityListResponse.php#L33 <Drupal.Commenting.FunctionComment.MissingParamComment>

Missing parameter comment
Raw output
/github/workspace/modules/thunder_gqls/src/Wrappers/EntityListResponse.php:33:6: error: Missing parameter comment (Drupal.Commenting.FunctionComment.MissingParamComment)
*/
public function __construct(QueryInterface|EntityBuffer $parameter) {
if ($parameter instanceof QueryInterface) {
$this->setQuery($parameter);
return;
}
$this->buffer = $parameter;
}

/**
* {@inheritDoc}
*/
public static function create(ContainerInterface $container): self {
return new static(
$container->get('graphql.buffer.entity'),
);
}

/**
* Set query.
*
* @param \Drupal\Core\Entity\Query\QueryInterface $query
* The query interface.
* The query.
*/
public function __construct(QueryInterface $query) {
public function setQuery(QueryInterface $query): EntityListResponse {
$this->query = $query;
return $this;
}

/**
Expand All @@ -51,8 +84,11 @@ public function items() {
return [];
}

$buffer = \Drupal::service('graphql.buffer.entity');
$callback = $buffer->add($this->query->getEntityTypeId(), array_values($result));
if (empty($this->buffer)) {
$this->buffer = \Drupal::service('graphql.buffer.entity');

Check warning on line 88 in modules/thunder_gqls/src/Wrappers/EntityListResponse.php

View workflow job for this annotation

GitHub Actions / drupal-coder (drupal practice)

[drupal-coder (drupal practice)] modules/thunder_gqls/src/Wrappers/EntityListResponse.php#L88 <DrupalPractice.Objects.GlobalDrupal.GlobalDrupal>

\Drupal calls should be avoided in classes, use dependency injection instead
Raw output
/github/workspace/modules/thunder_gqls/src/Wrappers/EntityListResponse.php:88:24: warning: \Drupal calls should be avoided in classes, use dependency injection instead (DrupalPractice.Objects.GlobalDrupal.GlobalDrupal)
}

$callback = $this->buffer->add($this->query->getEntityTypeId(), array_values($result));
return new Deferred(fn() => $callback());
}

Expand Down

0 comments on commit 8a07c23

Please sign in to comment.