Completed
Push — master ( 17cd5a...3adfc7 )
by Thomas
02:38
created

Indexer::lookupValues()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3

Importance

Changes 1
Bugs 1 Features 1
Metric Value
c 1
b 1
f 1
dl 0
loc 10
ccs 8
cts 8
cp 1
rs 9.4285
cc 3
eloc 6
nc 2
nop 1
crap 3
1
<?php
2
3
namespace Gielfeldt\TransactionalPHP;
4
5
/**
6
 * Class Indexer
7
 *
8
 * @package Gielfeldt\TransactionalPHP
9
 */
10
class Indexer
11
{
12
    /**
13
     * Operations indexed by key and operation index.
14
15
     * @var Operation[][]
16
     */
17
    protected $index = [];
18
19
    /**
20
     * The connection used by this indexer.
21
     *
22
     * @var Connection
23
     */
24
    protected $connection;
25
26
    /**
27
     * Indexer constructor.
28
     *
29
     * @param Connection $connection
30
     */
31 5
    public function __construct(Connection $connection)
32
    {
33 5
        $this->connection = $connection;
34 5
    }
35
36
    /**
37
     * Get connection.
38
     *
39
     * @return Connection
40
     */
41 1
    public function getConnection()
42
    {
43 1
        return $this->connection;
44
    }
45
46
    /**
47
     * Index operation.
48
     *
49
     * @param string $key
50
     *   The key to index under.
51
     * @param Operation $operation
52
     *   The operation to index.
53
     *
54
     * @return Operation
55
     *   The operation indexed.
56
     */
57 4
    public function index($key, Operation $operation)
58
    {
59 4
        $this->index[$key][$operation->idx($this->connection)] = $operation;
60 4
        $indexer = $this;
61
        $operation->onCommit(function ($operation) use ($key, $indexer) {
62 1
            $indexer->deIndex($key, $operation);
63 4
        });
64 4
        $operation->onRollback(function ($operation) use ($key, $indexer) {
65 1
            $indexer->deIndex($key, $operation);
66 4
        });
67 4
        return $operation;
68
    }
69
70
    /**
71
     * De-index operation.
72
     *
73
     * @param string $key
74
     *   The key to index under.
75
     * @param Operation $operation
76
     *   The operation to index.
77
     *
78
     * @return Operation
79
     *   The operation de-indexed.
80
     */
81 2
    public function deIndex($key, Operation $operation)
82
    {
83 2
        unset($this->index[$key][$operation->idx($this->connection)]);
84 2
        return $operation;
85
    }
86
87
    /**
88
     * Lookup operation.
89
     *
90
     * @param string $key
91
     *   The key to look up.
92
     *
93
     * @return Operation[]
94
     *   Operations keyed by operation index.
95
     */
96 3
    public function lookup($key)
97
    {
98 3
        return isset($this->index[$key]) ? $this->index[$key] : [];
99
    }
100
101
    /**
102
     * Lookup operation values.
103
     *
104
     * @param string $key
105
     *   The key to look up.
106
     *
107
     * @return array
108
     *   Values keyed by operation index.
109
     */
110 1
    public function lookupValues($key)
111
    {
112 1
        $values = [];
113 1
        if (isset($this->index[$key])) {
114 1
            foreach ($this->index[$key] as $operation) {
115 1
                $values[$operation->idx($this->connection)] = $operation->getValue();
116 1
            }
117 1
        }
118 1
        return $values;
119
    }
120
}
121