Passed
Push — master ( 693d98...8cf93f )
by Tom
02:05
created

Entity   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 28
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 28
rs 10
wmc 8

3 Methods

Rating   Name   Duplication   Size   Complexity  
A create() 0 5 2
A __construct() 0 2 1
B wrap() 0 11 5
1
<?php
2
// C
3
namespace Maphper\Lib;
4
class Entity {
5
	//Creates an object of type $className and populates it with data
6
7
	private $className;
8
	private $parent;
9
10
	public function __construct(\Maphper\Maphper $parent, $className = null) {
0 ignored issues
show
Unused Code introduced by
The parameter $parent is not used and could be removed. ( Ignorable by Annotation )

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

10
	public function __construct(/** @scrutinizer ignore-unused */ \Maphper\Maphper $parent, $className = null) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
11
		$this->className = $className;
12
	}
13
	
14
	public function create($data = [], $relations = [], $siblings = []) {
15
		$obj = (is_callable($this->className)) ? call_user_func($this->className) : new $this->className;
16
		$writer = new PropertyWriter($obj);
17
		$writer->write($data);
18
		return $this->wrap($relations, $obj, $siblings);
19
	}
20
21
	public function wrap($relations, $object, $siblings = []) {
22
		//see if any relations need overwriting
23
		foreach ($relations as $name => $relation) {
24
			if (isset($object->$name) && !($object->$name instanceof \Maphper\Relation) ) {
25
				//After overwriting the relation, does the parent object ($object) need overwriting as well?
26
				if ($relation->overwrite($object, $object->$name)) $this->parent[] = $object;
27
			}
28
29
			$object->$name = $relation->getData($object, $siblings);
30
		}
31
		return $object;
32
	}
33
}