Commit 44cf211c authored by e0ipso's avatar e0ipso
Browse files

refactor: split collector

Split it in two single-purpose classes.
parent 97bd7d07
......@@ -7,7 +7,7 @@
use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\typed_entity\RepositoryCollector;
use Drupal\typed_entity\RepositoryManager;
use Drupal\typed_entity_example\TypedRepositories\ArticleRepository;
use Drupal\typed_entity_example\WrappedEntities\Article;
......@@ -18,9 +18,9 @@ function typed_entity_example_node_view_alter(array &$build, EntityInterface $en
if ($entity->bundle() !== 'article') {
return;
}
$repository_collector = \Drupal::service(RepositoryCollector::class);
assert($repository_collector instanceof RepositoryCollector);
$repository = $repository_collector->repositoryFromEntity($entity);
$repository_manager = \Drupal::service(RepositoryManager::class);
assert($repository_manager instanceof RepositoryManager);
$repository = $repository_manager->repositoryFromEntity($entity);
assert($repository instanceof ArticleRepository);
$articles = $repository->findByTags(['Vegetarian', 'heaLthy', 'Baking']);
$build['related'] = [
......@@ -29,7 +29,7 @@ function typed_entity_example_node_view_alter(array &$build, EntityInterface $en
return $article->label();
}, $articles),
];
$author = $repository_collector->wrap($entity)
$author = $repository_manager->wrap($entity)
->owner()
->nickname();
$build['owner'] = [
......
......@@ -2,16 +2,13 @@
namespace Drupal\typed_entity;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\typed_entity\TypedRepositories\TypedEntityRepositoryBase;
use Drupal\typed_entity\WrappedEntities\WrappedEntityInterface;
use Drupal\typed_entity\TypedRepositories\TypedEntityRepositoryInterface;
/**
* Collects the repositories and negotiates from a loaded entity.
* Collects the repositories.
*/
final class RepositoryCollector implements EntityWrapperInterface {
final class RepositoryCollector {
/**
* The collected repositories.
......@@ -70,44 +67,16 @@ final class RepositoryCollector implements EntityWrapperInterface {
}
/**
* Gets the entity repository based on the entity information and the variant.
* Get a repository.
*
* @param \Drupal\Core\Entity\EntityInterface $entity
* The entity to extract info for.
* @param string $repository_id
* The repository identifier.
*
* @return \Drupal\typed_entity\TypedRepositories\TypedEntityRepositoryInterface
* The repository for the entity.
*
* @throws \Drupal\typed_entity\RepositoryNotFoundException
* When the repository was not found.
*
* @todo: The variant negotiation is still missing.
*/
public function repositoryFromEntity(EntityInterface $entity): TypedEntityRepositoryInterface {
$identifier = implode(
TypedEntityRepositoryBase::SEPARATOR,
array_filter([$entity->getEntityTypeId(), $entity->bundle()])
);
$repository = $this->repositories[$identifier];
if (empty($repository)) {
$message = 'Repository with identifier "' . $identifier . '" not found';
throw new RepositoryNotFoundException($message);
}
return $repository;
}
/**
* {@inheritdoc}
*/
public function wrap(EntityInterface $entity): WrappedEntityInterface {
return $this->repositoryFromEntity($entity)->wrap($entity);
}
/**
* {@inheritdoc}
* @return \Drupal\typed_entity\TypedRepositories\TypedEntityRepositoryInterface|null
* The repository.
*/
public function wrapMultiple(array $entities): array {
return array_map([$this, 'wrap'], $entities);
public function get(string $repository_id): ?TypedEntityRepositoryInterface {
return $this->repositories[$repository_id] ?? NULL;
}
}
<?php
namespace Drupal\typed_entity;
use Drupal\Core\Entity\EntityInterface;
use Drupal\typed_entity\TypedRepositories\TypedEntityRepositoryBase;
use Drupal\typed_entity\WrappedEntities\WrappedEntityInterface;
use Drupal\typed_entity\TypedRepositories\TypedEntityRepositoryInterface;
/**
* Repository to wrap entities and negotiate specific repositories.
*/
final class RepositoryManager implements EntityWrapperInterface {
/**
* The repository collector.
*
* @var \Drupal\typed_entity\RepositoryCollector
*/
private $collector;
/**
* RepositoryManager constructor.
*/
public function __construct(RepositoryCollector $collector) {
$this->collector = $collector;
}
/**
* Gets the entity repository based on the entity information and the variant.
*
* @param \Drupal\Core\Entity\EntityInterface $entity
* The entity to extract info for.
*
* @return \Drupal\typed_entity\TypedRepositories\TypedEntityRepositoryInterface
* The repository for the entity.
*
* @throws \Drupal\typed_entity\RepositoryNotFoundException
* When the repository was not found.
*
* @todo: The variant negotiation is still missing.
*/
public function repositoryFromEntity(EntityInterface $entity): TypedEntityRepositoryInterface {
$identifier = implode(
TypedEntityRepositoryBase::SEPARATOR,
array_filter([$entity->getEntityTypeId(), $entity->bundle()])
);
$repository = $this->collector->get($identifier);
if (empty($repository)) {
$message = 'Repository with identifier "' . $identifier . '" not found';
throw new RepositoryNotFoundException($message);
}
return $repository;
}
/**
* {@inheritdoc}
*/
public function wrap(EntityInterface $entity): WrappedEntityInterface {
return $this->repositoryFromEntity($entity)->wrap($entity);
}
/**
* {@inheritdoc}
*/
public function wrapMultiple(array $entities): array {
return array_map([$this, 'wrap'], $entities);
}
}
......@@ -3,7 +3,7 @@
namespace Drupal\typed_entity\WrappedEntities;
use Drupal\Core\Entity\EntityInterface;
use Drupal\typed_entity\RepositoryCollector;
use Drupal\typed_entity\RepositoryManager;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
......@@ -61,9 +61,9 @@ abstract class WrappedEntityBase implements WrappedEntityInterface {
if (!$owner instanceof EntityInterface) {
return NULL;
}
$collector = \Drupal::service(RepositoryCollector::class);
assert($collector instanceof RepositoryCollector);
return $collector->wrap($owner);
$manager = \Drupal::service(RepositoryManager::class);
assert($manager instanceof RepositoryManager);
return $manager->wrap($owner);
}
}
services:
Drupal\typed_entity\RepositoryManager:
arguments: ['@Drupal\typed_entity\RepositoryCollector']
Drupal\typed_entity\RepositoryCollector:
arguments: ['@entity_type.manager']
tags:
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment