Completed
Push — master ( 5251c4...38d2db )
by Jean-Christophe
01:38
created

RestControllerUtilitiesTrait::operate_()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

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