Passed
Branch master (84f592)
by Jean-Christophe
11:35
created

DynamicResourceTrait::getOne_()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 2
c 0
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 2
crap 1
1
<?php
2
3
namespace Ubiquity\controllers\rest\traits;
4
5
use Ubiquity\controllers\crud\CRUDHelper;
6
use Ubiquity\controllers\rest\RestError;
7
use Ubiquity\controllers\Startup;
8
use Ubiquity\orm\DAO;
9
use Ubiquity\orm\OrmUtils;
10
11
/**
12
 * Ubiquity\controllers\rest\traits$DynamicResourceTrait
13
 * This class is part of Ubiquity
14
 *
15
 * @author jc
16
 * @version 1.0.0
17
 * 
18
 * @property string $model
19
 */
20
trait DynamicResourceTrait {
21
	
22
	abstract protected function _getResponseFormatter();
23
	
24
	abstract public function _setResponseCode($value);
25
	
26
	abstract public function _format($arrayMessage);
27
	
28
	abstract public function _getAll();
29
	
30
	abstract public function _getRelationShip($id, $member);
31
	
32
	abstract public function _getOne($keyValues, $include = false, $useCache = false);
33
34
	abstract protected function getRequestParam($param, $default);
35
	
36
	abstract protected function hasErrors();
37
	
38
	abstract protected function displayErrors();
39
	
40
	abstract public function _delete(...$keyValues);
41
	
42 1
	protected function updateOperation($instance, $datas, $updateMany = false) {
43 1
		$instance->_new = false;
44 1
		return CRUDHelper::update ( $instance, $datas, false, $updateMany );
45
	}
46
47 1
	protected function addOperation($instance, $datas, $insertMany = false) {
48 1
		$instance->_new = true;
49 1
		return CRUDHelper::update ( $instance, $datas, false, $insertMany );
50
	}
51
52 3
	protected function _setResource($resource) {
53 3
		$modelsNS = $this->config ["mvcNS"] ["models"];
54 3
		$this->model = $modelsNS . "\\" . $this->_getResponseFormatter ()->getModel ( $resource );
55 3
	}
56
57 3
	protected function _checkResource($resource, $callback) {
58 3
		$this->_setResource ( $resource );
59 3
		if (\class_exists ( $this->model )) {
60 3
			$callback ();
61
		} else {
62
			$this->_setResponseCode ( 404 );
63
			$error = new RestError ( 404, "Not existing class", $this->model . " class does not exists!", Startup::getController () . "/" . Startup::getAction () );
64
			echo $this->_format ( $error->asArray () );
65
		}
66 3
	}
67
68
	/**
69
	 * Returns all the instances from the model $resource.
70
	 */
71 1
	public function getAll_($resource) {
72
		$this->_checkResource ( $resource, function () {
73 1
			$this->_getAll ();
74 1
		} );
75 1
	}
76
77
	/**
78
	 * Returns an associated member value(s).
79
	 * Query parameters:
80
	 * - **include**: A string of associated members to load, comma separated (e.g. users,groups,organization...), or a boolean: true for all members, false for none (default: true).
81
	 *
82
	 * @param string $resource The resource (model) to use
83
	 * @param string $id The primary key value(s), if the primary key is composite, use a comma to separate the values (e.g. 1,115,AB)
84
	 * @param string $member The member to load
85
	 */
86
	public function getRelationShip_($resource, $id, $member) {
87
		$this->_checkResource ( $resource, function () use ($id, $member) {
88
			$this->_getRelationShip ( $id, $member );
89
		} );
90
	}
91
92
	/**
93
	 * Returns an instance of $resource, by primary key $id.
94
	 *
95
	 * @param string $resource The resource (model) to use
96
	 * @param string $id The primary key value(s), if the primary key is composite, use a comma to separate the values (e.g. 1,115,AB)
97
	 *
98
	 */
99 2
	public function getOne_($resource, $id) {
100
		$this->_checkResource ( $resource, function () use ($id) {
101 2
			$this->_getOne ( $id, $this->getRequestParam ( 'include', false ), false );
102 2
		} );
103 2
	}
104
105
	/**
106
	 * Inserts a new instance of $resource.
107
	 * Data attributes are send in data[attributes] request body (in JSON format)
108
	 *
109
	 * @param string $resource The resource (model) to use
110
	 */
111 1
	public function add_($resource) {
112
		$this->_checkResource ( $resource, function () {
113 1
			parent::_add ();
114 1
		} );
115 1
	}
116
117
	/**
118
	 * Updates an existing instance of $resource.
119
	 * Data attributes are send in data[attributes] request body (in JSON format)
120
	 *
121
	 * @param string $resource The resource (model) to use
122
	 *
123
	 */
124 1
	public function update_($resource, ...$id) {
125
		$this->_checkResource ( $resource, function () use ($id) {
126 1
			if (! $this->hasErrors ()) {
127 1
				parent::_update ( ...$id );
128
			} else {
129
				echo $this->displayErrors ();
130
			}
131 1
		} );
132 1
	}
133
134
	/**
135
	 * Deletes an existing instance of $resource.
136
	 *
137
	 * @param string $resource The resource (model) to use
138
	 * @param string $ids The primary key value(s), if the primary key is composite, use a comma to separate the values (e.g. 1,115,AB)
139
	 *
140
	 */
141 1
	public function delete_($resource, ...$id) {
142
		$this->_checkResource ( $resource, function () use ($id) {
143 1
			$this->_delete ( ...$id );
144 1
		} );
145
	}
146
}