Skip to content

Commit

Permalink
bulk-delete with tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
JoeZ99 committed Dec 5, 2016
1 parent 60aa060 commit 9790641
Show file tree
Hide file tree
Showing 5 changed files with 361 additions and 323 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.txt
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,5 @@ Version 5.
- Added phpunit tests
- Added phpunit tests
- some small refactoring
- v5.5.1
- added deleteItems method
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -489,7 +489,7 @@ $mySearchEngine->deleteItem('product', 'newid');
$mySearchEngine->updateItem('product', '888493', array('title'=>'modifiled title'));
```

##### Bulk Add/Update
##### Bulk Add/Update/Delete

```php
$mySearchEngine->updateItems('product', array(
Expand All @@ -501,6 +501,8 @@ $mySearchEngine->addItems('product', array(
array('title' => 'first item', 'id' => 'newid1'),
array('title' => 'second item'),
));

$mySearchEngine->deleteItems('product', array('id1', 'id2'));
```

##### Iterating Items
Expand Down
4 changes: 1 addition & 3 deletions src/Management/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,9 @@ public function managementApiCall($method = 'GET', $entryPoint = '', $params = n
$url .= '?'.http_build_query($params);
}

if (!in_array($method, array('POST', 'PUT'))){
if (!in_array($method, array('POST', 'PUT', 'DELETE'))){
$data = null;
}

$serverResponse = $this->talkToServer($method, $url, $headers, $data);
$statusCode = $serverResponse['statusCode'];
$contentResponse = $serverResponse['contentResponse'];
Expand Down Expand Up @@ -120,7 +119,6 @@ public function getSearchEngines(){

protected function talkToServer($method, $url, $headers, $data)
{

$session = curl_init($url);
curl_setopt($session, CURLOPT_CUSTOMREQUEST, $method);
curl_setopt($session, CURLOPT_HEADER, false);
Expand Down
16 changes: 16 additions & 0 deletions src/Management/SearchEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,22 @@ public function deleteItem($datatype, $itemId) {
return $result['statusCode'] == 204 ;
}

/**
* Delete items in bulk (up to 100)
*
* @param string $datatype type of the items
* @param array list of item ids to be deleted
* @return array assoc array with both items which could be deleted and couldn't
* array('errors'=>array(), 'success'=>array('AX01', 'AX02', 'AXFD')
*/
public function deleteItems($datatype, $itemsIds) {
$objects = array_map(function($id){return array('id'=>$id);}, $itemsIds);
$result = $this->client->managementApiCall(
'DELETE', "{$this->hashid}/items/{$datatype}", null, json_encode($objects)
);
return $result['response'];
}

/**
* Obtain stats aggregated data for a certain period.
*
Expand Down
Loading

0 comments on commit 9790641

Please sign in to comment.