Completed
Push — in-memory-cache2 ( de4787 )
by André
21:05
created

AbstractInMemoryHandler::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 4
dl 0
loc 9
rs 9.9666
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * File containing the ContentHandler implementation.
5
 *
6
 * @copyright Copyright (C) eZ Systems AS. All rights reserved.
7
 * @license For full copyright and license information view LICENSE file distributed with this source code.
8
 */
9
namespace eZ\Publish\Core\Persistence\Cache;
10
11
use eZ\Publish\Core\Persistence\Cache\InMemory\MetadataCachePool;
12
use eZ\Publish\SPI\Persistence\Handler as PersistenceHandler;
13
use Symfony\Component\Cache\Adapter\TagAwareAdapterInterface;
14
15
/**
16
 * Class AbstractHandler.
17
 *
18
 * Abstract handler for use in other Persistence Cache Handlers.
19
 */
20
abstract class AbstractInMemoryHandler extends AbstractHandler
21
{
22
    /**
23
     * @var \eZ\Publish\Core\Persistence\Cache\InMemory\MetadataCachePool
24
     */
25
    protected $inMemoryPool;
26
27
    /**
28
     * Setups current handler with everything needed.
29
     *
30
     * @param \Symfony\Component\Cache\Adapter\TagAwareAdapterInterface $cache
31
     * @param \eZ\Publish\SPI\Persistence\Handler $persistenceHandler
32
     * @param \eZ\Publish\Core\Persistence\Cache\PersistenceLogger $logger
33
     */
34
    public function __construct(
35
        TagAwareAdapterInterface $cache,
36
        PersistenceHandler $persistenceHandler,
37
        PersistenceLogger $logger,
38
        MetadataCachePool $inMemoryPool
39
    ) {
40
        parent::__construct($cache, $persistenceHandler, $logger);
41
        $this->inMemoryPool = $inMemoryPool;
42
    }
43
44
    /**
45
     * Adds in-memory cache handling to {@see getMultipleCacheValues()}.
46
     */
47
    final protected function getMultipleInMemoryCacheValues(
48
        array $ids,
49
        string $keyPrefix,
50
        callable $missingFn,
51
        callable $taggingFn,
52
        callable $secondayIndexFn
53
    ): array {
54
        $list = [];
55
        foreach ($ids as $index => $id) {
56
            if ($object = $this->inMemoryPool->get($keyPrefix . $id)) {
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $object is correct as $this->inMemoryPool->get($keyPrefix . $id) (which targets eZ\Publish\Core\Persiste...etadataCachePool::get()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
57
                $list[$id] = $object;
58
                unset($ids[$index]);
59
            }
60
        }
61
62
        if (empty($ids)) {
63
            return $list;
64
        }
65
66
        foreach (parent::getMultipleCacheItems($ids, $keyPrefix, $missingFn, $taggingFn) as $id => $object) {
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (getMultipleCacheItems() instead of getMultipleInMemoryCacheValues()). Are you sure this is correct? If so, you might want to change this to $this->getMultipleCacheItems().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
67
            $list[$id] = $object;
68
            $this->inMemoryPool->set($keyPrefix . $id, $object, $secondayIndexFn($object));
69
        }
70
71
        return $list;
72
    }
73
}
74