Indexer::getConnection()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 3
Bugs 0 Features 1
Metric Value
c 3
b 0
f 1
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
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
     * Operations indexed by this indexer.
21
     *
22
     * @var Operation[]
23
     */
24
    protected $operations = [];
25
26
    /**
27
     * The connection used by this indexer.
28
     *
29
     * @var Connection
30
     */
31
    protected $connection;
32
33
    /**
34
     * Indexer constructor.
35
     *
36
     * @param Connection $connection
37
     */
38 6
    public function __construct(Connection $connection)
39
    {
40 6
        $this->connection = $connection;
41 6
    }
42
43
    /**
44
     * Get connection.
45
     *
46
     * @return Connection
47
     */
48 1
    public function getConnection()
49
    {
50 1
        return $this->connection;
51
    }
52
53
    /**
54
     * Index operation.
55
     *
56
     * @param Operation $operation
57
     *   The operation to index.
58
     * @param string $key
59
     *   (optional) The key to index under.
60
     *
61
     * @return Operation
62
     *   The operation indexed.
63
     */
64 5
    public function index(Operation $operation, $key = null)
65
    {
66 5
        $this->operations[$operation->idx($this->connection)] = $operation;
67 5
        if (isset($key)) {
68 5
            $this->index[$key][$operation->idx($this->connection)] = $operation;
69 5
        }
70 5
        $indexer = $this;
71 5
        $operation->onRemove(function ($operation) use ($key, $indexer) {
72 1
            $indexer->deIndex($operation, $key);
73 5
        });
74 5
        return $operation;
75
    }
76
77
    /**
78
     * De-index operation.
79
     *
80
     * @param Operation $operation
81
     *   The operation to index.
82
     * @param string $key
83
     *   (optional) The key to index under.
84
     *
85
     * @return Operation
86
     *   The operation de-indexed.
87
     */
88 2
    public function deIndex(Operation $operation, $key = null)
89
    {
90 2
        unset($this->operations[$operation->idx($this->connection)]);
91 2
        if (isset($key)) {
92 2
            unset($this->index[$key][$operation->idx($this->connection)]);
93 2
        }
94 2
        return $operation;
95
    }
96
97
    /**
98
     * Lookup operation.
99
     *
100
     * @param string $key
101
     *   The key to look up.
102
     *
103
     * @return Operation[]
104
     *   Operations keyed by operation index.
105
     */
106 3
    public function lookup($key)
107
    {
108 3
        return isset($this->index[$key]) ? $this->index[$key] : [];
109
    }
110
111
    /**
112
     * Lookup operation values.
113
     *
114
     * @param string $indexKey
115
     *   The index key to look up.
116
     * @param string $metadataKey
117
     *   The metadata key to look up.
118
     *
119
     * @return array
120
     *   Values keyed by operation index.
121
     */
122 1
    public function lookupMetadata($indexKey, $metadataKey)
123
    {
124 1
        $values = [];
125 1
        if (isset($this->index[$indexKey])) {
126 1
            foreach ($this->index[$indexKey] as $operation) {
127 1
                $values[$operation->idx($this->connection)] = $operation->getMetadata($metadataKey);
128 1
            }
129 1
        }
130 1
        return $values;
131
    }
132
133
    /**
134
     * Get current indexed operations.
135
     *
136
     * @return Operation[]
137
     */
138 1
    public function getOperations()
139
    {
140 1
        return $this->operations;
141
    }
142
143
    /**
144
     * Get current index.
145
     *
146
     * @return Operation[][]
147
     */
148 1
    public function getIndex()
149
    {
150 1
        return $this->index;
151
    }
152
}
153