Mercurial > hg > cmmr2012-drupal-site
comparison core/modules/jsonapi/src/Revisions/NegotiatorBase.php @ 5:12f9dff5fda9 tip
Update to Drupal core 8.7.1
author | Chris Cannam |
---|---|
date | Thu, 09 May 2019 15:34:47 +0100 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
4:a9cd425dd02b | 5:12f9dff5fda9 |
---|---|
1 <?php | |
2 | |
3 namespace Drupal\jsonapi\Revisions; | |
4 | |
5 use Drupal\Core\Entity\EntityInterface; | |
6 use Drupal\Core\Entity\EntityTypeManagerInterface; | |
7 | |
8 /** | |
9 * Base implementation for version negotiators. | |
10 * | |
11 * @internal JSON:API maintains no PHP API since its API is the HTTP API. This | |
12 * class may change at any time and this will break any dependencies on it. | |
13 * | |
14 * @see https://www.drupal.org/project/jsonapi/issues/3032787 | |
15 * @see jsonapi.api.php | |
16 */ | |
17 abstract class NegotiatorBase implements VersionNegotiatorInterface { | |
18 | |
19 /** | |
20 * The entity type manager to load the revision. | |
21 * | |
22 * @var \Drupal\Core\Entity\EntityTypeManagerInterface | |
23 */ | |
24 protected $entityTypeManager; | |
25 | |
26 /** | |
27 * Constructs a version negotiator instance. | |
28 * | |
29 * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager | |
30 * The entity type manager. | |
31 */ | |
32 public function __construct(EntityTypeManagerInterface $entity_type_manager) { | |
33 $this->entityTypeManager = $entity_type_manager; | |
34 } | |
35 | |
36 /** | |
37 * Gets the revision ID. | |
38 * | |
39 * @param \Drupal\Core\Entity\EntityInterface $entity | |
40 * The entity. | |
41 * @param string $version_argument | |
42 * A value used to derive a revision ID for the given entity. | |
43 * | |
44 * @return int | |
45 * The revision ID. | |
46 * | |
47 * @throws \Drupal\jsonapi\Revisions\VersionNotFoundException | |
48 * When the revision does not exist. | |
49 * @throws \Drupal\jsonapi\Revisions\InvalidVersionIdentifierException | |
50 * When the revision ID is not valid. | |
51 */ | |
52 abstract protected function getRevisionId(EntityInterface $entity, $version_argument); | |
53 | |
54 /** | |
55 * {@inheritdoc} | |
56 */ | |
57 public function getRevision(EntityInterface $entity, $version_argument) { | |
58 return $this->loadRevision($entity, $this->getRevisionId($entity, $version_argument)); | |
59 } | |
60 | |
61 /** | |
62 * Loads an entity revision. | |
63 * | |
64 * @param \Drupal\Core\Entity\EntityInterface $entity | |
65 * The entity for which to load a revision. | |
66 * @param int $revision_id | |
67 * The revision ID to be loaded. | |
68 * | |
69 * @return \Drupal\Core\Entity\EntityInterface|null | |
70 * The revision or NULL if the revision does not exists. | |
71 * | |
72 * @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException | |
73 * Thrown if the entity type doesn't exist. | |
74 * @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException | |
75 * Thrown if the storage handler couldn't be loaded. | |
76 */ | |
77 protected function loadRevision(EntityInterface $entity, $revision_id) { | |
78 $storage = $this->entityTypeManager->getStorage($entity->getEntityTypeId()); | |
79 $revision = static::ensureVersionExists($storage->loadRevision($revision_id)); | |
80 if ($revision->id() !== $entity->id()) { | |
81 throw new VersionNotFoundException(sprintf('The requested resource does not have a version with ID %s.', $revision_id)); | |
82 } | |
83 return $revision; | |
84 } | |
85 | |
86 /** | |
87 * Helper method that ensures that a version exists. | |
88 * | |
89 * @param int|\Drupal\Core\Entity\EntityInterface $revision | |
90 * A revision ID, or NULL if one was not found. | |
91 * | |
92 * @return int|\Drupal\Core\Entity\EntityInterface | |
93 * A revision or revision ID, if one was found. | |
94 * | |
95 * @throws \Drupal\jsonapi\Revisions\VersionNotFoundException | |
96 * Thrown if the given value is NULL, meaning the requested version was not | |
97 * found. | |
98 */ | |
99 protected static function ensureVersionExists($revision) { | |
100 if (is_null($revision)) { | |
101 throw new VersionNotFoundException(); | |
102 } | |
103 return $revision; | |
104 } | |
105 | |
106 } |