Failed Conditions
Pull Request — 2.8.x (#7919)
by
unknown
07:31
created

DefaultMultiGetRegion::getMultiple()   A

Complexity

Conditions 5
Paths 8

Size

Total Lines 24
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 5.0113

Importance

Changes 0
Metric Value
eloc 12
c 0
b 0
f 0
dl 0
loc 24
ccs 12
cts 13
cp 0.9231
rs 9.5555
cc 5
nc 8
nop 1
crap 5.0113
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\Region;
22
23
use Doctrine\Common\Cache\MultiGetCache;
24
use Doctrine\ORM\Cache\CacheEntry;
25
use Doctrine\ORM\Cache\CollectionCacheEntry;
26
27
/**
28
 * A cache region that enables the retrieval of multiple elements with one call
29
 *
30
 * @since   2.5
31
 * @author  Asmir Mustafic <[email protected]>
32
 */
33
class DefaultMultiGetRegion extends DefaultRegion
34
{
35
    /**
36
     * Note that the multiple type is due to doctrine/cache not integrating the MultiGetCache interface
37
     * in its signature due to BC in 1.x
38
     *
39
     * @var MultiGetCache|\Doctrine\Common\Cache\Cache
40
     */
41
    protected $cache;
42
43
    /**
44
     * {@inheritDoc}
45
     *
46
     * @param MultiGetCache $cache
47
     */
48 46
    public function __construct($name, MultiGetCache $cache, $lifetime = 0)
49
    {
50
        /* @var $cache \Doctrine\Common\Cache\Cache */
51 46
        parent::__construct($name, $cache, $lifetime);
52 46
    }
53
54
    /**
55
     * {@inheritdoc}
56
     */
57 2
    public function getMultiple(CollectionCacheEntry $collection)
58
    {
59 2
        $keysToRetrieve = [];
60
61 2
        foreach ($collection->identifiers as $index => $key) {
62 2
            $keysToRetrieve[$index] = $this->getCacheEntryKey($key);
63
        }
64
65 2
        $items = $this->cache->fetchMultiple($keysToRetrieve);
66 2
        if (count($items) !== count($keysToRetrieve)) {
67
            return null;
68
        }
69
70 2
        $returnableItems = [];
71
72 2
        foreach ($keysToRetrieve as $index => $key) {
73 2
            if (! $items[$key] instanceof CacheEntry) {
74 1
                return null;
75
            }
76
77 1
            $returnableItems[$index] = $items[$key];
78
        }
79
80 1
        return $returnableItems;
81
    }
82
}
83