Skip to content
Snippets Groups Projects
Commit 010514d0 authored by Tim Rohaly's avatar Tim Rohaly
Browse files

Issue #3511838 by tr: Use injected entity query in storage classes

parent 76bdf935
No related branches found
No related tags found
1 merge request!54Resolve #3511838 "Use injected entity"
Pipeline #505708 passed with warnings
......@@ -4,9 +4,8 @@ parameters:
ignoreErrors:
-
message: '#\Drupal calls should be avoided in classes, use dependency injection instead#'
paths:
- src/VoteResultStorage.php
- src/VoteStorage.php
path: src/VoteStorage.php
count: 2
# new static() is a best practice in Drupal, so we cannot fix that.
# @see https://www.drupal.org/docs/develop/development-tools/phpstan/handling-unsafe-usage-of-new-static
......
<?php
declare(strict_types=1);
namespace Drupal\votingapi;
use Drupal\Core\Entity\Sql\SqlContentEntityStorage;
use Drupal\votingapi\Entity\VoteResult;
/**
* Storage class for vote entities.
* Storage class for vote_result entities.
*
* This extends the \Drupal\entity\EntityDatabaseStorage class, adding
* required special handling for vote entities.
* required special handling for vote_result entities.
*/
class VoteResultStorage extends SqlContentEntityStorage implements VoteResultStorageInterface {
/**
* {@inheritdoc}
*/
public function getEntityResults($entity_type_id, $entity_id, $vote_type, $function) {
$query = \Drupal::entityQuery('vote_result')
public function getEntityResults(string $entity_type_id, string|int $entity_id, string $vote_type, string $function): array {
$query = $this->getQuery()
->condition('entity_type', $entity_type_id)
->condition('entity_id', $entity_id)
->condition('type', $vote_type);
......@@ -27,7 +28,7 @@ class VoteResultStorage extends SqlContentEntityStorage implements VoteResultSto
$query->sort('type');
$query->accessCheck(TRUE);
$vote_ids = $query->execute();
return VoteResult::loadMultiple($vote_ids);
return $this->loadMultiple($vote_ids);
}
}
......@@ -18,7 +18,7 @@ interface VoteResultStorageInterface extends EntityStorageInterface {
*
* @param string $entity_type_id
* The entity type id.
* @param int $entity_id
* @param string|int $entity_id
* The node id of the node for which number of votes is requested.
* @param string $vote_type
* Plugin implementing \Drupal\votingapi\Plugin\VoteType.
......@@ -28,6 +28,6 @@ interface VoteResultStorageInterface extends EntityStorageInterface {
* @return \Drupal\votingapi\Entity\VoteResult[]
* The number of votes for the entity.
*/
public function getEntityResults($entity_type_id, $entity_id, $vote_type, $function);
public function getEntityResults(string $entity_type_id, string|int $entity_id, string $vote_type, string $function): array;
}
......@@ -15,8 +15,8 @@ class VoteStorage extends SqlContentEntityStorage implements VoteStorageInterfac
/**
* {@inheritdoc}
*/
public function getUserVotes($uid, $vote_type_id = NULL, $entity_type_id = NULL, $entity_id = NULL, $vote_source = NULL) {
$query = \Drupal::entityQuery('vote')
public function getUserVotes(int $uid, ?string $vote_type_id = NULL, ?string $entity_type_id = NULL, string|int|null $entity_id = NULL, ?string $vote_source = NULL): array {
$query = $this->getQuery()
->accessCheck(TRUE)
->condition('user_id', $uid);
if ($vote_type_id) {
......@@ -37,7 +37,7 @@ class VoteStorage extends SqlContentEntityStorage implements VoteStorageInterfac
/**
* {@inheritdoc}
*/
public function deleteUserVotes($uid, $vote_type_id = NULL, $entity_type_id = NULL, $entity_id = NULL, $vote_source = NULL): void {
public function deleteUserVotes(int $uid, ?string $vote_type_id = NULL, ?string $entity_type_id = NULL, string|int|null $entity_id = NULL, ?string $vote_source = NULL): void {
$votes = $this->getUserVotes($uid, $vote_type_id, $entity_type_id, $entity_id, $vote_source);
if (!empty($votes)) {
$entities = $this->loadMultiple($votes);
......@@ -61,9 +61,9 @@ class VoteStorage extends SqlContentEntityStorage implements VoteStorageInterfac
/**
* {@inheritdoc}
*/
public function getVotesSinceMoment() {
public function getVotesSinceMoment(): array {
$last_cron = \Drupal::state()->get('votingapi.last_cron', 0);
return \Drupal::entityQueryAggregate('vote')
return $this->getAggregateQuery()
->condition('timestamp', $last_cron, '>')
->groupBy('entity_type')
->groupBy('entity_id')
......@@ -75,8 +75,8 @@ class VoteStorage extends SqlContentEntityStorage implements VoteStorageInterfac
/**
* {@inheritdoc}
*/
public function deleteVotesForDeletedEntity($entity_type_id, $entity_id): void {
$votes = \Drupal::entityQuery('vote')
public function deleteVotesForDeletedEntity(string $entity_type_id, string|int $entity_id): void {
$votes = $this->getQuery()
->accessCheck(TRUE)
->condition('entity_type', $entity_type_id)
->condition('entity_id', $entity_id)
......
......@@ -14,40 +14,40 @@ interface VoteStorageInterface extends EntityStorageInterface {
*
* @param int $uid
* User ID.
* @param string $vote_type_id
* The vote type ID.
* @param string $entity_type_id
* The entity type ID.
* @param int $entity_id
* The entity ID.
* @param string $vote_source
* The vote source, only used if $uid == 0.
* @param ?string $vote_type_id
* (optional) The vote type ID.
* @param ?string $entity_type_id
* (optional) The voted entity type ID.
* @param string|int|null $entity_id
* (optional) The voted entity ID.
* @param ?string $vote_source
* (optional) The vote source, only used if $uid == 0.
*
* @return mixed
* @return \Drupal\votingapi\VoteInterface[]
* Returns the user votes.
*/
public function getUserVotes($uid, $vote_type_id = NULL, $entity_type_id = NULL, $entity_id = NULL, $vote_source = NULL);
public function getUserVotes(int $uid, ?string $vote_type_id = NULL, ?string $entity_type_id = NULL, string|int|null $entity_id = NULL, ?string $vote_source = NULL): array;
/**
* Deletes votes for a user.
*
* @param int $uid
* The User ID.
* @param string $vote_type_id
* The vote type ID.
* @param string $entity_type_id
* The entity type ID.
* @param int $entity_id
* The entity ID.
* @param string $vote_source
* The vote source, only used if $uid == 0.
* @param ?string $vote_type_id
* (optional) The vote type ID.
* @param ?string $entity_type_id
* (optional) The voted entity type ID.
* @param string|int|null $entity_id
* (optional) The voted entity ID.
* @param ?string $vote_source
* (optional) The vote source, only used if $uid == 0.
*/
public function deleteUserVotes($uid, $vote_type_id = NULL, $entity_type_id = NULL, $entity_id = NULL, $vote_source = NULL): void;
public function deleteUserVotes(int $uid, ?string $vote_type_id = NULL, ?string $entity_type_id = NULL, string|int|null $entity_id = NULL, ?string $vote_source = NULL): void;
/**
* Returns the default vote source.
*
* @param string $vote_source
* @param ?string $vote_source
* (optional) The vote source.
*
* @return string
......@@ -58,19 +58,19 @@ interface VoteStorageInterface extends EntityStorageInterface {
/**
* Gets votes since a determined moment.
*
* @return mixed
* Returns the votes since last cron run.
* @return \Drupal\votingapi\VoteInterface[]
* Returns array of votes since last cron run.
*/
public function getVotesSinceMoment();
public function getVotesSinceMoment(): array;
/**
* Deletes votes for deleted entity everywhere in the database.
*
* @param string $entity_type_id
* The entity type ID.
* @param int $entity_id
* The entity ID.
* The voted entity type ID.
* @param string|int $entity_id
* The voted entity ID.
*/
public function deleteVotesForDeletedEntity($entity_type_id, $entity_id): void;
public function deleteVotesForDeletedEntity(string $entity_type_id, string|int $entity_id): void;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment