MultiPk::getDepth()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 2
nop 0
dl 0
loc 8
rs 10
c 0
b 0
f 0
1
<?php
2
namespace Maphper;
3
class MultiPk implements \ArrayAccess {
4
	private $parent;
5
	private $primaryKey;
6
	private $lookup;
7
    private $mapper;
8
9
	public function __construct(Maphper $mapper, $lookup, array $primaryKey, MultiPk $parent = null) {
10
		$this->parent = $parent;
11
		$this->primaryKey = $primaryKey;
12
		$this->lookup = $lookup;
13
		$depth = $this->getDepth();
14
		$this->mapper = $mapper->filter([$primaryKey[$depth] => $lookup]);
0 ignored issues
show
Bug introduced by
The method filter() does not exist on Maphper\Maphper. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

14
		/** @scrutinizer ignore-call */ 
15
  $this->mapper = $mapper->filter([$primaryKey[$depth] => $lookup]);
Loading history...
15
	}
16
17
	private function getDepth() {
18
		$depth = 0;
19
		$obj = $this;
20
		while ($obj->parent != null) {
21
			$depth++;
22
			$obj = $obj->parent;
23
		}
24
		return $depth;
25
	}
26
27
	public function offsetGet($key) {
28
		$depth = $this->getDepth()+1;
29
		if (count($this->primaryKey)-1 == $depth) return $this->mapper->filter([$this->primaryKey[$depth] => $key])->item(0);
30
		else return new MultiPk($this->mapper, $key, $this->primaryKey, $this);
31
	}
32
33
	public function offsetSet($key, $value) {
34
		$keys = $this->primaryKey;
35
		$obj = $this;
36
		$key1 = array_pop($keys);
37
		$value->$key1 = $key;
38
39
		while ($key = array_pop($keys)) {
40
			$value->$key = $obj->lookup;
41
			$obj = $obj->parent;
42
		}
43
		$this->mapper[] = $value;
44
	}
45
46
	public function offsetUnset($key) {
47
		$keys = $this->primaryKey;
48
		$this->mapper->filter([ array_pop($keys) => $key])->delete();
49
	}
50
51
	public function offsetExists($key) {
52
		return $this->offsetGet($key);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->offsetGet($key) also could return the type Maphper\MultiPk which is incompatible with the return type mandated by ArrayAccess::offsetExists() of boolean.
Loading history...
53
	}
54
}
55