You are here

class RefinableCalculatedGroupPermissions in Group 2.0.x

Same name and namespace in other branches
  1. 8 src/Access/RefinableCalculatedGroupPermissions.php \Drupal\group\Access\RefinableCalculatedGroupPermissions

Represents a calculated set of group permissions with cacheable metadata.

Hierarchy

Expanded class hierarchy of RefinableCalculatedGroupPermissions

See also

\Drupal\group\Access\ChainGroupPermissionCalculator

4 files declare their use of RefinableCalculatedGroupPermissions
ChainGroupPermissionCalculatorTest.php in tests/src/Kernel/ChainGroupPermissionCalculatorTest.php
GroupPermissionCheckerTest.php in tests/src/Unit/GroupPermissionCheckerTest.php
GroupPermissionHashGeneratorTest.php in tests/src/Unit/GroupPermissionHashGeneratorTest.php
RefinableCalculatedGroupPermissionsTest.php in tests/src/Unit/RefinableCalculatedGroupPermissionsTest.php

File

src/Access/RefinableCalculatedGroupPermissions.php, line 12

Namespace

Drupal\group\Access
View source
class RefinableCalculatedGroupPermissions implements RefinableCalculatedGroupPermissionsInterface {
  use CalculatedGroupPermissionsTrait;
  use RefinableCacheableDependencyTrait;

  /**
   * {@inheritdoc}
   */
  public function addItem(CalculatedGroupPermissionsItemInterface $item, $overwrite = FALSE) {
    if (!$overwrite && ($existing = $this
      ->getItem($item
      ->getScope(), $item
      ->getIdentifier()))) {
      $item = $this
        ->mergeItems($existing, $item);
    }
    $this->items[$item
      ->getScope()][$item
      ->getIdentifier()] = $item;
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function removeItem($scope, $identifier) {
    unset($this->items[$scope][$identifier]);
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function removeItems() {
    $this->items = [];
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function removeItemsByScope($scope) {
    unset($this->items[$scope]);
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function merge(CalculatedGroupPermissionsInterface $calculated_permissions) {
    foreach ($calculated_permissions
      ->getItems() as $item) {
      $this
        ->addItem($item);
    }
    $this
      ->addCacheableDependency($calculated_permissions);
    return $this;
  }

  /**
   * Merges two items of identical scope and identifier.
   *
   * @param \Drupal\group\Access\CalculatedGroupPermissionsItemInterface $a
   *   The first item to merge.
   * @param \Drupal\group\Access\CalculatedGroupPermissionsItemInterface $b
   *   The second item to merge.
   *
   * @return \Drupal\group\Access\CalculatedGroupPermissionsItemInterface
   *   A new item representing the merger of both items.
   *
   * @throws \LogicException
   *   Exception thrown when someone somehow manages to call this method with
   *   mismatching items.
   */
  protected function mergeItems(CalculatedGroupPermissionsItemInterface $a, CalculatedGroupPermissionsItemInterface $b) {
    if ($a
      ->getScope() != $b
      ->getScope()) {
      throw new \LogicException('Trying to merge two items of different scopes.');
    }
    if ($a
      ->getIdentifier() != $b
      ->getIdentifier()) {
      throw new \LogicException('Trying to merge two items with different identifiers.');
    }
    $permissions = array_merge($a
      ->getPermissions(), $b
      ->getPermissions());

    // @todo In Group 8.2.x merge isAdmin flags and pass to constructor.
    return new CalculatedGroupPermissionsItem($a
      ->getScope(), $a
      ->getIdentifier(), array_unique($permissions));
  }

}

Members

Namesort descending Modifiers Type Description Overrides
CacheableDependencyTrait::$cacheContexts protected property Cache contexts.
CacheableDependencyTrait::$cacheMaxAge protected property Cache max-age.
CacheableDependencyTrait::$cacheTags protected property Cache tags.
CacheableDependencyTrait::getCacheContexts public function 4
CacheableDependencyTrait::getCacheMaxAge public function 4
CacheableDependencyTrait::getCacheTags public function 4
CacheableDependencyTrait::setCacheability protected function Sets cacheability; useful for value object constructors.
CalculatedGroupPermissionsTrait::$items protected property A list of calculated group permission items, keyed by scope and identifier.
CalculatedGroupPermissionsTrait::getItem public function
CalculatedGroupPermissionsTrait::getItems public function
CalculatedGroupPermissionsTrait::getItemsByScope public function
RefinableCacheableDependencyTrait::addCacheableDependency public function 1
RefinableCacheableDependencyTrait::addCacheContexts public function
RefinableCacheableDependencyTrait::addCacheTags public function
RefinableCacheableDependencyTrait::mergeCacheMaxAge public function
RefinableCalculatedGroupPermissions::addItem public function Adds a calculated permission item. Overrides RefinableCalculatedGroupPermissionsInterface::addItem
RefinableCalculatedGroupPermissions::merge public function Merge another calculated group permissions object into this one. Overrides RefinableCalculatedGroupPermissionsInterface::merge
RefinableCalculatedGroupPermissions::mergeItems protected function Merges two items of identical scope and identifier.
RefinableCalculatedGroupPermissions::removeItem public function Removes a single calculated permission item from a given scope. Overrides RefinableCalculatedGroupPermissionsInterface::removeItem
RefinableCalculatedGroupPermissions::removeItems public function Removes all of the calculated permission items, regardless of scope. Overrides RefinableCalculatedGroupPermissionsInterface::removeItems
RefinableCalculatedGroupPermissions::removeItemsByScope public function Removes all of the calculated permission items for the given scope. Overrides RefinableCalculatedGroupPermissionsInterface::removeItemsByScope