|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Yokai\Batch\Bridge\Doctrine\Persistence; |
| 6 | + |
| 7 | +use Doctrine\Persistence\ManagerRegistry; |
| 8 | +use Doctrine\Persistence\ObjectManager; |
| 9 | +use Doctrine\Persistence\ObjectRepository; |
| 10 | +use Yokai\Batch\Exception\InvalidArgumentException; |
| 11 | + |
| 12 | +/** |
| 13 | + * This class will remember objects identifies when found. |
| 14 | + * Using it as a proxy to your queries will simplify queries afterward. |
| 15 | + */ |
| 16 | +final class ObjectRegistry |
| 17 | +{ |
| 18 | + /** |
| 19 | + * @var array<class-string, array<string, array<string, mixed>>> |
| 20 | + */ |
| 21 | + private array $identities = []; |
| 22 | + |
| 23 | + public function __construct( |
| 24 | + private ManagerRegistry $doctrine, |
| 25 | + ) { |
| 26 | + } |
| 27 | + |
| 28 | + /** |
| 29 | + * Finds a single object by a set of criteria. |
| 30 | + * |
| 31 | + * @template T of object |
| 32 | + * |
| 33 | + * @param class-string<T> $class |
| 34 | + * @param array<string, mixed> $criteria |
| 35 | + * |
| 36 | + * @return T|null |
| 37 | + */ |
| 38 | + public function findOneBy(string $class, array $criteria): ?object |
| 39 | + { |
| 40 | + return $this->findOneUsing( |
| 41 | + $class, |
| 42 | + function ($repository) use ($criteria) { |
| 43 | + /** @var ObjectRepository<T> $repository */ |
| 44 | + |
| 45 | + return $repository->findOneBy($criteria); |
| 46 | + }, |
| 47 | + serialize($criteria) |
| 48 | + ); |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * Finds a single object by using a callback to find it. |
| 53 | + * |
| 54 | + * @template T of object |
| 55 | + * |
| 56 | + * @param class-string<T> $class |
| 57 | + * @param \Closure(ObjectRepository<T>, ObjectManager): (T|null) $closure |
| 58 | + * |
| 59 | + * @return T|null |
| 60 | + */ |
| 61 | + public function findOneUsing(string $class, \Closure $closure, string $key = null): ?object |
| 62 | + { |
| 63 | + $manager = $this->doctrine->getManagerForClass($class); |
| 64 | + if ($manager === null) { |
| 65 | + throw new InvalidArgumentException(sprintf('Class "%s" is not a managed Doctrine entity.', $class)); |
| 66 | + } |
| 67 | + |
| 68 | + $key ??= spl_object_hash($closure); |
| 69 | + $key = md5($key); |
| 70 | + |
| 71 | + $identity = $this->identities[$class][$key] ?? null; |
| 72 | + if ($identity !== null) { |
| 73 | + return $manager->find($class, $identity); |
| 74 | + } |
| 75 | + |
| 76 | + $object = $closure($manager->getRepository($class), $manager); |
| 77 | + |
| 78 | + if ($object instanceof $class) { |
| 79 | + $this->identities[$class] ??= []; |
| 80 | + $this->identities[$class][$key] = $manager->getClassMetadata($class)->getIdentifierValues($object); |
| 81 | + } |
| 82 | + |
| 83 | + return $object; |
| 84 | + } |
| 85 | + |
| 86 | + /** |
| 87 | + * Removes all remembered identities of all classes. |
| 88 | + */ |
| 89 | + public function reset(): void |
| 90 | + { |
| 91 | + $this->identities = []; |
| 92 | + } |
| 93 | +} |
0 commit comments