Skip to content

Commit

Permalink
refactor class instatiation
Browse files Browse the repository at this point in the history
  • Loading branch information
dbosen committed Aug 19, 2024
1 parent 8098902 commit c7f455d
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,9 @@ protected function resolve(
$cacheContext
);

return new SearchApiResponse($query);
return $this->classResolver
->getInstanceFromDefinition(SearchApiResponse::class)
->setQuery($query);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Drupal\thunder_gqls\Plugin\GraphQL\DataProducer;

use Drupal\Core\DependencyInjection\ClassResolverInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
Expand Down Expand Up @@ -33,6 +34,13 @@ abstract class ThunderSearchApiProducerBase extends DataProducerPluginBase imple
*/
protected LanguageManagerInterface $languageManager;

/**
* The class resolver service.
*
* @var \Drupal\Core\DependencyInjection\ClassResolverInterface
*/
protected ClassResolverInterface $classResolver;

/**
* {@inheritdoc}
*/
Expand All @@ -50,6 +58,7 @@ public static function create(

$instance->setEntityTypeManager($container->get('entity_type.manager'));
$instance->setLanguageManager($container->get('language_manager'));
$instance->setClassResolver($container->get('class_resolver'));

return $instance;
}
Expand All @@ -74,6 +83,16 @@ public function setLanguageManager(LanguageManagerInterface $languageManager): v
$this->languageManager = $languageManager;
}

/**
* Set the class resolver service.
*
* @param \Drupal\Core\DependencyInjection\ClassResolverInterface $classResolver
* The class resolver service.
*/
public function setClassResolver(ClassResolverInterface $classResolver): void {
$this->classResolver = $classResolver;
}

/**
* Build base search api query.
*
Expand Down
93 changes: 68 additions & 25 deletions modules/thunder_gqls/src/Wrappers/SearchApiResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,21 @@

namespace Drupal\thunder_gqls\Wrappers;

use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use Drupal\Core\Entity\EntityFieldManagerInterface;
use Drupal\facets\Entity\Facet;
use Drupal\search_api\Query\QueryInterface;
use Drupal\search_api\Query\ResultSetInterface;
use Drupal\thunder_gqls\GraphQL\Buffers\SearchApiResultBuffer;
use GraphQL\Deferred;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
* SearchApi Result graphql wrapper.
*
* @package Drupal\thunder_gqls
*/
class SearchApiResponse implements SearchApiResponseInterface {
class SearchApiResponse implements SearchApiResponseInterface, ContainerInjectionInterface {

/**
* The Search Api Query.
Expand All @@ -31,42 +35,87 @@ class SearchApiResponse implements SearchApiResponseInterface {
/**
* Array of Facets.
*
* @var ?array
* @var array
*/
private ?array $facets;
protected array $facets;

/**
* Array of Facet mapping.
*
* @var ?array
* @var array
*/
private ?array $facetMapping;
protected array $facetMapping;

/**
* The bundle for fetching facet field information.
*
* @var ?string
* @var string
*/
private ?string $bundle;
protected string $bundle;

/**
* SearchApiResponse Constructor.
*
* @param \Drupal\thunder_gqls\GraphQL\Buffers\SearchApiResultBuffer $buffer
* The search api result buffer.
* @param \Drupal\Core\Entity\EntityFieldManagerInterface $entityFieldManager
* The entity type manager.
*/
public function __construct(protected SearchApiResultBuffer $buffer, protected EntityFieldManagerInterface $entityFieldManager) {
$this->result = NULL;
}

/**
* {@inheritDoc}
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('thunder_gqls.buffer.search_api_result'),
$container->get('entity_field.manager'),
);
}

/**
* Set query.
*
* @param \Drupal\search_api\Query\QueryInterface $query
* The search api query.
* @param array|null $facets
* The facets.
* @param array|null $facetMapping
* The facet mapping.
* @param string|null $bundle
* The bundle.
* The query.
*/
public function __construct(QueryInterface $query, mixed $facets = NULL, ?array $facetMapping = NULL, ?string $bundle = NULL) {
public function setQuery(QueryInterface $query): SearchApiResponse {
$this->query = $query;
$this->result = NULL;
$this->facets = $facets;
return $this;
}

/**
* Set Facet mapping.
*
* @param array $facetMapping

Check failure on line 92 in modules/thunder_gqls/src/Wrappers/SearchApiResponse.php

View workflow job for this annotation

GitHub Actions / drupal-coder (drupal)

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

Missing parameter comment
Raw output
/github/workspace/modules/thunder_gqls/src/Wrappers/SearchApiResponse.php:92:6: error: Missing parameter comment (Drupal.Commenting.FunctionComment.MissingParamComment)
*/
public function setFacetMapping(array $facetMapping): SearchApiResponse {
$this->facetMapping = $facetMapping;
return $this;
}

/**
* Set bundle.
*
* @param string $bundle
* The bundle.
*/
public function setBundle(string $bundle): SearchApiResponse {
$this->bundle = $bundle;
return $this;
}

/**
* Set facets.
*
* @param array $facets
* The facets.
*/
public function setFacets(array $facets): SearchApiResponse {
$this->facets = $facets;
return $this;
}

/**
Expand Down Expand Up @@ -109,9 +158,6 @@ public function items(): array|Deferred {
$this->result = $this->query->execute();
}

// @phpstan-ignore-next-line
$searchApiResultBuffer = \Drupal::service('thunder_gqls.buffer.search_api_result');

$ids = array_map(static function ($item) {
return $item->getId();
}, $this->result->getResultItems());
Expand All @@ -122,7 +168,7 @@ public function items(): array|Deferred {
return [];
}

$callback = $searchApiResultBuffer->add(
$callback = $this->buffer->add(
$this->query->getIndex()->id(),
array_values($ids)
);
Expand Down Expand Up @@ -201,10 +247,7 @@ private function processFacetResultsFromFieldConfig(
}

$fieldName = $facet->getFieldIdentifier();
/** @var \Drupal\Core\Entity\EntityFieldManagerInterface $entityFieldManager */
// @phpstan-ignore-next-line
$entityFieldManager = \Drupal::service('entity_field.manager');
$fieldConfig = $entityFieldManager->getFieldDefinitions('node', $this->bundle);
$fieldConfig = $this->entityFieldManager->getFieldDefinitions('node', $this->bundle);

if (isset($fieldConfig[$fieldName])) {
$allowedValues = options_allowed_values($fieldConfig[$fieldName]->getFieldStorageDefinition());
Expand Down

0 comments on commit c7f455d

Please sign in to comment.