Completed
Push — 6.7_race_cond_testing ( 56a653 )
by André
18:41 queued 11s
created

TransactionItem   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

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

5 Methods

Rating   Name   Duplication   Size   Complexity  
A setClearCallback() 0 4 1
A setIsClearedCallback() 0 4 1
A isMiss() 0 11 2
A save() 0 13 1
A clear() 0 7 1
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;
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
    private $clearFn;
21
    private $isItemDeferedClearedFn;
22
23
    public function setClearCallback(callable $clear)
24
    {
25
        $this->clearFn = $clear;
26
    }
27
28
    public function setIsClearedCallback(callable $isItemDeferedCleared)
29
    {
30
        $this->isItemDeferedClearedFn = $isItemDeferedCleared;
31
    }
32
33
    /**
34
     * {@inheritdoc}
35
     */
36
    public function isMiss()
37
    {
38
        // Mark any cache item which has been scheduled to be cleared as a miss.
39
        // We do this using callback since isHit property is private, & it will be reset on get()
40
        $fn = $this->isItemDeferedClearedFn;
41
        if ($fn($this->keyString)) {
42
            return true;
43
        }
44
45
        return parent::isMiss();
46
    }
47
48
    /**
49
     * {@inheritdoc}
50
     */
51
    public function save()
52
    {
53
        // We don't save cache during transaction (as cache is shared), & given the use of cache items within eZ
54
        // Platform kernel is not used across transaction boundaries items (these) given by pool can safely be assumed
55
        // to still be within transaction when save() is called.
56
        // ...
57
        // We do need to tell Pool that it should delete the item tough (which is done on commit if in transaction)
58
        // so this save will be done on-demand when needed.
59
        $clear = $this->clearFn;
60
        $clear($this->keyString);
61
62
        return false;
63
    }
64
65
    /**
66
     * {@inheritdoc}
67
     */
68
    public function clear()
69
    {
70
        $clear = $this->clearFn;
71
        $clear($this->keyString);
72
73
        return true;
74
    }
75
}
76