/**
* @param object $command
* @param callable $next
*
* @return mixed
* @throws \Exception when Transaction fails to commit.
*/
public function execute($command, callable $next)
{
$this->connection->begin();
try {
$returnValue = $next($command);
$isCommitted = $this->connection->commit();
} catch (\Exception $e) {
$this->connection->rollback();
throw $e;
}
if (!$isCommitted) {
throw new \Exception('Failed to commit the transaction.');
}
return $returnValue;
}