Skip to content

Commit

Permalink
Adding TransactionalManagerInterface
Browse files Browse the repository at this point in the history
  • Loading branch information
eerison committed Aug 11, 2022
1 parent 99e5a29 commit f77639b
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 1 deletion.
27 changes: 26 additions & 1 deletion src/Entity/BaseEntityManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,18 @@

use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\EntityRepository;
use Sonata\Doctrine\Exception\TransactionException;
use Sonata\Doctrine\Model\BaseManager;
use Sonata\Doctrine\Model\TransactionalManagerInterface;
use Throwable;

/**
* @author Sylvain Deloux <[email protected]>
*
* @phpstan-template T of object
* @phpstan-extends BaseManager<T>
*/
abstract class BaseEntityManager extends BaseManager
abstract class BaseEntityManager extends BaseManager implements TransactionalManagerInterface
{
/**
* Make sure the code is compatible with legacy code.
Expand Down Expand Up @@ -85,6 +88,28 @@ protected function getRepository(): EntityRepository
{
return $this->getEntityManager()->getRepository($this->class);
}

public function beginTransaction(): void
{
$this->getEntityManager()->beginTransaction();
}

/**
* @throws TransactionException
*/
public function commit(): void
{
try {
$this->getEntityManager()->commit();
} catch (Throwable $exception) {
throw new TransactionException($exception->getMessage(), $exception->getCode(), $exception);
}
}

public function rollBack(): void
{
$this->getEntityManager()->rollback();
}
}

class_exists(\Sonata\CoreBundle\Model\BaseEntityManager::class);
10 changes: 10 additions & 0 deletions src/Exception/TransactionException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace Sonata\Doctrine\Exception;

use Exception;

class TransactionException extends Exception
{

}
26 changes: 26 additions & 0 deletions src/Model/TransactionalManagerInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

declare(strict_types=1);

/*
* This file is part of the Sonata Project package.
*
* (c) Thomas Rabaix <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Sonata\Doctrine\Model;

/**
* @author Erison Silva <[email protected]>
*/
interface TransactionalManagerInterface
{
public function beginTransaction(): void;

public function commit(): void;

public function rollBack(): void;
}

0 comments on commit f77639b

Please sign in to comment.