Test Failed
Branch master (5614f0)
by Jean-Christophe
07:11
created

SimpleRestController::add()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 2
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Ubiquity\controllers\rest;
4
5
use Ubiquity\cache\CacheManager;
6
use Ubiquity\orm\DAO;
7
8
/**
9
 * Abstract base class for Rest controllers.
10
 * Ubiquity\controllers\rest$SimpleRestController
11
 * This class is part of Ubiquity
12
 *
13
 * @author jcheron <[email protected]>
14
 * @version 1.0.0
15
 *
16
 */
17
class SimpleRestController extends RestBaseController implements HasResourceInterface {
18
19
	public function initialize() {
20
		$thisClass = \get_class ( $this );
21
		if (! isset ( $this->model ))
22
			$this->model = CacheManager::getRestResource ( $thisClass );
23
		if (! isset ( $this->model )) {
24
			$modelsNS = $this->config ["mvcNS"] ["models"];
25
			$this->model = $modelsNS . "\\" . $this->_getResponseFormatter ()->getModel ( $thisClass );
26
		}
27
		parent::initialize ();
28
	}
29
30
	/**
31
	 * Returns all objects for the resource $model
32
	 */
33
	public function _index() {
34
		$datas = DAO::getAll ( $this->model );
35
		echo $this->_getResponseFormatter ()->get ( $datas );
36
	}
37
38
	/**
39
	 * Returns all the instances from the model $this->model.
40
	 * Query parameters:
41
	 * - **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).
42
	 * - **filter**: The filter to apply to the query (where part of an SQL query) (default: 1=1).
43
	 * - **page[number]**: The page to display (in this case, the page size is set to 1).
44
	 * - **page[size]**: The page size (count of instance per page) (default: 1).
45
	 *
46
	 * @route("/","methods"=>["get"],"priority"=>0)
47
	 */
48
	public function getAll_() {
49
		$filter = $this->getRequestParam ( 'filter', '1=1' );
50
		$pages = null;
51
		if (isset ( $_GET ['page'] )) {
52
			$pageNumber = $_GET ['page'] ['number'];
53
			$pageSize = $_GET ['page'] ['size'] ?? 1;
54
			$pages = $this->generatePagination ( $filter, $pageNumber, $pageSize );
55
		}
56
		$datas = DAO::getAll ( $this->model, $filter, $this->getInclude ( $this->getRequestParam ( 'include', true ) ) );
57
		echo $this->_getResponseFormatter ()->get ( $datas, $pages );
58
	}
59
60
	/**
61
	 * Get the first object corresponding to the $keyValues
62
	 * Query parameters:
63
	 * - **include**: A string of associated members to load, comma separated (e.g.
64
	 * users,groups,organization...), or a boolean: true for all members, false for none (default: true).
65
	 *
66
	 * @param string $id primary key(s) value(s) or condition
67
	 * @route("{id}/","methods"=>["get"],"priority"=>1000)
68
	 */
69
	public function getOne($id) {
70
		$this->_getOne ( $id, $this->getRequestParam ( 'include', true ) );
71
	}
72
73
	/**
74
	 * Update an instance of $model selected by the primary key $keyValues
75
	 * Require members values in $_POST array
76
	 * Requires an authorization with access token
77
	 *
78
	 * @param array $keyValues
79
	 * @authorization
80
	 * @route("methods"=>["patch"])
81
	 */
82
	public function update(...$keyValues) {
83
		$this->_update ( $keyValues );
84
	}
85
86
	/**
87
	 * Insert a new instance of $model
88
	 * Require members values in $_POST array
89
	 * Requires an authorization with access token
90
	 *
91
	 * @authorization
92
	 * @route("methods"=>["post"])
93
	 */
94
	public function add() {
95
		$this->_add ();
96
	}
97
98
	/**
99
	 * Delete the instance of $model selected by the primary key $keyValues
100
	 * Requires an authorization with access token
101
	 *
102
	 * @param array $keyValues
103
	 * @route("methods"=>["delete"],"priority"=>30)
104
	 * @authorization
105
	 */
106
	public function delete(...$keyValues) {
107
		$this->_delete ( $keyValues );
108
	}
109
}
110