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

Create::create()   D

Complexity

Conditions 9
Paths 9

Size

Total Lines 34
Code Lines 12

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 34
rs 4.9091
cc 9
eloc 12
nc 9
nop 1
1
<?php
2
3
namespace Modules\Entitizer\Utils\Entity\Action {
4
5
	use DB;
6
7
	trait Create {
8
9
		protected $definition = null, $error = false, $id = 0, $data = [];
10
11
		# Create entity entry in DB
12
13
		public function create(array $data) {
14
15
			if (0 !== $this->id) return false;
16
17
			$set = $this->getDataset($data, true);
18
19
			if (isset($set['id']) && !($set['id'] > 0)) return false;
20
21
			# Insert entity
22
23
			DB::insert(static::$table, $set);
24
25
			if (!(DB::last() && DB::last()->status)) return false;
26
27
			# Re-init entity
28
29
			$this->error = false; $this->id = DB::last()->id;
30
31
			foreach ($set as $name => $value) if ($name !== 'id') $this->data[$name] = $value;
32
33
			if (static::$nesting) $this->data['path'] = $this->getPath();
34
35
			# Implement entity
36
37
			$this->implement();
38
39
			# Cache entity
40
41
			self::$cache[static::$type][$this->id] = $this;
42
43
			# ------------------------
44
45
			return true;
46
		}
47
48
		# Get dataset interface
49
50
		abstract protected function getDataset(array $data, bool $initial = false);
51
52
		# Get path interface
53
54
		abstract protected function getPath();
55
56
		# Implementor interface
57
58
		abstract protected function implement();
59
	}
60
}
61