xsolve-pl /
xsolve-associate
| 1 | <?php |
||
| 2 | |||
| 3 | namespace Xsolve\Associate\DoctrineOrm\Metadata; |
||
| 4 | |||
| 5 | use Doctrine\ORM\EntityManagerInterface; |
||
| 6 | use Doctrine\ORM\EntityRepository; |
||
| 7 | use Doctrine\ORM\Mapping\ClassMetadata; |
||
| 8 | |||
| 9 | class MetadataAdapterProvider |
||
| 10 | { |
||
| 11 | /** |
||
| 12 | * @var EntityManagerInterface |
||
| 13 | */ |
||
| 14 | protected $entityManager; |
||
| 15 | |||
| 16 | /** |
||
| 17 | * @var (ClassMetadataAdapter|null)[] |
||
| 18 | */ |
||
| 19 | protected $classMetadataAdapters = []; |
||
| 20 | |||
| 21 | /** |
||
| 22 | * @param EntityManagerInterface $entityManager |
||
| 23 | */ |
||
| 24 | public function __construct(EntityManagerInterface $entityManager) |
||
| 25 | { |
||
| 26 | $this->entityManager = $entityManager; |
||
| 27 | } |
||
| 28 | |||
| 29 | // /** |
||
| 30 | // * @param array $objects |
||
| 31 | // * |
||
| 32 | // * @return ClassMetadataAdapter |
||
| 33 | // * |
||
| 34 | // * @throws \Exception |
||
| 35 | // */ |
||
| 36 | // public function getClassMetadataAdapterForEntities(array $objects): ClassMetadataAdapter |
||
| 37 | // { |
||
| 38 | // $className = $this->getClassNameForEntities($objects); |
||
| 39 | // $classMetadataAdapter = $this->getClassMetadataAdapterByClassName($className); |
||
| 40 | // if (!$classMetadataAdapter instanceof ClassMetadataAdapter) { |
||
| 41 | // throw new \Exception(); |
||
| 42 | // } |
||
| 43 | // |
||
| 44 | // return $classMetadataAdapter; |
||
| 45 | // } |
||
| 46 | |||
| 47 | /** |
||
| 48 | * @param array $objects |
||
| 49 | * |
||
| 50 | * @return string |
||
| 51 | * |
||
| 52 | * @throws \Exception |
||
| 53 | */ |
||
| 54 | public function getClassNameForEntities(array $objects): string |
||
| 55 | { |
||
| 56 | if (!$objects) { |
||
|
0 ignored issues
–
show
|
|||
| 57 | throw new \Exception(); |
||
| 58 | } |
||
| 59 | |||
| 60 | $firstObject = array_shift($objects); |
||
| 61 | $classMetadataAdapter = $this->getClassMetadataAdapterByClassName(get_class($firstObject)); |
||
| 62 | if (!$classMetadataAdapter instanceof ClassMetadataAdapter) { |
||
| 63 | throw new \Exception(); |
||
| 64 | } |
||
| 65 | $commonClassName = $classMetadataAdapter->getClassName(); |
||
| 66 | $rootClassNameUsed = false; |
||
| 67 | |||
| 68 | foreach ($objects as $object) { |
||
| 69 | if (is_a($object, $commonClassName, true)) { |
||
| 70 | continue; |
||
| 71 | } |
||
| 72 | if ($rootClassNameUsed) { |
||
| 73 | throw new \Exception(); |
||
| 74 | } |
||
| 75 | $commonClassName = $classMetadataAdapter->getRootClassName(); |
||
| 76 | if (!is_a($object, $commonClassName, true)) { |
||
| 77 | throw new \Exception(); |
||
| 78 | } |
||
| 79 | } |
||
| 80 | |||
| 81 | return $commonClassName; |
||
| 82 | } |
||
| 83 | |||
| 84 | /** |
||
| 85 | * @param string $className |
||
| 86 | * |
||
| 87 | * @return ClassMetadataAdapter|null |
||
| 88 | * |
||
| 89 | * @throws \Exception |
||
| 90 | */ |
||
| 91 | public function getClassMetadataAdapterByClassName(string $className): ?ClassMetadataAdapter |
||
| 92 | { |
||
| 93 | if (!array_key_exists($className, $this->classMetadataAdapters)) { |
||
| 94 | $this->initializeClassMetadataAdapterByClassName($className); |
||
| 95 | } |
||
| 96 | |||
| 97 | return $this->classMetadataAdapters[$className]; |
||
| 98 | } |
||
| 99 | |||
| 100 | /** |
||
| 101 | * @param string $className |
||
| 102 | * |
||
| 103 | * @throws \Exception |
||
| 104 | */ |
||
| 105 | protected function initializeClassMetadataAdapterByClassName(string $className): void |
||
| 106 | { |
||
| 107 | $classMetadata = $this->entityManager->getClassMetadata($className); |
||
| 108 | if (!$classMetadata instanceof ClassMetadata) { |
||
| 109 | $this->classMetadataAdapters[$className] = null; |
||
| 110 | |||
| 111 | return; |
||
| 112 | } |
||
| 113 | |||
| 114 | $entityRepository = $this->entityManager->getRepository($className); |
||
| 115 | if (!$entityRepository instanceof EntityRepository) { |
||
| 116 | throw new \Exception(); |
||
| 117 | } |
||
| 118 | $this->classMetadataAdapters[$className] = new ClassMetadataAdapter($this, $entityRepository, $classMetadata); |
||
| 119 | } |
||
| 120 | } |
||
| 121 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.