From c7f455d5c2d3baf949258015c5c409279b70f036 Mon Sep 17 00:00:00 2001 From: Daniel Bosen Date: Mon, 19 Aug 2024 17:54:05 +0200 Subject: [PATCH] refactor class instatiation --- .../GraphQL/DataProducer/ThunderSearchApi.php | 4 +- .../ThunderSearchApiProducerBase.php | 19 ++++ .../src/Wrappers/SearchApiResponse.php | 93 ++++++++++++++----- 3 files changed, 90 insertions(+), 26 deletions(-) diff --git a/modules/thunder_gqls/src/Plugin/GraphQL/DataProducer/ThunderSearchApi.php b/modules/thunder_gqls/src/Plugin/GraphQL/DataProducer/ThunderSearchApi.php index c824a81af..d3fc2924e 100644 --- a/modules/thunder_gqls/src/Plugin/GraphQL/DataProducer/ThunderSearchApi.php +++ b/modules/thunder_gqls/src/Plugin/GraphQL/DataProducer/ThunderSearchApi.php @@ -81,7 +81,9 @@ protected function resolve( $cacheContext ); - return new SearchApiResponse($query); + return $this->classResolver + ->getInstanceFromDefinition(SearchApiResponse::class) + ->setQuery($query); } } diff --git a/modules/thunder_gqls/src/Plugin/GraphQL/DataProducer/ThunderSearchApiProducerBase.php b/modules/thunder_gqls/src/Plugin/GraphQL/DataProducer/ThunderSearchApiProducerBase.php index de1f9727d..c0ab04b1a 100644 --- a/modules/thunder_gqls/src/Plugin/GraphQL/DataProducer/ThunderSearchApiProducerBase.php +++ b/modules/thunder_gqls/src/Plugin/GraphQL/DataProducer/ThunderSearchApiProducerBase.php @@ -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; @@ -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} */ @@ -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; } @@ -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. * diff --git a/modules/thunder_gqls/src/Wrappers/SearchApiResponse.php b/modules/thunder_gqls/src/Wrappers/SearchApiResponse.php index 0e1fd145f..112b444b0 100644 --- a/modules/thunder_gqls/src/Wrappers/SearchApiResponse.php +++ b/modules/thunder_gqls/src/Wrappers/SearchApiResponse.php @@ -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. @@ -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 + */ + 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; } /** @@ -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()); @@ -122,7 +168,7 @@ public function items(): array|Deferred { return []; } - $callback = $searchApiResultBuffer->add( + $callback = $this->buffer->add( $this->query->getIndex()->id(), array_values($ids) ); @@ -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());