Passed
Push — master ( 9c1274...95a447 )
by Jean-Christophe
06:00
created

RestControllerUtilitiesTrait::generatePagination()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 13
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
eloc 10
dl 0
loc 13
ccs 0
cts 11
cp 0
rs 9.9332
c 0
b 0
f 0
cc 3
nc 4
nop 3
crap 12
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
	protected function generatePagination(&$filter,$pageNumber,$pageSize){
35
			$count=DAO::count($this->model,$filter);
36
			$pagesCount=ceil($count/$pageSize);
37
			$pages=['self'=>$pageNumber,'first'=>1,'last'=>$pagesCount,'pageSize'=>$pageSize];
38
			if($pageNumber-1>0){
39
				$pages['prev']=$pageNumber-1;
40
			}
41
			if($pageNumber+1<=$pagesCount){
42
				$pages['next']=$pageNumber+1;
43
			}
44
			$offset=($pageNumber-1)*$pageSize;
45
			$filter.=' limit '.$offset.','.$pageSize;
46
			return $pages;
47
	}
48
49 5
	protected function _getResponseFormatter() {
50 5
		if (! isset ( $this->responseFormatter )) {
51 5
			$this->responseFormatter = $this->getResponseFormatter ();
52
		}
53 5
		return $this->responseFormatter;
54
	}
55
56
	/**
57
	 * To override, returns the active formatter for the response
58
	 *
59
	 * @return \Ubiquity\controllers\rest\ResponseFormatter
60
	 */
61 4
	protected function getResponseFormatter(): ResponseFormatter {
62 4
		return new ResponseFormatter ();
63
	}
64
65 5
	protected function _getRestServer() {
66 5
		if (! isset ( $this->server )) {
67 5
			$this->server = $this->getRestServer ();
68
		}
69 5
		return $this->server;
70
	}
71
72
	/**
73
	 * To override, returns the active RestServer
74
	 *
75
	 * @return \Ubiquity\controllers\rest\RestServer
76
	 */
77 5
	protected function getRestServer(): RestServer {
78 5
		return new RestServer ( $this->config );
79
	}
80
81 2
	protected function connectDb($config) {
82 2
		$db = $config ["database"];
83 2
		if ($db ["dbName"] !== "") {
84 2
			DAO::connect ( $db ["type"], $db ["dbName"], @$db ["serverName"], @$db ["port"], @$db ["user"], @$db ["password"], @$db ["options"], @$db ["cache"] );
85
		}
86 2
	}
87
88
	/**
89
	 * Updates $instance with $values
90
	 * To eventually be redefined in derived classes
91
	 *
92
	 * @param object $instance
93
	 *        	the instance to update
94
	 * @param array|null $values
95
	 */
96
	protected function _setValuesToObject($instance, $values = null) {
97
		if (URequest::isJSON ()) {
98
			$values = \json_decode ( $values, true );
99
		}
100
		URequest::setValuesToObject ( $instance, $values );
101
	}
102
103
	/**
104
	 *
105
	 * @param string|boolean $included
106
	 * @return array|boolean
107
	 */
108 2
	protected function getIncluded($included) {
109 2
		if (! UString::isBooleanStr ( $included )) {
110
			return explode ( ",", $included );
111
		}
112 2
		return UString::isBooleanTrue ( $included );
113
	}
114
115
	/**
116
	 *
117
	 * @param callable $getDatas
118
	 * @param string $member
119
	 * @param boolean|string $included
120
	 *        	if true, loads associate members with associations, if string, example : client.*,commands
121
	 * @param boolean $useCache
122
	 * @throws \Exception
123
	 */
124
	protected function getMany_($getDatas, $member, $included = false, $useCache = false) {
125
		if (isset ( $_SESSION ["_restInstance"] )) {
126
			$included = $this->getIncluded ( $included );
127
			$useCache = UString::isBooleanTrue ( $useCache );
128
			$datas = $getDatas ( $_SESSION ["_restInstance"], $member, $included, $useCache );
129
			echo $this->_getResponseFormatter ()->get ( $datas );
130
		} else {
131
			throw new \Exception ( "You have to call getOne before calling getManyToMany or getOneToMany." );
132
		}
133
	}
134
}
135
136