command   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 14
eloc 26
dl 0
loc 59
c 0
b 0
f 0
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A argument() 0 7 3
A log() 0 15 4
A after_connstruct() 0 1 1
A before_run() 0 1 1
A after_run() 0 1 1
A execute() 0 8 2
A __construct() 0 3 1
A run() 0 1 1
1
<?php
2
3
/**
4
 * © 2018 - Phoponent
5
 * Author: Nicolas Choquet
6
 * Email: [email protected]
7
 * LICENSE GPL ( GNU General Public License )
8
 */
9
10
namespace phoponent\framework\traits;
11
12
use Exception;
13
14
trait command {
15
	private $argv = [];
16
17
	protected static $LOG_ECHO = 'echo';
18
	protected static $LOG_FILE = '';
19
20
	public function __construct($argv = []) {
21
		$this->argv = $argv;
22
		$this->after_connstruct();
23
	}
24
25
	protected function after_connstruct() {}
26
27
	protected function log($text = '', $type_log = 'echo') {
28
		if ($type_log === self::$LOG_FILE) {
29
			$path = str_replace('/'.basename(self::$LOG_FILE), '', self::$LOG_FILE);
30
			if(!is_dir($path)) {
31
				mkdir($path, 0777, true);
32
			}
33
			$content = '';
34
			if(is_file(self::$LOG_FILE)) {
35
				$content = file_get_contents(self::$LOG_FILE);
36
			}
37
38
			file_put_contents(self::$LOG_FILE, $content."\n".date('#~ '.exec('echo $USER').': '.$text));
39
		}
40
		else {
41
			echo date('Y-m-d H:i:s').' '.exec('echo $USER').' #~ '.$text."\n";
42
		}
43
	}
44
45
	protected function before_run() {}
46
47
	protected function after_run() {}
48
49
	protected function run() {}
50
51
	protected function argument($name) {
52
		foreach ($this->argv as $argv) {
53
			if(explode('=', $argv)[0] === $name) {
54
				return explode('=', $argv)[1];
55
			}
56
		}
57
		return null;
58
	}
59
60
	/**
61
	 * @param string $method
62
	 * @return bool
63
	 * @throws Exception
64
	 */
65
	public function execute($method = 'run') {
66
		$this->before_run();
67
		if(in_array($method, get_class_methods(get_class($this)))) {
68
			$this->$method();
69
			$this->after_run();
70
			return true;
71
		}
72
		throw new Exception("method ".explode('\\', __CLASS__)[count(explode('\\', __CLASS__))-1]."::{$method}() not found");
73
	}
74
}