Completed
Push — master ( 416cb1...222de5 )
by Anton
03:44
created

Init::init()   D

Complexity

Conditions 10
Paths 12

Size

Total Lines 44
Code Lines 15

Duplication

Lines 1
Ratio 2.27 %
Metric Value
dl 1
loc 44
rs 4.8197
cc 10
eloc 15
nc 12
nop 2

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Modules\Entitizer\Utils\Entity\Action {
4
5
	use DB;
6
7
	trait Init {
8
9
		protected $definition = null, $error = false, $id = 0, $data = [];
10
11
		# Init entity
12
13
		public function init($value, string $name = 'id') {
14
15
			if (0 !== $this->id) return false;
16
17
			# Check param name
18
19
			if ($name === 'id') $param = $this->definition->id();
20
21 View Code Duplication
			else if ((false === ($param = $this->definition->get($name))) || !$param->unique()) return false;
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
22
23
			# Select entity from DB
24
25
			$selection = array_merge(['id'], array_keys($this->definition->params()));
26
27
			$name = $param->name(); $value = $param->cast($value);
28
29
			DB::select(static::$table, $selection, [$name => $value], null, 1);
30
31
			if (($this->error = !(DB::last() && DB::last()->status)) || (DB::last()->rows !== 1)) return false;
32
33
			$data = DB::last()->row();
34
35
			# Cast data
36
37
			$this->id = $this->definition->id()->cast($data['id']);
38
39
			foreach ($this->definition->params() as $name => $param) $this->data[$name] = $param->cast($data[$name]);
40
41
			# Init path
42
43
			if (static::$nesting) $this->data['path'] = $this->getPath();
44
45
			# Implement entity
46
47
			$this->implement();
48
49
			# Cache entity
50
51
			self::$cache[static::$type][$this->id] = $this;
52
53
			# ------------------------
54
55
			return true;
56
		}
57
58
		# Get path interface
59
60
		abstract protected function getPath();
61
62
		# Implementor interface
63
64
		abstract protected function implement();
65
	}
66
}
67