Completed
Push — 6.13 ( 09bc2f...2da551 )
by André
18:44 queued 11s
created

TransactionItem::isMiss()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 0
dl 0
loc 11
rs 9.9
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * @copyright Copyright (C) eZ Systems AS. All rights reserved.
5
 * @license For full copyright and license information view LICENSE file distributed with this source code.
6
 */
7
namespace eZ\Publish\Core\Persistence\Cache\Adapter\Item;
8
9
use Tedivm\StashBundle\Service\CacheItem;
10
11
/**
12
 * Class TransactionItem.
13
 *
14
 * Custom Item class for use during transactions, routes calls to save() and clear() to clear callback.
15
 *
16
 * @internal
17
 */
18
class TransactionItem extends CacheItem
19
{
20
    /** @var callable */
21
    private $clearFn;
22
23
    /** @var callable */
24
    private $isItemDeferedClearedFn;
25
26
    public function setClearCallback(callable $clear)
27
    {
28
        $this->clearFn = $clear;
29
    }
30
31
    public function setIsClearedCallback(callable $isItemDeferedCleared)
32
    {
33
        $this->isItemDeferedClearedFn = $isItemDeferedCleared;
34
    }
35
36
    /**
37
     * {@inheritdoc}
38
     */
39
    public function isMiss()
40
    {
41
        // Mark any cache item which has been scheduled to be cleared as a miss.
42
        // We do this using callback since isHit property is private, & it will be reset on get()
43
        $fn = $this->isItemDeferedClearedFn;
44
        if ($fn($this->keyString)) {
45
            return true;
46
        }
47
48
        return parent::isMiss();
49
    }
50
51
    /**
52
     * {@inheritdoc}
53
     */
54
    public function save()
55
    {
56
        // We don't save cache during transaction (as cache is shared), & given the use of cache items within eZ
57
        // Platform kernel is not used across transaction boundaries items (these) given by pool can safely be assumed
58
        // to still be within transaction when save() is called.
59
        // ...
60
        // We do need to tell Pool that it should delete the item tough (which is done on commit if in transaction)
61
        // so this save will be done on-demand when needed.
62
        $clear = $this->clearFn;
63
        $clear($this->keyString);
64
65
        return false;
66
    }
67
68
    /**
69
     * {@inheritdoc}
70
     */
71
    public function clear()
72
    {
73
        $clear = $this->clearFn;
74
        $clear($this->keyString);
75
76
        return true;
77
    }
78
}
79