Passed
Push — 0.3.0 ( 0d73f1...9a02d7 )
by Anton
04:10
created

Modify::edit()   B

Complexity

Conditions 6
Paths 7

Size

Total Lines 26
Code Lines 9

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 26
rs 8.439
cc 6
eloc 9
nc 7
nop 1
1
<?php
2
3
namespace Modules\Entitizer\Utils\Entity {
4
5
	use Modules\Entitizer, DB;
6
7
	trait Modify {
8
9
		protected $definition = null, $dataset = null;
10
11
		# Init entity subtree connection
12
13
		private function initSubtree() {
14
15
			$data = ['ancestor' => $this->id, 'descendant' => $this->id, 'depth' => 0];
0 ignored issues
show
Bug introduced by
The property id does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
16
17
			DB::insert(static::$table_relations, $data, false, true);
18
19
			# ------------------------
20
21
			return (DB::last() && DB::last()->status);
22
		}
23
24
		# Disconnect subtree from current position
25
26 View Code Duplication
		private function disconnectSubtree() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
27
28
			$query = ("DELETE rla FROM " . static::$table_relations . " rla ") .
29
30
			         ("JOIN "  . static::$table_relations . " rlb ON rlb.descendant = rla.descendant ") .
31
32
			         ("LEFT JOIN " . static::$table_relations . " rlx ") .
33
34
			         ("ON rlx.ancestor = rlb.ancestor AND rlx.descendant = rla.ancestor ") .
35
36
			         ("WHERE rlb.ancestor = " . $this->id . " AND rlx.ancestor IS NULL");
37
38
			if (!(DB::send($query) && DB::last()->status)) return false;
39
40
			# Set path
41
42
			$this->dataset->update(['parent_id' => 0]);
43
44
			# ------------------------
45
46
			return true;
47
		}
48
49
		# Connect subtree under new position
50
51 View Code Duplication
		private function connectSubtree(int $parent_id) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
52
53
			$query = ("INSERT INTO " . static::$table_relations . " (ancestor, descendant, depth) ") .
54
55
			         ("SELECT sup.ancestor, sub.descendant, sup.depth + sub.depth + 1 ") .
56
57
			         ("FROM " . static::$table_relations . " sup ") .
58
59
			         ("JOIN " . static::$table_relations . " sub ") .
60
61
			         ("WHERE sub.ancestor = " . $this->id . " AND sup.descendant = " . $parent_id);
62
63
			if (!(DB::send($query) && (DB::last()->rows > 0))) return false;
64
65
			# Set path
66
67
			$this->dataset->update(['parent_id' => $parent_id]);
68
69
			# ------------------------
70
71
			return true;
72
		}
73
74
		# Create entity entry in DB
75
76
		public function create(array $data) {
77
78
			if (0 !== $this->id) return false;
79
80
			$data = $this->dataset->cast($data);
81
82
			if (static::$auto_increment && isset($data['id'])) $data['id'] = 0;
83
84
			# Insert entity
85
86
			DB::insert(static::$table, $data);
87
88
			if (!(DB::last() && DB::last()->status)) return false;
89
90
			# Update data
91
92
			if (static::$auto_increment) $data['id'] = DB::last()->id;
93
94
			$this->dataset->update($data);
95
96
			# Init subtreee
97
98
			if (static::$nesting) $this->initSubtree();
99
100
			# Cache entity
101
102
			self::$cache[static::$table][$this->id] = $this;
103
104
			# ------------------------
105
106
			return true;
107
		}
108
109
		# Edit entity entry in DB
110
111
		public function edit(array $data) {
112
113
			if (0 === $this->id) return false;
114
115
			$data = $this->dataset->cast($data);
116
117
			if (isset($data['id'])) unset($data['id']);
118
119
			# Update entity
120
121
			DB::update(static::$table, $data, ['id' => $this->id]);
122
123
			if (!(DB::last() && DB::last()->status)) return false;
124
125
			# Update data
126
127
			$this->dataset->update($data);
128
129
			# Init subtreee
130
131
			if (static::$nesting) $this->initSubtree();
132
133
			# ------------------------
134
135
			return true;
136
		}
137
138
		# Move entity to new parent
139
140
		public function move(int $parent_id) {
141
142
			if (0 === $this->id) return false;
143
144
			# Re-connect entity if not in tree
145
146
			if (!$this->initSubtree()) return false;
147
148
			# Create parent entity
149
150
			if (0 !== ($parent = Entitizer::get(static::$table, $parent_id))->id) {
151
152
				if (false === ($path = $parent->path())) return false;
153
154
				if (false === ($depth = $this->subtreeDepth())) return false;
0 ignored issues
show
Bug introduced by
It seems like subtreeDepth() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
155
156
				if ((count($path) + $depth + 1) > CONFIG_ENTITIZER_MAX_DEPTH) return false;
157
			}
158
159
			# Disconnect subtree from current position
160
161
			if (!$this->disconnectSubtree()) return false;
162
163
			# Connect subtree under new position
164
165
		 	if (0 !== $parent->id) $this->connectSubtree($parent->id);
166
167
			# ------------------------
168
169
			return true;
170
		}
171
172
		# Remove entity entry from DB
173
174
		public function remove() {
175
176
			if (0 === $this->id) return false;
177
178
			# Check if entity is removable
179
180
			if (static::$super && ($this->id === 1)) return false;
181
182
			if (static::$nesting && (0 !== $this->subtreeCount())) return false;
0 ignored issues
show
Bug introduced by
It seems like subtreeCount() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
183
184
			# Remove entity
185
186
			DB::delete(static::$table, ['id' => $this->id]);
187
188
			if (!(DB::last() && DB::last()->status)) return false;
189
190
			# Uncache entity
191
192
			unset(self::$cache[static::$table][$this->id]);
193
194
			# Reset data
195
196
			$this->dataset->reset();
197
198
			# ------------------------
199
200
			return true;
201
		}
202
	}
203
}
204