-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from remi-san/dbal-fix
Add a Tx Manager for doctrine DBAL
- Loading branch information
Showing
1 changed file
with
64 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
<?php | ||
|
||
namespace RemiSan\TransactionManager\Doctrine; | ||
|
||
use Doctrine\DBAL\Connection; | ||
use Doctrine\DBAL\ConnectionException; | ||
use RemiSan\TransactionManager\Exception\BeginException; | ||
use RemiSan\TransactionManager\Exception\CommitException; | ||
use RemiSan\TransactionManager\Exception\RollbackException; | ||
use RemiSan\TransactionManager\Transactional; | ||
|
||
final class DoctrineDbalConnectionTransactionManager implements Transactional | ||
{ | ||
/** | ||
* @var Connection | ||
*/ | ||
private $dbalConnection; | ||
|
||
/** | ||
* Constructor. | ||
* | ||
* @param Connection $dbalConnection | ||
*/ | ||
public function __construct(Connection $dbalConnection) | ||
{ | ||
$this->dbalConnection = $dbalConnection; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function beginTransaction() | ||
{ | ||
try { | ||
$this->dbalConnection->beginTransaction(); | ||
} catch (\Exception $e) { | ||
throw new BeginException('Cannot begin Doctrine DBAL transaction'); | ||
} | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function commit() | ||
{ | ||
try { | ||
$this->dbalConnection->commit(); | ||
} catch (ConnectionException $e) { | ||
throw new CommitException('Cannot commit Doctrine DBAL transaction'); | ||
} | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function rollback() | ||
{ | ||
try { | ||
$this->dbalConnection->commit(); | ||
} catch (ConnectionException $e) { | ||
throw new RollbackException('Cannot rollback Doctrine DBAL transaction'); | ||
} | ||
} | ||
} |