Completed
Pull Request — master (#6365)
by Daniel Tome
10:44
created

DefaultCollectionHydrator   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 10

Test Coverage

Coverage 95.83%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 10
dl 0
loc 71
ccs 23
cts 24
cp 0.9583
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A buildCacheEntry() 0 10 2
B loadCacheEntry() 0 27 3
1
<?php
2
3
/*
4
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
5
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
6
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
7
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
8
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
9
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
10
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
11
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
12
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
13
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
14
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
15
 *
16
 * This software consists of voluntary contributions made by many individuals
17
 * and is licensed under the MIT license. For more information, see
18
 * <http://www.doctrine-project.org>.
19
 */
20
21
namespace Doctrine\ORM\Cache;
22
23
use Doctrine\ORM\Query;
24
use Doctrine\ORM\PersistentCollection;
25
use Doctrine\ORM\Mapping\ClassMetadata;
26
use Doctrine\ORM\EntityManagerInterface;
27
28
/**
29
 * Default hydrator cache for collections
30
 *
31
 * @since   2.5
32
 * @author  Fabio B. Silva <[email protected]>
33
 */
34
class DefaultCollectionHydrator implements CollectionHydrator
35
{
36
    /**
37
     * @var \Doctrine\ORM\EntityManagerInterface
38
     */
39
    private $em;
40
41
    /**
42
     * @var \Doctrine\ORM\UnitOfWork
43
     */
44
    private $uow;
45
46
    /**
47
     * @var array
48
     */
49
    private static $hints = [Query::HINT_CACHE_ENABLED => true];
50
51
    /**
52
     * @param \Doctrine\ORM\EntityManagerInterface $em The entity manager.
53
     */
54 76
    public function __construct(EntityManagerInterface $em)
55
    {
56 76
        $this->em  = $em;
57 76
        $this->uow = $em->getUnitOfWork();
58 76
    }
59
60
    /**
61
     * {@inheritdoc}
62
     */
63 1
    public function buildCacheEntry(ClassMetadata $metadata, CollectionCacheKey $key, $collection)
64
    {
65 1
        $data = [];
66
67 1
        foreach ($collection as $index => $entity) {
68 1
            $data[$index] = new EntityCacheKey($metadata->rootEntityName, $this->uow->getEntityIdentifier($entity));
69
        }
70
71 1
        return new CollectionCacheEntry($data);
72
    }
73
74
    /**
75
     * {@inheritdoc}
76
     */
77 2
    public function loadCacheEntry(ClassMetadata $metadata, CollectionCacheKey $key, CollectionCacheEntry $entry, PersistentCollection $collection)
78
    {
79 2
        $assoc           = $metadata->associationMappings[$key->association];
80
        /* @var $targetPersister \Doctrine\ORM\Cache\Persister\CachedPersister */
81 2
        $targetPersister = $this->uow->getEntityPersister($assoc['targetEntity']);
82 2
        $targetRegion    = $targetPersister->getCacheRegion();
83 2
        $list            = [];
84
85 2
        $entityEntries = $targetRegion->getMultiple($entry);
86
87 2
        if ($entityEntries === null) {
88
            return null;
89
        }
90
91
        /* @var $entityEntries \Doctrine\ORM\Cache\EntityCacheEntry[] */
92 2
        foreach ($entityEntries as $index => $entityEntry) {
93 2
            $list[$index] = $this->uow->createEntity($entityEntry->class, $entityEntry->resolveAssociationEntries($this->em), self::$hints);
94
        }
95
96 2
        array_walk($list, function($entity, $index) use ($collection) {
97 2
            $collection->hydrateSet($index, $entity);
98 2
        });
99
100 2
        $this->uow->hydrationComplete();
101
102 2
        return $list;
103
    }
104
}
105