ArrayMap::offsetGet()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 7
c 0
b 0
f 0
ccs 4
cts 4
cp 1
rs 10
cc 2
nc 2
nop 1
crap 2
1
<?php
2
3
namespace Maketok\DataMigration;
4
5
class ArrayMap implements MapInterface
6
{
7
    /**
8
     * @var array
9
     */
10
    protected $state = [];
11
    /**
12
     * @var array
13
     */
14
    protected $lastFed = [];
15
    /**
16
     * @var bool
17
     */
18
    protected $frozen = false;
19
20
    /**
21
     * {@inheritdoc}
22
     */
23 1
    public function getIterator()
24
    {
25 1
        return new \ArrayIterator($this->state);
26
    }
27
28
    /**
29
     * {@inheritdoc}
30
     */
31 34
    public function offsetExists($offset)
32
    {
33 34
        return array_key_exists($offset, $this->state);
34
    }
35
36
    /**
37
     * {@inheritdoc}
38
     */
39 31
    public function offsetGet($offset)
40
    {
41 31
        if ($this->offsetExists($offset)) {
42 30
            return $this->state[$offset];
43
        }
44 3
        return null;
45
    }
46
47
    /**
48
     * {@inheritdoc}
49
     */
50 14
    public function offsetSet($offset, $value)
51
    {
52 14
        $this->state[$offset] = $value;
53 14
    }
54
55
    /**
56
     * {@inheritdoc}
57
     */
58 1
    public function offsetUnset($offset)
59
    {
60 1
        if ($this->offsetExists($offset)) {
61 1
            unset($this->state[$offset]);
62 1
        }
63 1
    }
64
65
    /**
66
     * {@inheritdoc}
67
     */
68 23
    public function feed(array $row)
69
    {
70 23
        $this->state = array_replace($this->state, $row);
71 23
    }
72
73
    /**
74
     * {@inheritdoc}
75
     */
76 10
    public function isFresh(array $row)
77
    {
78 10
        return $this->lastFed != $row;
79
    }
80
81
    /**
82
     * {@inheritdoc}
83
     */
84 20
    public function clear()
85
    {
86 20
        $this->state = [];
87 20
        $this->lastFed = [];
88 20
    }
89
90
    /**
91
     * {@inheritdoc}
92
     */
93 10
    public function incr($key, $default, $step = 1)
94
    {
95 10
        if (!$this->offsetExists($key)) {
96 10
            $current = (int) $default;
97 10
        } else {
98 10
            $current = $this->offsetGet($key);
99 10
            $current += $step;
100
        }
101 10
        $this->offsetSet($key, $current);
102 10
        return $current;
103
    }
104
105
    /**
106
     * getter
107
     * @param $key
108
     * @return mixed|null
109
     */
110 28
    public function __get($key)
111
    {
112 28
        return $this->offsetGet($key);
113
    }
114
115
    /**
116
     * @param string $key
117
     * @param mixed $value
118
     */
119 5
    public function __set($key, $value)
120
    {
121 5
        $this->offsetSet($key, $value);
122 5
    }
123
124
    /**
125
     * {@inheritdoc}
126
     */
127 2
    public function dumpState()
128
    {
129 2
        return $this->state;
130
    }
131
132
    /**
133
     * {@inheritdoc}
134
     */
135 3
    public function setState(array $state)
136
    {
137 3
        $this->state = $state;
138 3
    }
139
140
    /**
141
     * {@inheritdoc}
142
     */
143 9
    public function frozenIncr($key, $default, $step = 1)
144
    {
145 9
        if (!$this->offsetExists($key)) {
146 8
            $current = (int) $default;
147 8
        } else {
148 7
            $current = $this->offsetGet($key);
149 7
            if (!$this->frozen) {
150 7
                $current += $step;
151 7
            }
152
        }
153 9
        $this->offsetSet($key, $current);
154 9
        return $current;
155
    }
156
157
    /**
158
     * {@inheritdoc}
159
     */
160 6
    public function unFreeze()
161
    {
162 6
        $this->frozen = false;
163 6
    }
164
165
    /**
166
     * {@inheritdoc}
167
     */
168 6
    public function freeze()
169
    {
170 6
        $this->frozen = true;
171 6
    }
172
}
173