You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
30 lines
680 B
30 lines
680 B
7 years ago
|
<?php
|
||
|
|
||
|
namespace core\services;
|
||
|
|
||
|
use core\dispatchers\DeferredEventDispatcher;
|
||
|
|
||
|
class TransactionManager
|
||
|
{
|
||
|
private $dispatcher;
|
||
|
|
||
|
public function __construct(DeferredEventDispatcher $dispatcher)
|
||
|
{
|
||
|
$this->dispatcher = $dispatcher;
|
||
|
}
|
||
|
|
||
|
public function wrap(callable $function): void
|
||
|
{
|
||
|
$transaction = \Yii::$app->db->beginTransaction();
|
||
|
try {
|
||
|
$this->dispatcher->defer();
|
||
|
$function();
|
||
|
$transaction->commit();
|
||
|
$this->dispatcher->release();
|
||
|
} catch (\Exception $e) {
|
||
|
$transaction->rollBack();
|
||
|
$this->dispatcher->clean();
|
||
|
throw $e;
|
||
|
}
|
||
|
}
|
||
|
}
|