-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- added interface `TransactionTrackerInterface` and implementor `TransactionTracker` - added method `RuntimeException::noActiveTransaction()` - transactions are automatically tracked byl the Tracker
- Loading branch information
Showing
7 changed files
with
136 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace SixtyEightPublishers\DoctrinePersistence; | ||
|
||
use ArrayIterator; | ||
use SixtyEightPublishers\DoctrinePersistence\Exception\RuntimeException; | ||
use SixtyEightPublishers\DoctrinePersistence\Exception\InvalidArgumentException; | ||
|
||
final class TransactionTracker implements TransactionTrackerInterface | ||
{ | ||
/** @var \SixtyEightPublishers\DoctrinePersistence\TransactionInterface[] */ | ||
private $transactions = []; | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function getIterator(): ArrayIterator | ||
{ | ||
return new ArrayIterator($this->transactions); | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function count(): int | ||
{ | ||
return count($this->transactions); | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
* | ||
* @throws \SixtyEightPublishers\DoctrinePersistence\Exception\InvalidArgumentException | ||
*/ | ||
public function track(TransactionInterface $transaction): void | ||
{ | ||
$hash = spl_object_hash($transaction); | ||
|
||
if (isset($this->transactions[$hash])) { | ||
throw new InvalidArgumentException(sprintf( | ||
'Transaction with hash %s is already tracked.', | ||
$hash | ||
)); | ||
} | ||
|
||
$transaction->finally(function () use ($hash): void { | ||
unset($this->transactions[$hash]); | ||
}); | ||
|
||
$this->transactions[$hash] = $transaction; | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function hasActiveTransaction(): bool | ||
{ | ||
return !empty($this->transactions); | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
* | ||
* @throws \SixtyEightPublishers\DoctrinePersistence\Exception\RuntimeException | ||
*/ | ||
public function getCurrentTransaction(): TransactionInterface | ||
{ | ||
if (empty($this->transactions)) { | ||
throw RuntimeException::noActiveTransaction(); | ||
} | ||
|
||
return end($this->transactions); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace SixtyEightPublishers\DoctrinePersistence; | ||
|
||
use Countable; | ||
use IteratorAggregate; | ||
|
||
interface TransactionTrackerInterface extends IteratorAggregate, Countable | ||
{ | ||
/** | ||
* @internal | ||
* | ||
* @param \SixtyEightPublishers\DoctrinePersistence\TransactionInterface $transaction | ||
*/ | ||
public function track(TransactionInterface $transaction): void; | ||
|
||
/** | ||
* @return bool | ||
*/ | ||
public function hasActiveTransaction(): bool; | ||
|
||
/** | ||
* @return \SixtyEightPublishers\DoctrinePersistence\TransactionInterface | ||
*/ | ||
public function getCurrentTransaction(): TransactionInterface; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters