Skip to content

Commit

Permalink
Merge pull request #11 from discoverygarden/feature/reusability
Browse files Browse the repository at this point in the history
ARQT-16: Feature/reusability
  • Loading branch information
bibliophileaxe authored Feb 6, 2023
2 parents 54a2ac4 + 2ce0a2d commit 1527905
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 41 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/auto_test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ jobs:
composer config minimum-stability dev
composer require ${{ env.COMPOSER_PACKAGE_PREREQUISITES }} \
"${{ env.COMPOSER_PACKAGE_NAME }}:dev-${{ github.sha }}"
"${{ env.COMPOSER_PACKAGE_NAME }}:dev-main#${{ github.sha }}"
# Install the module.
drush --uri=http://localhost --yes -v en ${{ env.DRUPAL_EXTENSION_NAME }}
Expand Down
65 changes: 65 additions & 0 deletions src/Access/QueryConjunctionTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php

namespace Drupal\islandora_hierarchical_access\Access;

use Drupal\Core\Database\Query\SelectInterface;

/**
* Helper to ensure queries perform base op is con- instead of dis-joint.
*/
trait QueryConjunctionTrait {

/**
* Ensure the given query represents an "AND" to which we can attach filters.
*
* Queries can select either "OR" or "AND" as their base operator when they
* are created; however, constraining results is much easier with "AND"... so
* let's rework the query object to make it so.
*
* @param \Drupal\Core\Database\Query\SelectInterface $query
* The query to be tagged.
*
* @return \Drupal\Core\Database\Query\SelectInterface
* The query which has been dealt with... should be the same query, just
* returning for (potential) convenience.
*/
protected static function conjunctionQuery(SelectInterface $query): SelectInterface {
$original_conditions =& $query->conditions();
if ($original_conditions['#conjunction'] === 'AND') {
// Nothing to do...
return $query;
}

$new_or = $query->orConditionGroup();

$original_conditions_copy = $original_conditions;
unset($original_conditions_copy['#conjunction']);
foreach ($original_conditions_copy as $orig_cond) {
$new_or->condition($orig_cond['field'], $orig_cond['value'] ?? NULL,
$orig_cond['operator'] ?? '=');
}

$new_and = $query->andConditionGroup()
->condition($new_or);

$original_conditions = $new_and->conditions();

return $query;
}

/**
* Deprecated method name.
*
* @pbpcs:ignore Drupal.Commenting.DocComment.SpacingBeforeTags - Additional tags cause whatever sniffs to break.
* @phpcs:disable Drupal.Commenting.Deprecated.DeprecatedWrongSeeUrlFormat
* We are not a project on d.o, so there's no applicable URL.
* @deprecated in project:1.2.0 and is removed from project:2.0.0. Deprecated in
* favor of the static and better-named ::conjunctionQuery() method.
* @see \Drupal\islandora_hierarchical_access\Access\QueryConjunctionTrait::conjunctionQuery()
* @phpcs:enable Drupal.Commenting.Deprecated.DeprecatedWrongSeeUrlFormat
*/
protected function andifyQuery(SelectInterface $query) : SelectInterface {
return static::conjunctionQuery($query);
}

}
44 changes: 4 additions & 40 deletions src/Access/QueryTagger.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
*/
class QueryTagger {

use QueryConjunctionTrait;

/**
* The database connection service.
*
Expand Down Expand Up @@ -94,7 +96,7 @@ public function tagFile(SelectInterface $query) : void {
return;
}

$this->andifyQuery($query);
static::conjunctionQuery($query);

$file_tables = $this->entityTypeManager->getStorage('file')
->getTableMapping()
Expand Down Expand Up @@ -181,44 +183,6 @@ protected function getTaggedMediaQuery(): SelectInterface {
return $this->taggedMediaQuery;
}

/**
* Ensure the given query represents an "AND" to which we can attach filters.
*
* Queries can select either "OR" or "AND" as their base conjunction when they
* are created; however, constraining results is much easier with "AND"... so
* let's rework the query object to make it so.
*
* @param \Drupal\Core\Database\Query\SelectInterface $query
* The query to be tagged.
*
* @return \Drupal\Core\Database\Query\SelectInterface
* The query which has been dealt with... should be the same query, just
* returning for (potential) convenience.
*/
protected function andifyQuery(SelectInterface $query): SelectInterface {
$original_conditions =& $query->conditions();
if ($original_conditions['#conjunction'] === 'AND') {
// Nothing to do...
return $query;
}

$new_or = $query->orConditionGroup();

$original_conditions_copy = $original_conditions;
unset($original_conditions_copy['#conjunction']);
foreach ($original_conditions_copy as $orig_cond) {
$new_or->condition($orig_cond['field'], $orig_cond['value'] ?? NULL,
$orig_cond['operator'] ?? '=');
}

$new_and = $query->andConditionGroup()
->condition($new_or);

$original_conditions = $new_and->conditions();

return $query;
}

/**
* Tag media_access queries.
*
Expand All @@ -231,7 +195,7 @@ public function tagMedia(SelectInterface $query) : void {
return;
}

$this->andifyQuery($query);
static::conjunctionQuery($query);

$media_tables = $this->entityTypeManager->getStorage('media')
->getTableMapping()
Expand Down

0 comments on commit 1527905

Please sign in to comment.