vendor/sonata-project/doctrine-extensions/src/Entity/BaseEntityManager.php line 65

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. /*
  4. * This file is part of the Sonata Project package.
  5. *
  6. * (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
  7. *
  8. * For the full copyright and license information, please view the LICENSE
  9. * file that was distributed with this source code.
  10. */
  11. namespace Sonata\Doctrine\Entity;
  12. use Doctrine\ORM\EntityManagerInterface;
  13. use Doctrine\ORM\EntityRepository;
  14. use Doctrine\Persistence\ObjectManager;
  15. use Doctrine\Persistence\ObjectRepository;
  16. use Sonata\Doctrine\Exception\TransactionException;
  17. use Sonata\Doctrine\Model\BaseManager;
  18. use Sonata\Doctrine\Model\TransactionalManagerInterface;
  19. /**
  20. * @author Sylvain Deloux <sylvain.deloux@ekino.com>
  21. *
  22. * @phpstan-template T of object
  23. * @phpstan-extends BaseManager<T>
  24. */
  25. abstract class BaseEntityManager extends BaseManager implements TransactionalManagerInterface
  26. {
  27. /**
  28. * @return EntityManagerInterface
  29. */
  30. public function getEntityManager(): ObjectManager
  31. {
  32. $objectManager = $this->getObjectManager();
  33. \assert($objectManager instanceof EntityManagerInterface);
  34. return $objectManager;
  35. }
  36. public function beginTransaction(): void
  37. {
  38. $this->getEntityManager()->beginTransaction();
  39. }
  40. public function commit(): void
  41. {
  42. try {
  43. $this->getEntityManager()->commit();
  44. } catch (\Throwable $exception) {
  45. throw new TransactionException($exception->getMessage(), (int) $exception->getCode(), $exception);
  46. }
  47. }
  48. public function rollBack(): void
  49. {
  50. $this->getEntityManager()->rollback();
  51. }
  52. /**
  53. * @phpstan-return EntityRepository<T>
  54. */
  55. protected function getRepository(): ObjectRepository
  56. {
  57. return $this->getEntityManager()->getRepository($this->class);
  58. }
  59. }