Skip to content

Commit

Permalink
Some fixes.
Browse files Browse the repository at this point in the history
  • Loading branch information
malef committed May 18, 2018
1 parent 27247a0 commit 637f886
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@
interface CollectionTraversalStrategyInterface
{
/**
* @param $propertyValue
* @param mixed $propertyValue
*
* @return bool
*/
public function supports($propertyValue): bool;

/**
* @param ObjectCollectionInterface $objectCollection
* @param $propertyValue
* @param mixed $propertyValue
*/
public function traverse(ObjectCollectionInterface $objectCollection, $propertyValue);
}
22 changes: 19 additions & 3 deletions lib/Facade.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
class Facade
{
/**
* @var EntityManagerInterface
* @var EntityManagerInterface|null
*/
protected $entityManager;

Expand Down Expand Up @@ -111,12 +111,14 @@ public function getBufferedDoctrineOrmCollector(): BufferedCollectorInterface

/**
* @return MetadataWrapperProvider
*
* @throws \Exception
*/
protected function getMetadataWrapperProvider(): MetadataWrapperProvider
{
$this->assertEntityManagerAvailable();
$entityManager = $this->getEntityManager();
if (!$this->metadataWrapperProvider instanceof MetadataWrapperProvider) {
$this->metadataWrapperProvider = new MetadataWrapperProvider($this->entityManager);
$this->metadataWrapperProvider = new MetadataWrapperProvider($entityManager);
}

return $this->metadataWrapperProvider;
Expand Down Expand Up @@ -190,4 +192,18 @@ protected function assertEntityManagerAvailable()
throw new \Exception('Entity manager not available.');
}
}

/**
* @return EntityManagerInterface
*
* @throws \Exception
*/
protected function getEntityManager(): EntityManagerInterface
{
if (!$this->entityManager instanceof EntityManagerInterface) {
throw new \Exception('Entity manager not available.');
}

return $this->entityManager;
}
}
7 changes: 4 additions & 3 deletions lib/Loader/DoctrineOrmEntityLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class DoctrineOrmEntityLoader
protected $nonProxiedAssociationQueryExecutor;

/**
* @var int
* @var int|null
*/
protected $chunkSize = 1000;

Expand Down Expand Up @@ -132,12 +132,13 @@ public function loadAssociatedUninitializedCollectionsAndProxies(
return;
}

// Or we can have one-to-one association with source being the inverse side.
// Or we have one-to-one association with source being the inverse side.
if (
$associationMetadataWrapper->isOneToOne()
&& $associationMetadataWrapper->isInverseSide()
) {
// We don't have to do anything as these objects as automatically loaded by Doctrine.
// We don't have to do anything as these objects are automatically loaded by Doctrine with separate queries
// and there's no way to optimize this.

return;
}
Expand Down
4 changes: 2 additions & 2 deletions lib/Metadata/ClassMetadataWrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class ClassMetadataWrapper
protected $classMetadata;

/**
* @var string
* @var string|null
*/
protected $identifierFieldName;

Expand Down Expand Up @@ -116,7 +116,7 @@ public function getAssociationMetadataWrapper(string $associationName)
$associationMapping = $this->classMetadata->getAssociationMapping($associationName);
} catch (MappingException $e) {
if (0 === strpos($e->getMessage(), 'No mapping found for field ')) {
return;
return null;
}
throw $e;
}
Expand Down
17 changes: 10 additions & 7 deletions lib/Metadata/MetadataWrapperProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Xsolve\Associate\Metadata;

use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\EntityRepository;
use Doctrine\ORM\Mapping\ClassMetadata;

class MetadataWrapperProvider
Expand All @@ -13,7 +14,7 @@ class MetadataWrapperProvider
protected $entityManager;

/**
* @var ClassMetadataWrapper[]
* @var (ClassMetadataWrapper|null)[]
*/
protected $classMetadataWrappers = [];

Expand Down Expand Up @@ -44,7 +45,7 @@ function (string $className) { return $this->getClassMetadataWrapperByClassName(

// If metadata were not available for some objects.
if (in_array(null, $classMetadataWrappers, true)) {
return;
return null;
}

$classNames = array_unique(array_map(
Expand Down Expand Up @@ -76,18 +77,20 @@ function (ClassMetadataWrapper $classMetadataWrapper) {
* @param string $className
*
* @return ClassMetadataWrapper|null
*
* @throws \Exception
*/
public function getClassMetadataWrapperByClassName(string $className)
{
if (!array_key_exists($className, $this->classMetadataWrappers)) {
$classMetadata = $this->entityManager->getClassMetadata($className);

if ($classMetadata instanceof ClassMetadata) {
$this->classMetadataWrappers[$className] = new ClassMetadataWrapper(
$this,
$this->entityManager->getRepository($className),
$classMetadata
);
$entityRepository = $this->entityManager->getRepository($className);
if (!$entityRepository instanceof EntityRepository) {
throw new \Exception();
}
$this->classMetadataWrappers[$className] = new ClassMetadataWrapper($this, $entityRepository, $classMetadata);
} else {
$this->classMetadataWrappers[$className] = null;
}
Expand Down
4 changes: 1 addition & 3 deletions lib/ObjectCollection/ObjectCollectionInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@
interface ObjectCollectionInterface
{
/**
* @param $object
*
* @return mixed
* @param mixed $object
*/
public function addOne($object);

Expand Down
Empty file removed tests/.gitkeep
Empty file.

0 comments on commit 637f886

Please sign in to comment.