Skip to content

Commit

Permalink
Merge dc68794 into d06fc26
Browse files Browse the repository at this point in the history
  • Loading branch information
emonkak committed Dec 26, 2019
2 parents d06fc26 + dc68794 commit c3a1d56
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 14 deletions.
30 changes: 16 additions & 14 deletions src/NestedTransaction.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,33 +15,35 @@ class NestedTransaction implements PDOTransactionInterface
private $savepoint;

/**
* @var integer
* @var NestedTransactionState
*/
private $level = 0;
private $state;

/**
* @param PDOInterface $pdo
* @param SavepointInterface $savepoint
* @param ?NestedTransactionState $state
*/
public function __construct(PDOInterface $pdo, SavepointInterface $savepoint)
public function __construct(PDOInterface $pdo, SavepointInterface $savepoint, NestedTransactionState $state = null)
{
$this->pdo = $pdo;
$this->savepoint = $savepoint;
$this->state = $state ?: new NestedTransactionState();
}

/**
* {@inheritDoc}
*/
public function beginTransaction()
{
if ($this->level > 0) {
if ($this->state->getLevel() > 0) {
$this->savepoint->create($this->pdo, $this->getSavepoint());
$result = true;
} else {
$result = $this->pdo->beginTransaction();
}

$this->level++;
$this->state->incrementLevel();

return $result;
}
Expand All @@ -51,11 +53,11 @@ public function beginTransaction()
*/
public function commit()
{
if ($this->level > 0) {
$this->level--;
if ($this->state->getLevel() > 0) {
$this->state->decrementLevel();
}

if ($this->level > 0) {
if ($this->state->getLevel() > 0) {
$this->savepoint->release($this->pdo, $this->getSavepoint());
return true;
} else {
Expand All @@ -68,19 +70,19 @@ public function commit()
*/
public function inTransaction()
{
return $this->level > 0;
return $this->state->getLevel() > 0;
}

/**
* {@inheritDoc}
*/
public function rollback()
{
if ($this->level > 0) {
$this->level--;
if ($this->state->getLevel() > 0) {
$this->state->decrementLevel();
}

if ($this->level > 0) {
if ($this->state->getLevel() > 0) {
$this->savepoint->rollbackTo($this->pdo, $this->getSavepoint());
return true;
} else {
Expand All @@ -93,14 +95,14 @@ public function rollback()
*/
public function getTransactionLevel()
{
return $this->level;
return $this->state->getLevel();
}

/**
* @return string
*/
private function getSavepoint()
{
return 'level_' . $this->level;
return 'level_' . $this->state->getLevel();
}
}
43 changes: 43 additions & 0 deletions src/NestedTransactionState.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace Emonkak\Database;

class NestedTransactionState
{
/**
* @var int
*/
private $level;

/**
* @param int $level
*/
public function __construct($level = 0)
{
$this->level = $level;
}

/**
* @return int
*/
public function getLevel()
{
return $this->level;
}

/**
* @return void
*/
public function incrementLevel()
{
$this->level++;
}

/**
* @return void
*/
public function decrementLevel()
{
$this->level--;
}
}
1 change: 1 addition & 0 deletions tests/NestedTransactionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

/**
* @covers Emonkak\Database\NestedTransaction
* @covers Emonkak\Database\NestedTransactionState
*/
class NestedTransactionTest extends \PHPUnit_Framework_TestCase
{
Expand Down

0 comments on commit c3a1d56

Please sign in to comment.