Completed
Push — master ( 97e40f...89ec5c )
by André
40:26 queued 12:35
created

AbstractHandler   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 73
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 73
rs 10
c 0
b 0
f 0
wmc 6
lcom 1
cbo 1

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
B getMultipleCacheItems() 0 27 5
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\SPI\Persistence\Handler as PersistenceHandler;
12
use Symfony\Component\Cache\Adapter\TagAwareAdapterInterface;
13
14
/**
15
 * Class AbstractHandler.
16
 *
17
 * Abstract handler for use in other Persistence Cache Handlers.
18
 */
19
abstract class AbstractHandler
20
{
21
    /**
22
     * @var \Symfony\Component\Cache\Adapter\TagAwareAdapterInterface
23
     */
24
    protected $cache;
25
26
    /**
27
     * @var \eZ\Publish\SPI\Persistence\Handler
28
     */
29
    protected $persistenceHandler;
30
31
    /**
32
     * @var \eZ\Publish\Core\Persistence\Cache\PersistenceLogger
33
     */
34
    protected $logger;
35
36
    /**
37
     * Setups current handler with everything needed.
38
     *
39
     * @param \Symfony\Component\Cache\Adapter\TagAwareAdapterInterface $cache
40
     * @param \eZ\Publish\SPI\Persistence\Handler $persistenceHandler
41
     * @param \eZ\Publish\Core\Persistence\Cache\PersistenceLogger $logger
42
     */
43
    public function __construct(
44
        TagAwareAdapterInterface $cache,
45
        PersistenceHandler $persistenceHandler,
46
        PersistenceLogger $logger
47
    ) {
48
        $this->cache = $cache;
49
        $this->persistenceHandler = $persistenceHandler;
50
        $this->logger = $logger;
51
    }
52
53
    /**
54
     * Helper for getting multiple cache items in one call and do the id extraction for you.
55
     *
56
     * Cache items must be stored with a key in the following format "${keyPrefix}${id}", like "ez-content-info-${id}",
57
     * in order for this method to be able to prefix key on id's and also extract key prefix afterwards.
58
     *
59
     * @param array $ids
60
     * @param string $keyPrefix
61
     *
62
     * @return array Format [id[] $cacheMisses, CacheItem[<id>] $list], list contains hits & misses (if there where any).
63
     */
64
    final protected function getMultipleCacheItems(array $ids, string $keyPrefix): array
65
    {
66
        if (empty($ids)) {
67
            return [[], []];
68
        }
69
70
        $cacheKeys = [];
71
        foreach (array_unique($ids) as $id) {
72
            $cacheKeys[] = $keyPrefix . $id;
73
        }
74
75
        $list = [];
76
        $cacheMisses = [];
77
        $keyPrefixLength = strlen($keyPrefix);
78
        foreach ($this->cache->getItems($cacheKeys) as $key => $cacheItem) {
79
            $id = substr($key, $keyPrefixLength);
80
            if ($cacheItem->isHit()) {
81
                $list[$id] = $cacheItem->get();
82
                continue;
83
            }
84
85
            $cacheMisses[] = $id;
86
            $list[$id] = $cacheItem;
87
        }
88
89
        return [$cacheMisses, $list];
90
    }
91
}
92