Passed
Push — master ( 7da913...a3998d )
by Jean-Christophe
05:48
created

RestControllerUtilitiesTrait::getMany_()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

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