src/Repository/BaseRepository.php line 66

Open in your IDE?
  1. <?php
  2. namespace App\Repository;
  3. use App\Entity\EntityInterface;
  4. use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepositoryInterface;
  5. use Doctrine\Common\Persistence\ManagerRegistry;
  6. use Doctrine\ORM\EntityManager;
  7. use Doctrine\ORM\EntityManagerInterface;
  8. use Doctrine\ORM\EntityRepository;
  9. use Doctrine\ORM\Mapping\ClassMetadata;
  10. use Symfony\Component\HttpFoundation\Response;
  11. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  12. /**
  13.  * BaseRepository
  14.  *
  15.  * @author Zwer<ante@q-software.com>
  16.  */
  17. abstract class BaseRepository extends EntityRepository implements ServiceEntityRepositoryInterface
  18. {
  19.     public const ENTITY_CLASS_NAME '';
  20.     public function __construct(EntityManagerInterface $entityManager, ?ClassMetadata $metadata null, ?ManagerRegistry $registry null)
  21.     {
  22.         if (static::ENTITY_CLASS_NAME === '') {
  23.             throw new \RuntimeException('Repository entity class name is empty');
  24.         }
  25.         if ($registry) {
  26.             /** @var EntityManager $manager */
  27.             $manager $registry->getManagerForClass($this::ENTITY_CLASS_NAME);
  28.             parent::__construct($manager$manager->getClassMetadata($this::ENTITY_CLASS_NAME));
  29.         } elseif ($entityManager instanceof EntityManager && $metadata instanceof ClassMetadata) {
  30.             parent::__construct($entityManager$metadata);
  31.         } else {
  32.             throw new \RuntimeException('Failed to initialize repository.');
  33.         }
  34.     }
  35.     public function save(EntityInterface $entitybool $flush true): EntityInterface
  36.     {
  37.         $this->_em->persist($entity);
  38.         if ($flush) {
  39.             $this->_em->flush();
  40.         }
  41.         return $entity;
  42.     }
  43.     public function merge(EntityInterface $entitybool $flush true): EntityInterface
  44.     {
  45.         /** @var EntityInterface $entity */
  46.         $entity $this->_em->merge($entity);
  47.         if ($flush) {
  48.             $this->_em->flush();
  49.             $this->_em->refresh($entity);
  50.         }
  51.         return $entity;
  52.     }
  53.     public function flush(EntityInterface $entity null): void
  54.     {
  55.         $this->_em->flush($entity);
  56.     }
  57.     public function remove(EntityInterface $entitybool $flush true): void
  58.     {
  59.         $this->_em->remove($entity);
  60.         if ($flush) {
  61.             $this->_em->flush();
  62.         }
  63.     }
  64.     public function findOr404(int $id): EntityInterface
  65.     {
  66.         /** @var EntityInterface|null $entity */
  67.         $entity $this->find($id);
  68.         if (null === $entity) {
  69.             $message sprintf('Resource of type %s and ID %s could not be found!'$this::ENTITY_CLASS_NAME$id);
  70.             throw new NotFoundHttpException($messagenullResponse::HTTP_NOT_FOUND);
  71.         }
  72.         return $entity;
  73.     }
  74. }