Completed
Push — master ( 506749...99ae87 )
by Jean-Christophe
03:59
created

RestControllerUtilitiesTrait   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 86
Duplicated Lines 0 %

Test Coverage

Coverage 29.73%

Importance

Changes 0
Metric Value
wmc 14
eloc 31
dl 0
loc 86
ccs 11
cts 37
cp 0.2973
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getResponseFormatter() 0 2 1
A connectDb() 0 4 2
A getIncluded() 0 5 2
A operate_() 0 12 3
A _setValuesToObject() 0 5 2
A _getResponseFormatter() 0 5 2
A getMany_() 0 8 2
1
<?php
2
3
namespace Ubiquity\controllers\rest;
4
5
use Ubiquity\orm\DAO;
6
use Ubiquity\utils\base\UString;
7
use Ubiquity\utils\http\URequest;
8
9
/**
10
 *
11
 * @author jc
12
 * @property ResponseFormatter $responseFormatter
13
 */
14
trait RestControllerUtilitiesTrait {
15
16
	abstract public function _setResponseCode($value);
17
18
	protected function operate_($instance, $callback, $status, $exceptionMessage, $keyValues) {
19
		if (isset ( $instance )) {
20
			$result = $callback ( $instance );
21
			if ($result) {
22
				$formatter = $this->_getResponseFormatter ();
23
				echo $formatter->format ( [ "status" => $status,"data" => $formatter->cleanRestObject ( $instance ) ] );
24
			} else {
25
				throw new \Exception ( $exceptionMessage );
26
			}
27
		} else {
28
			$this->_setResponseCode ( 404 );
29
			echo $this->_getResponseFormatter ()->format ( [ "message" => "No result found","keyValues" => $keyValues ] );
30
		}
31
	}
32
33 2
	protected function _getResponseFormatter() {
34 2
		if (! isset ( $this->responseFormatter )) {
35
			$this->responseFormatter = $this->getResponseFormatter ();
36
		}
37 2
		return $this->responseFormatter;
38
	}
39
40
	/**
41
	 * To override, returns the active formatter for the response
42
	 *
43
	 * @return \Ubiquity\controllers\rest\ResponseFormatter
44
	 */
45
	protected function getResponseFormatter() {
46
		return new ResponseFormatter ();
47
	}
48
49 1
	protected function connectDb($config) {
50 1
		$db = $config ["database"];
51 1
		if ($db ["dbName"] !== "") {
52 1
			DAO::connect ( $db ["type"], $db ["dbName"], @$db ["serverName"], @$db ["port"], @$db ["user"], @$db ["password"], @$db ["options"], @$db ["cache"] );
53
		}
54 1
	}
55
56
	/**
57
	 * Updates $instance with $values
58
	 * To eventually be redefined in derived classes
59
	 *
60
	 * @param object $instance
61
	 *        	the instance to update
62
	 * @param array|null $values
63
	 */
64
	protected function _setValuesToObject($instance, $values = null) {
65
		if (URequest::isJSON ()) {
66
			$values = \json_decode ( $values, true );
67
		}
68
		URequest::setValuesToObject ( $instance, $values );
69
	}
70
71
	/**
72
	 *
73
	 * @param string|boolean $included
74
	 * @return array|boolean
75
	 */
76 1
	private function getIncluded($included) {
77 1
		if (! UString::isBooleanStr ( $included )) {
78
			return explode ( ",", $included );
79
		}
80 1
		return UString::isBooleanTrue ( $included );
81
	}
82
83
	/**
84
	 *
85
	 * @param callable $getDatas
86
	 * @param string $member
87
	 * @param boolean|string $included
88
	 *        	if true, loads associate members with associations, if string, example : client.*,commands
89
	 * @param boolean $useCache
90
	 * @throws \Exception
91
	 */
92
	protected function getMany_($getDatas, $member, $included = false, $useCache = false) {
93
		if (isset ( $_SESSION ["_restInstance"] )) {
94
			$included = $this->getIncluded ( $included );
95
			$useCache = UString::isBooleanTrue ( $useCache );
96
			$datas = $getDatas ( $_SESSION ["_restInstance"], $member, $included, $useCache );
97
			echo $this->_getResponseFormatter ()->get ( $datas );
98
		} else {
99
			throw new \Exception ( "You have to call getOne before calling getManyToMany or getOneToMany." );
100
		}
101
	}
102
}
103
104