/**
  * @param  string                     $targetEntityClassName
  * @param  int                        $accessLevel
  * @param  OwnershipMetadataInterface $metadata
  *
  * @return null|array
  *
  * The cyclomatic complexity warning is suppressed by performance reasons
  * (to avoid unnecessary cloning od arrays)
  * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  */
 protected function buildConstraintIfAccessIsGranted($targetEntityClassName, $accessLevel, OwnershipMetadataInterface $metadata)
 {
     $tree = $this->getTree();
     $constraint = null;
     if (AccessLevel::SYSTEM_LEVEL === $accessLevel) {
         $constraint = [];
     } elseif (!$metadata->hasOwner()) {
         if (AccessLevel::GLOBAL_LEVEL === $accessLevel) {
             if ($this->metadataProvider->getGlobalLevelClass() === $targetEntityClassName) {
                 $orgIds = $tree->getUserOrganizationIds($this->getUserId());
                 $constraint = $this->getCondition($orgIds, $metadata, 'id');
             } else {
                 $constraint = [];
             }
         } else {
             $constraint = [];
         }
     } else {
         if (AccessLevel::BASIC_LEVEL === $accessLevel) {
             if ($this->metadataProvider->getBasicLevelClass() === $targetEntityClassName) {
                 $constraint = $this->getCondition($this->getUserId(), $metadata, 'id');
             } elseif ($metadata->isBasicLevelOwned()) {
                 $constraint = $this->getCondition($this->getUserId(), $metadata);
             }
         } elseif (AccessLevel::LOCAL_LEVEL === $accessLevel) {
             if ($this->metadataProvider->getLocalLevelClass() === $targetEntityClassName) {
                 $buIds = $tree->getUserBusinessUnitIds($this->getUserId(), $this->getOrganizationId());
                 $constraint = $this->getCondition($buIds, $metadata, 'id');
             } elseif ($metadata->isLocalLevelOwned()) {
                 $buIds = $tree->getUserBusinessUnitIds($this->getUserId(), $this->getOrganizationId());
                 $constraint = $this->getCondition($buIds, $metadata);
             } elseif ($metadata->isBasicLevelOwned()) {
                 $userIds = [];
                 $this->fillBusinessUnitUserIds($this->getUserId(), $this->getOrganizationId(), $userIds);
                 $constraint = $this->getCondition($userIds, $metadata);
             }
         } elseif (AccessLevel::DEEP_LEVEL === $accessLevel) {
             if ($this->metadataProvider->getLocalLevelClass() === $targetEntityClassName) {
                 $buIds = [];
                 $this->fillSubordinateBusinessUnitIds($this->getUserId(), $this->getOrganizationId(), $buIds);
                 $constraint = $this->getCondition($buIds, $metadata, 'id');
             } elseif ($metadata->isLocalLevelOwned()) {
                 $buIds = [];
                 $this->fillSubordinateBusinessUnitIds($this->getUserId(), $this->getOrganizationId(), $buIds);
                 $constraint = $this->getCondition($buIds, $metadata);
             } elseif ($metadata->isBasicLevelOwned()) {
                 $userIds = [];
                 $this->fillSubordinateBusinessUnitUserIds($this->getUserId(), $this->getOrganizationId(), $userIds);
                 $constraint = $this->getCondition($userIds, $metadata);
             }
         } elseif (AccessLevel::GLOBAL_LEVEL === $accessLevel) {
             if ($metadata->isGlobalLevelOwned()) {
                 $constraint = $this->getCondition([$this->getOrganizationId()], $metadata, null, true);
             } else {
                 $constraint = $this->getCondition(null, $metadata, null, true);
             }
         }
     }
     return $constraint;
 }