|
| 1 | +<?php |
| 2 | +declare(strict_types=1); |
| 3 | + |
| 4 | +namespace Markup\JobQueueBundle\Repository; |
| 5 | + |
| 6 | +use Doctrine\Common\Persistence\ManagerRegistry; |
| 7 | +use Doctrine\ORM\EntityManager; |
| 8 | +use Doctrine\ORM\EntityManagerInterface; |
| 9 | +use Doctrine\ORM\EntityRepository; |
| 10 | +use Doctrine\ORM\Query; |
| 11 | +use Markup\JobQueueBundle\Entity\JobStatus; |
| 12 | + |
| 13 | +class JobStatusRepository |
| 14 | +{ |
| 15 | + /** @var ManagerRegistry */ |
| 16 | + private $doctrine; |
| 17 | + |
| 18 | + public function __construct(ManagerRegistry $doctrine) |
| 19 | + { |
| 20 | + $this->doctrine = $doctrine; |
| 21 | + } |
| 22 | + |
| 23 | + public function isUserEnabled(string $command, array $arguments): bool |
| 24 | + { |
| 25 | + /** @var JobStatus $jobStatus */ |
| 26 | + $jobStatus = $this->getEntityRepository()->findOneBy([ |
| 27 | + 'command' => $command, |
| 28 | + 'arguments' => ($arguments) ? json_encode($arguments) : null |
| 29 | + ]); |
| 30 | + |
| 31 | + if ($jobStatus instanceof JobStatus) { |
| 32 | + return $jobStatus->getEnabled(); |
| 33 | + } |
| 34 | + |
| 35 | + return true; |
| 36 | + } |
| 37 | + |
| 38 | + public function findBy(array $arguments): ?JobStatus |
| 39 | + { |
| 40 | + $jobStatus = $this->getEntityRepository()->findOneBy($arguments); |
| 41 | + |
| 42 | + if ($jobStatus instanceof JobStatus) { |
| 43 | + return $jobStatus; |
| 44 | + } |
| 45 | + |
| 46 | + return null; |
| 47 | + } |
| 48 | + |
| 49 | + public function store(JobStatus $jobStatus): void |
| 50 | + { |
| 51 | + $this->getEntityManager()->persist($jobStatus); |
| 52 | + $this->getEntityManager()->flush($jobStatus); |
| 53 | + } |
| 54 | + |
| 55 | + private function getEntityRepository(): EntityRepository |
| 56 | + { |
| 57 | + $repository = $this->getEntityManager()->getRepository(JobStatus::class); |
| 58 | + |
| 59 | + if ($repository instanceof EntityRepository) { |
| 60 | + return $repository; |
| 61 | + } |
| 62 | + |
| 63 | + throw new \RuntimeException(sprintf('Doctrine returned an invalid repository for entity JobStatus')); |
| 64 | + } |
| 65 | + |
| 66 | + private function getEntityManager(): EntityManager |
| 67 | + { |
| 68 | + $manager = $this->doctrine->getManager(); |
| 69 | + |
| 70 | + if ($manager instanceof EntityManagerInterface && !$manager->isOpen()) { |
| 71 | + $manager = $this->doctrine->resetManager(); |
| 72 | + } |
| 73 | + |
| 74 | + if ($manager instanceof EntityManager) { |
| 75 | + return $manager; |
| 76 | + } |
| 77 | + |
| 78 | + throw new \RuntimeException(sprintf('Doctrine returned an invalid type for entity manager')); |
| 79 | + } |
| 80 | +} |
0 commit comments