malef /
associate
| 1 | <?php |
||||
| 2 | |||||
| 3 | namespace Malef\Associate\DoctrineOrm\Loader; |
||||
| 4 | |||||
| 5 | use Doctrine\ORM\Mapping\MappingException; |
||||
| 6 | use Malef\Associate\AssociateException; |
||||
| 7 | use Malef\Associate\DoctrineOrm\Association\AssociationTree; |
||||
| 8 | use Malef\Associate\DoctrineOrm\Association\AssociationTreeNode; |
||||
| 9 | use Malef\Associate\DoctrineOrm\Collector\AssociationCollector; |
||||
| 10 | use Malef\Associate\DoctrineOrm\Loader\ArgumentConverter\AssociationsArgumentConverter; |
||||
| 11 | use Malef\Associate\DoctrineOrm\Loader\ArgumentConverter\EntitiesArgumentConverter; |
||||
| 12 | use Malef\Associate\DoctrineOrm\Metadata\MetadataAdapterProvider; |
||||
| 13 | use Malef\Associate\DoctrineOrm\Source\EntitySource; |
||||
| 14 | |||||
| 15 | class EntityLoader |
||||
| 16 | { |
||||
| 17 | /** |
||||
| 18 | * @var EntitiesArgumentConverter |
||||
| 19 | */ |
||||
| 20 | protected $entitiesArgumentConverter; |
||||
| 21 | |||||
| 22 | /** |
||||
| 23 | * @var AssociationsArgumentConverter |
||||
| 24 | */ |
||||
| 25 | protected $associationsArgumentConverter; |
||||
| 26 | |||||
| 27 | /** |
||||
| 28 | * @var MetadataAdapterProvider |
||||
| 29 | */ |
||||
| 30 | protected $metadataAdapterProvider; |
||||
| 31 | |||||
| 32 | /** |
||||
| 33 | * @var AssociationLoader |
||||
| 34 | */ |
||||
| 35 | protected $associationLoader; |
||||
| 36 | |||||
| 37 | /** |
||||
| 38 | * @var AssociationCollector |
||||
| 39 | */ |
||||
| 40 | protected $associationCollector; |
||||
| 41 | |||||
| 42 | /** |
||||
| 43 | * @var UninitializedProxiesLoader |
||||
| 44 | */ |
||||
| 45 | protected $uninitializedProxiesLoader; |
||||
| 46 | |||||
| 47 | public function __construct( |
||||
| 48 | EntitiesArgumentConverter $entitiesArgumentConverter, |
||||
| 49 | AssociationsArgumentConverter $associationsArgumentConverter, |
||||
| 50 | MetadataAdapterProvider $metadataAdapterProvider, |
||||
| 51 | AssociationLoader $associationLoader, |
||||
| 52 | AssociationCollector $associationCollector, |
||||
| 53 | UninitializedProxiesLoader $uninitializedProxiesLoader |
||||
| 54 | ) { |
||||
| 55 | $this->entitiesArgumentConverter = $entitiesArgumentConverter; |
||||
| 56 | $this->associationsArgumentConverter = $associationsArgumentConverter; |
||||
| 57 | $this->metadataAdapterProvider = $metadataAdapterProvider; |
||||
| 58 | $this->associationLoader = $associationLoader; |
||||
| 59 | $this->associationCollector = $associationCollector; |
||||
| 60 | $this->uninitializedProxiesLoader = $uninitializedProxiesLoader; |
||||
| 61 | } |
||||
| 62 | |||||
| 63 | /** |
||||
| 64 | * @param AssociationTree|string[]|string $associations |
||||
| 65 | * |
||||
| 66 | * @throws \Exception |
||||
| 67 | */ |
||||
| 68 | public function load(iterable $entities, $associations, ?string $entityClass): void |
||||
| 69 | { |
||||
| 70 | $rootEntities = $this->entitiesArgumentConverter->convertToEntitiesSource( |
||||
| 71 | $entities, |
||||
| 72 | $entityClass, |
||||
| 73 | $this->metadataAdapterProvider |
||||
| 74 | ); |
||||
| 75 | |||||
| 76 | if ($rootEntities->isEmpty()) { |
||||
| 77 | return; |
||||
| 78 | } |
||||
| 79 | |||||
| 80 | $associationTree = $this->associationsArgumentConverter->convertToAssociationTree($associations); |
||||
| 81 | |||||
| 82 | $this->uninitializedProxiesLoader->load( |
||||
| 83 | $rootEntities->getEntities(), |
||||
| 84 | $rootEntities->getClassMetadataAdapter() |
||||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||||
| 85 | ); |
||||
| 86 | |||||
| 87 | /* @var AssociationTreeNode[] $nodes */ |
||||
| 88 | $nodes = $associationTree->getPreOrderedNodes(); |
||||
| 89 | |||||
| 90 | $nodeToEntitiesMap = new \SplObjectStorage(); |
||||
| 91 | |||||
| 92 | foreach ($nodes as $node) { |
||||
| 93 | $this->loadEntitiesForAssociationTreeNode($node, $rootEntities, $nodeToEntitiesMap); |
||||
| 94 | } |
||||
| 95 | } |
||||
| 96 | |||||
| 97 | /** |
||||
| 98 | * @throws MappingException |
||||
| 99 | * @throws AssociateException |
||||
| 100 | */ |
||||
| 101 | protected function loadEntitiesForAssociationTreeNode( |
||||
| 102 | AssociationTreeNode $node, |
||||
| 103 | EntitySource $rootEntities, |
||||
| 104 | \SplObjectStorage $nodeToEntitiesMap |
||||
| 105 | ): void { |
||||
| 106 | $parentNode = $node->getParent(); |
||||
| 107 | |||||
| 108 | /* var EntitySource $parentEntities */ |
||||
| 109 | $parentEntities = ($parentNode instanceof AssociationTreeNode) |
||||
| 110 | ? $nodeToEntitiesMap->offsetGet($parentNode) |
||||
| 111 | : $rootEntities; |
||||
| 112 | |||||
| 113 | $nodeAssociationMetadataAdapter = $this->metadataAdapterProvider |
||||
| 114 | ->getClassMetadataAdapterByClassName($parentEntities->getEntityClass()) |
||||
|
0 ignored issues
–
show
It seems like
$parentEntities->getEntityClass() can also be of type null; however, parameter $className of Malef\Associate\Doctrine...ataAdapterByClassName() does only seem to accept string, maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 115 | ->getAssociationMetadataAdapter($node->getAssociation()->getRelationshipName()); |
||||
| 116 | |||||
| 117 | $this->associationLoader->load( |
||||
| 118 | $parentEntities->getEntities(), |
||||
| 119 | $nodeAssociationMetadataAdapter |
||||
| 120 | ); |
||||
| 121 | |||||
| 122 | $nodeToEntitiesMap->offsetSet( |
||||
| 123 | $node, |
||||
| 124 | new EntitySource( |
||||
| 125 | $this->associationCollector->collect( |
||||
| 126 | $parentEntities->getEntities(), |
||||
| 127 | $nodeAssociationMetadataAdapter |
||||
| 128 | ), |
||||
| 129 | $nodeAssociationMetadataAdapter->getTargetClassName() |
||||
| 130 | ) |
||||
| 131 | ); |
||||
| 132 | } |
||||
| 133 | } |
||||
| 134 |