ConsentUserResolverPluginManager.php in General Data Protection Regulation 3.0.x        
                          
                  
                        
  
  
  
  
File
  modules/gdpr_consent/src/ConsentUserResolver/ConsentUserResolverPluginManager.php
  
    View source  
  <?php
namespace Drupal\gdpr_consent\ConsentUserResolver;
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Plugin\DefaultPluginManager;
use function array_filter;
use function count;
use function reset;
class ConsentUserResolverPluginManager extends DefaultPluginManager {
  
  protected $resolvers;
  
  public function __construct(\Traversable $namespaces, CacheBackendInterface $cacheBackend, ModuleHandlerInterface $moduleHandler) {
    parent::__construct('Plugin/Gdpr/ConsentUserResolver', $namespaces, $moduleHandler, GdprConsentUserResolverInterface::class, GdprConsentUserResolver::class);
    $this
      ->setCacheBackend($cacheBackend, 'gdpr_consent_resolver_plugins');
    $this
      ->alterInfo('gdpr_consent_resolver_info');
  }
  
  public function getDefinitionForType($entityType, $bundle) {
    $definitions = $this
      ->getDefinitions();
    
    $definitionsForEntity = array_filter($definitions, static function ($definition) use ($entityType) {
      return $definition['entityType'] === $entityType;
    });
    $definitionsForBundle = array_filter($definitionsForEntity, static function ($definition) use ($bundle) {
      return array_key_exists('bundle', $definition) && $definition['bundle'] === $bundle;
    });
    $definition = NULL;
    if (count($definitionsForBundle) > 0) {
      
      $definition = reset($definitionsForBundle);
    }
    elseif (count($definitionsForEntity) > 0) {
      
      $definitionsForBundle = array_filter($definitionsForEntity, static function ($definition) {
        return !array_key_exists('bundle', $definition) || $definition['bundle'] === '';
      });
      if (count($definitionsForBundle) > 0) {
        
        $definition = reset($definitionsForBundle);
      }
    }
    if (NULL === $definition) {
      return FALSE;
    }
    return $definition;
  }
  
  public function getForEntityType($entityType, $bundle) {
    $definition = $this
      ->getDefinitionForType($entityType, $bundle);
    if (FALSE === $definition) {
      throw new \Exception("Could not determine user ID for entity type {$entityType}. Please ensure there is a resolver registered.");
    }
    return $this
      ->createInstance($definition['id']);
  }
}