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

Remove::implement()

Size

Total Lines 1

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 1
nc 1
1
<?php
2
3
namespace Modules\Entitizer\Utils\Entity\Action {
4
5
	use DB;
6
7
	trait Remove {
8
9
		protected $definition = null, $error = false, $id = 0, $data = [];
10
11
		# Remove entity entry from DB
12
13
		public function remove() {
14
15
			if (0 === $this->id) return false;
16
17
			# Check if entity is removable
18
19
			if (static::$super && ($this->id === 1)) return false;
20
21
			if (static::$nesting && (0 !== $this->children())) return false;
22
23
			# Remove extension entries
24
25
			foreach (static::$extensions as $extension) {
26
27
				$entity = self::get($extension, $this->id);
28
29
				if ($entity->error() || ((0 !== $entity->id) && !$entity->remove())) return false;
30
			}
31
32
			# Remove entity
33
34
			DB::delete(static::$table, ['id' => $this->id]);
35
36
			if (!(DB::last() && DB::last()->status)) return false;
37
38
			# Uncache entity
39
40
			if (self::$cache[static::$type][$this->id] === $this) unset(self::$cache[static::$type][$this->id]);
41
42
			# Reset id
43
44
			$this->id = 0;
45
46
			# Reset data array
47
48
			foreach ($this->definition->params() as $name => $param) $this->data[$name] = $param->cast(null);
49
50
			# Reset path
51
52
			if (static::$nesting) $this->data['path'] = [];
53
54
			# Implement entity
55
56
			$this->implement();
57
58
			# ------------------------
59
60
			return true;
61
		}
62
63
		# Get dataset interface
64
65
		abstract protected function children();
66
67
		# Implementor interface
68
69
		abstract protected function implement();
70
	}
71
}
72