vendor/sonata-project/doctrine-extensions/src/Model/BaseManager.php line 54

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\Model;
  12. use Doctrine\Persistence\ManagerRegistry;
  13. use Doctrine\Persistence\ObjectManager;
  14. use Doctrine\Persistence\ObjectRepository;
  15. /**
  16. * @author Hugo Briand <briand@ekino.com>
  17. *
  18. * @phpstan-template T of object
  19. * @phpstan-implements ManagerInterface<T>
  20. */
  21. abstract class BaseManager implements ManagerInterface, ClearableManagerInterface
  22. {
  23. /**
  24. * @phpstan-param class-string<T> $class
  25. */
  26. public function __construct(
  27. protected string $class,
  28. protected ManagerRegistry $registry,
  29. ) {
  30. }
  31. public function getClass(): string
  32. {
  33. return $this->class;
  34. }
  35. public function findAll(): array
  36. {
  37. return $this->getRepository()->findAll();
  38. }
  39. public function findBy(array $criteria, ?array $orderBy = null, ?int $limit = null, ?int $offset = null): array
  40. {
  41. return $this->getRepository()->findBy($criteria, $orderBy, $limit, $offset);
  42. }
  43. public function findOneBy(array $criteria): ?object
  44. {
  45. return $this->getRepository()->findOneBy($criteria);
  46. }
  47. public function find(mixed $id): ?object
  48. {
  49. return $this->getRepository()->find($id);
  50. }
  51. public function create(): object
  52. {
  53. return new $this->class();
  54. }
  55. public function save(object $entity, bool $andFlush = true): void
  56. {
  57. $this->checkObject($entity);
  58. $this->getObjectManager()->persist($entity);
  59. if ($andFlush) {
  60. $this->getObjectManager()->flush();
  61. }
  62. }
  63. public function delete(object $entity, bool $andFlush = true): void
  64. {
  65. $this->checkObject($entity);
  66. $this->getObjectManager()->remove($entity);
  67. if ($andFlush) {
  68. $this->getObjectManager()->flush();
  69. }
  70. }
  71. public function clear(): void
  72. {
  73. $this->getObjectManager()->clear();
  74. }
  75. /**
  76. * Returns the related Object Repository.
  77. *
  78. * @return ObjectRepository<object>
  79. *
  80. * @phpstan-return ObjectRepository<T>
  81. */
  82. protected function getRepository(): ObjectRepository
  83. {
  84. return $this->getObjectManager()->getRepository($this->class);
  85. }
  86. /**
  87. * @throws \InvalidArgumentException
  88. *
  89. * @phpstan-param T $object
  90. */
  91. protected function checkObject(object $object): void
  92. {
  93. if (!$object instanceof $this->class) {
  94. throw new \InvalidArgumentException(\sprintf(
  95. 'Object must be instance of %s, %s given',
  96. $this->class,
  97. get_debug_type($object)
  98. ));
  99. }
  100. }
  101. /**
  102. * @throws \RuntimeException
  103. */
  104. protected function getObjectManager(): ObjectManager
  105. {
  106. $manager = $this->registry->getManagerForClass($this->class);
  107. if (null === $manager) {
  108. throw new \RuntimeException(\sprintf(
  109. 'Unable to find the mapping information for the class %s.'
  110. .' Please check the `auto_mapping` option'
  111. .' (http://symfony.com/doc/current/reference/configuration/doctrine.html#configuration-overview)'
  112. .' or add the bundle to the `mappings` section in the doctrine configuration.',
  113. $this->class
  114. ));
  115. }
  116. return $manager;
  117. }
  118. }