RepositoryManager.php 2.64 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
<?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 {
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
    return $this->repository($entity->getEntityTypeId(), $entity->bundle());
  }

  /**
   * 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 repository(string $entity_type_id, string $bundle = ''): TypedEntityRepositoryInterface {
    $bundle = $bundle ?: $entity_type_id;
63
64
    $identifier = implode(
      TypedEntityRepositoryBase::SEPARATOR,
65
      array_filter([$entity_type_id, $bundle])
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
    );
    $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);
  }

}