TransactionService.php in Transaction 8
File
src/TransactionService.php
View source
<?php
namespace Drupal\transaction;
use Drupal\Core\Entity\EntityTypeManagerInterface;
class TransactionService implements TransactionServiceInterface {
protected $entityTypeManager;
public function __construct(EntityTypeManagerInterface $entity_type_manager) {
$this->entityTypeManager = $entity_type_manager;
}
public function getLastExecutedTransaction($target_entity, $transaction_type) {
$target_entity_id = is_object($target_entity) ? $target_entity
->id() : $target_entity;
if (!is_object($transaction_type) && !($transaction_type = $this->entityTypeManager
->getStorage('transaction_type')
->load($transaction_type))) {
throw new \InvalidArgumentException('Invalid transaction type.');
}
$transaction_type_id = $transaction_type
->id();
$target_entity_type_id = $transaction_type
->getTargetEntityTypeId();
$storage = $this->entityTypeManager
->getStorage('transaction');
$result = $storage
->getQuery()
->condition('type', $transaction_type_id)
->condition('target_entity.target_type', $target_entity_type_id)
->condition('target_entity.target_id', $target_entity_id)
->exists('executed')
->range(0, 1)
->sort('execution_sequence', 'DESC')
->sort('executed', 'DESC')
->execute();
return count($result) ? $storage
->load(array_pop($result)) : NULL;
}
}