Passed
Push — master ( b9acc3...1dafdc )
by Jean-Christophe
03:25
created

RestControllerUtilitiesTrait::_setValuesToObject()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

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