Skip to content

Commit

Permalink
DDST-382: Handle errors while deleting orphaned embargo
Browse files Browse the repository at this point in the history
  • Loading branch information
Prashant-bd committed Jul 29, 2024
1 parent b13c6c9 commit 8d307c4
Showing 1 changed file with 15 additions and 40 deletions.
55 changes: 15 additions & 40 deletions src/Entity/Embargo.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
use Drupal\Core\TypedData\Exception\MissingDataException;
use Drupal\datetime\Plugin\Field\FieldType\DateTimeItemInterface;
use Drupal\embargo\EmbargoInterface;
use Drupal\embargo\EmbargoStorageInterface;
use Drupal\embargo\IpRangeInterface;
use Drupal\node\NodeInterface;
use Drupal\user\UserInterface;
Expand All @@ -33,7 +32,6 @@
* handlers = {
* "storage" = "Drupal\embargo\EmbargoStorage",
* "view_builder" = "Drupal\Core\Entity\EntityViewBuilder",
* "views_data" = "Drupal\embargo\EmbargoViewsData",
* "list_builder" = "Drupal\embargo\EmbargoListBuilder",
* "form" = {
* "add" = "Drupal\embargo\Form\EmbargoForm",
Expand All @@ -44,6 +42,7 @@
* "html" = "Drupal\Core\Entity\Routing\AdminHtmlRouteProvider"
* },
* },
* list_cache_tags = { "node_list", "media_list", "file_list" },
* base_table = "embargo",
* admin_permission = "administer embargo",
* entity_keys = {
Expand Down Expand Up @@ -379,47 +378,35 @@ public function setEmbargoedNode(NodeInterface $node): EmbargoInterface {
}

/**
* {@inheritDoc}
* The maximum age for which this object may be cached.
*
* @return int
* The maximum time in seconds that this object may be cached.
*/
public function getCacheMaxAge() {
$max_age = parent::getCacheMaxAge();

$now = time();
// Invalidate cache after a scheduled embargo expires.
if ($this->getExpirationType() === static::EXPIRATION_TYPE_SCHEDULED && !$this->expiresBefore($now)) {
$max_age = Cache::mergeMaxAges($max_age, $this->getExpirationDate()->getTimestamp() - $now);
return $this->getExpirationDate()->getTimestamp() - $now;
}

return $max_age;
// Other properties of the embargo are not time dependent.
return parent::getCacheMaxAge();
}

/**
* {@inheritdoc}
*/
public function getCacheTags() {
$tags = Cache::mergeTags(parent::getCacheTags(), $this->getEmbargoedNode()->getCacheTags());
$tags = parent::getCacheTags();

if ($this->getExemptIps()) {
$tags = Cache::mergeTags($tags, $this->getExemptIps()->getCacheTags());
}
return $tags;
}

/**
* {@inheritDoc}
*/
public function getCacheContexts() {
$contexts = Cache::mergeContexts(
parent::getCacheContexts(),
$this->getEmbargoedNode()->getCacheContexts(),
['user.embargo__has_exemption'],
);

if ($this->getExemptIps()) {
$contexts = Cache::mergeContexts($contexts, $this->getExemptIps()->getCacheContexts());
if ($this->getEmbargoedNode() !== NULL) {
$tags[] = "node:{$this->getEmbargoedNode()->id()}";
if ($this->getExemptIps()) {
$tags = Cache::mergeTags($tags, $this->getExemptIps()->getCacheTags());
}
}

return $contexts;
return $tags;
}

/**
Expand Down Expand Up @@ -451,16 +438,4 @@ public function ipIsExempt(string $ip): bool {
return $exempt_ips && $exempt_ips->withinRanges($ip);
}

/**
* {@inheritdoc}
*/
protected function getListCacheTagsToInvalidate() : array {
return array_merge(
parent::getListCacheTagsToInvalidate(),
array_map(function (string $type) {
return "{$type}_list";
}, EmbargoStorageInterface::APPLICABLE_ENTITY_TYPES),
);
}

}

0 comments on commit 8d307c4

Please sign in to comment.