Completed
Pull Request — master (#8105)
by
unknown
10:29
created

DefaultCollectionHydrator::buildCacheEntry()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
eloc 4
c 0
b 0
f 0
dl 0
loc 9
ccs 0
cts 5
cp 0
rs 10
cc 2
nc 2
nop 3
crap 6
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\ORM\Cache;
6
7
use Doctrine\ORM\Cache\Persister\CachedPersister;
8
use Doctrine\ORM\EntityManagerInterface;
9
use Doctrine\ORM\Mapping\ClassMetadata;
10
use Doctrine\ORM\PersistentCollection;
11
use Doctrine\ORM\Query;
12
use Doctrine\ORM\UnitOfWork;
13
14
/**
15
 * Default hydrator cache for collections
16
 */
17
class DefaultCollectionHydrator implements CollectionHydrator
18
{
19
    /** @var EntityManagerInterface */
20
    private $em;
21
22
    /** @var UnitOfWork */
23
    private $uow;
24
25
    /** @var mixed[] */
26
    private static $hints = [Query::HINT_CACHE_ENABLED => true];
27
28
    /**
29
     * @param EntityManagerInterface $em The entity manager.
30
     */
31 52
    public function __construct(EntityManagerInterface $em)
32
    {
33 52
        $this->em  = $em;
34 52
        $this->uow = $em->getUnitOfWork();
35 52
    }
36
37
    /**
38
     * {@inheritdoc}
39
     */
40
    public function buildCacheEntry(ClassMetadata $metadata, CollectionCacheKey $key, $collection)
41
    {
42
        $data = [];
43
44
        foreach ($collection as $index => $entity) {
45
            $data[$index] = new EntityCacheKey($metadata->getRootClassName(), $this->uow->getEntityIdentifier($entity));
46
        }
47
48
        return new CollectionCacheEntry($data);
49
    }
50
51
    /**
52
     * {@inheritdoc}
53
     */
54 1
    public function loadCacheEntry(
55
        ClassMetadata $metadata,
56
        CollectionCacheKey $key,
57
        CollectionCacheEntry $entry,
58
        PersistentCollection $collection
59
    ) {
60
        /** @var CachedPersister $targetPersister */
61 1
        $association     = $metadata->getProperty($key->association);
62 1
        $targetPersister = $this->uow->getEntityPersister($association->getTargetEntity());
0 ignored issues
show
Bug introduced by
The method getTargetEntity() does not exist on Doctrine\ORM\Mapping\Property. It seems like you code against a sub-type of Doctrine\ORM\Mapping\Property such as Doctrine\ORM\Mapping\EmbeddedMetadata or Doctrine\ORM\Mapping\AssociationMetadata. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

62
        $targetPersister = $this->uow->getEntityPersister($association->/** @scrutinizer ignore-call */ getTargetEntity());
Loading history...
63 1
        $targetRegion    = $targetPersister->getCacheRegion();
0 ignored issues
show
Bug introduced by
The method getCacheRegion() does not exist on Doctrine\ORM\Persisters\Entity\EntityPersister. It seems like you code against a sub-type of Doctrine\ORM\Persisters\Entity\EntityPersister such as Doctrine\ORM\Cache\Persi...y\CachedEntityPersister. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

63
        /** @scrutinizer ignore-call */ 
64
        $targetRegion    = $targetPersister->getCacheRegion();
Loading history...
64 1
        $list            = [];
65
66 1
        $entityEntries = $targetRegion->getMultiple($entry);
67
68 1
        if ($entityEntries === null) {
69
            return null;
70
        }
71
72
        /** @var EntityCacheEntry[] $entityEntries */
73 1
        foreach ($entityEntries as $index => $entityEntry) {
74 1
            $data = $entityEntry->resolveAssociationEntries($this->em);
75
76 1
            $entity = $this->uow->createEntity($entityEntry->class, $data, self::$hints);
77
78 1
            $collection->hydrateSet($index, $entity);
79
80 1
            $list[$index] = $entity;
81
        }
82
83 1
        $this->uow->hydrationComplete();
84
85 1
        return $list;
86
    }
87
}
88