Skip to content

Commit 7659da0

Browse files
committed
Refactor to remove some duplication.
1 parent f12eda1 commit 7659da0

File tree

1 file changed

+36
-31
lines changed

1 file changed

+36
-31
lines changed

src/OdooClient.php

Lines changed: 36 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -196,14 +196,8 @@ public function search(
196196
$limit = 100,
197197
$order = ''
198198
) {
199-
$msg = new Request('execute');
200-
201-
$msg->addParam($this->stringValue($this->database));
202-
$msg->addParam($this->intValue($this->getUserId()));
203-
$msg->addParam($this->stringValue($this->password));
199+
$msg = $this->getBaseObjectRequest($modelName, 'search');
204200

205-
$msg->addParam($this->stringValue($modelName));
206-
$msg->addParam($this->stringValue('search'));
207201
$msg->addParam($this->objectifyArray($criteria));
208202

209203
$msg->addParam($this->intValue($offset)); // offset
@@ -223,14 +217,8 @@ public function searchCount(
223217
string $modelName,
224218
array $criteria = []
225219
) {
226-
$msg = new Request('execute');
227-
228-
$msg->addParam($this->stringValue($this->database));
229-
$msg->addParam($this->intValue($this->getUserId()));
230-
$msg->addParam($this->stringValue($this->password));
220+
$msg = $this->getBaseObjectRequest($modelName, 'search_count');
231221

232-
$msg->addParam($this->stringValue($modelName));
233-
$msg->addParam($this->stringValue('search_count'));
234222
$msg->addParam($this->objectifyArray($criteria));
235223

236224
$response = $this->getXmlRpcClient('object')->send($msg);
@@ -249,17 +237,11 @@ public function searchRead(
249237
$limit = 100,
250238
$order = ''
251239
) {
252-
$msg = new Request('execute');
240+
$msg = $this->getBaseObjectRequest($modelName, 'search_read');
253241

254-
$msg->addParam($this->stringValue($this->database));
255-
$msg->addParam($this->intValue($this->getUserId()));
256-
$msg->addParam($this->stringValue($this->password));
257-
258-
$msg->addParam($this->stringValue($modelName));
259-
$msg->addParam($this->stringValue('search_read'));
260242
$msg->addParam($this->objectifyArray($criteria));
261243

262-
// To be fixed when we have Odoo 8 available.
244+
// To be fixed when we have Odoo 8 available to develop against.
263245

264246
//$msg->addParam($this->stringValue('id'));
265247

@@ -280,14 +262,8 @@ public function read(
280262
string $modelName,
281263
array $criteria = []
282264
) {
283-
$msg = new Request('execute');
284-
285-
$msg->addParam($this->stringValue($this->database));
286-
$msg->addParam($this->intValue($this->getUserId()));
287-
$msg->addParam($this->stringValue($this->password));
265+
$msg = $this->getBaseObjectRequest($modelName, 'read');
288266

289-
$msg->addParam($this->stringValue($modelName));
290-
$msg->addParam($this->stringValue('read'));
291267
$msg->addParam($this->objectifyArray($criteria));
292268

293269
$response = $this->getXmlRpcClient('object')->send($msg);
@@ -296,9 +272,38 @@ public function read(
296272
}
297273

298274
//
299-
// TODO: create write unlink
275+
// TODO: actions to implement = create write unlink
300276
//
301277

278+
/**
279+
* Return a message with the base parameters for any object call.
280+
* Identified the login credentials, model and action.
281+
*
282+
* @param string|null $modelName
283+
* @param string|null $action will be used only the $modelName is provided
284+
* @return Request
285+
*/
286+
public function getBaseObjectRequest(
287+
string $modelName = null,
288+
string $action = null
289+
) {
290+
$msg = new Request('execute');
291+
292+
$msg->addParam($this->stringValue($this->database));
293+
$msg->addParam($this->intValue($this->getUserId()));
294+
$msg->addParam($this->stringValue($this->password));
295+
296+
if ($modelName !== null) {
297+
$msg->addParam($this->stringValue($modelName));
298+
299+
if ($action !== null) {
300+
$msg->addParam($this->stringValue($action));
301+
}
302+
}
303+
304+
return $msg;
305+
}
306+
302307
/**
303308
* Walk through the criteria array and convert scalar values to
304309
* XML-RPC objects, and nested arrays to array and struct objects.
@@ -337,7 +342,7 @@ public function objectifyArray($item)
337342
}
338343

339344
// Map to an array or a struct, depending on whether a numeric
340-
// array or associative array.
345+
// keyed array or an associative array is to be encoded.
341346

342347
if ($item === [] || array_keys($item) === range(0, count($item) - 1)) {
343348
return $this->arrayValue($item);

0 commit comments

Comments
 (0)