Completed
Branch 0.3.0 (b16461)
by Anton
04:03
created

Form   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 36
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
c 1
b 0
f 0
lcom 1
cbo 4
dl 0
loc 36
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A displayError() 0 10 2
B handle() 0 18 5
1
<?php
2
3
namespace Utils {
4
5
	use Utils\Messages, Utils\Popup, Language;
6
7
	abstract class Form extends \Form {
8
9
		# Display error
10
11
		private function displayError(string $code, bool $popup) {
12
13
			$text = Language::get($code);
14
15
			if (!$popup) Messages::set('error', $text); else Popup::set('negative', $text);
16
17
			# ------------------------
18
19
			return false;
20
		}
21
22
		# Handle form
23
24
		public function handle(callable $callback, bool $popup = false) {
25
26
			if (false === ($post = $this->post())) return false;
27
28
			# Check form for errors
29
30
			if ($this->errors()) return $this->displayError('FORM_ERROR_REQUIRED', $popup);
31
32
			# Call controller method
33
34
			if (is_string($result = $callback($post))) return $this->displayError($result, $popup);
35
36
			if (is_array($result)) { $this->get($result[0])->error(true); return $this->displayError($result[1], $popup); }
37
38
			# ------------------------
39
40
			return (true === $result);
41
		}
42
	}
43
}
44