Skip to content

Commit

Permalink
Merge pull request #15 from productsupcom/allow-cloning-of-client
Browse files Browse the repository at this point in the history
Support cloning of ProductData Service
  • Loading branch information
m038-pup authored Dec 19, 2019
2 parents b9d7c49 + fabc094 commit 9f7836a
Showing 1 changed file with 30 additions and 2 deletions.
32 changes: 30 additions & 2 deletions lib/Productsup/Service/ProductData.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,17 +54,39 @@ class ProductData extends Service {
*/
public function __construct(Client $Client, $useShutdownHandler = true) {
parent::__construct($Client);
$this->createBatchId();
if($useShutdownHandler) {
register_shutdown_function(array($this, 'shutdownHandler'));
}
}

/**
* @return \Productsup\Service\ProductData
* @throws \Productsup\Exceptions\ClientException
*/
public function __clone()
{
if ($this->_batchId) {
throw new Exceptions\ClientException('a batch id is already generated, please create a new instance');
}
if (!empty($this->_productData)) {
throw new Exceptions\ClientException('products are added already, please create a new instance');
}
if ($this->didSubmit) {
throw new Exceptions\ClientException('the current batch is already submitted, please create a new instance');
}
if ($this->finished) {
throw new Exceptions\ClientException('the current batch is already finished, please create a new instance');
}
}


/**
* creates a new batch id for multi-request submits
*/
private function createBatchId() {
$this->_batchId = md5(microtime().uniqid());
if (is_null($this->_batchId)) {
$this->_batchId = md5(microtime() . uniqid());
}
}

public function disableDiscards() {
Expand Down Expand Up @@ -129,6 +151,7 @@ public function commit() {
throw new Exceptions\ClientException('no data submitted yet');
}

$this->createBatchId();
$request = $this->getRequest();
$request->method = Request::METHOD_POST;
$request->postBody = array(
Expand All @@ -150,6 +173,10 @@ public function discard() {
if($this->disableDiscards) {
throw new Exceptions\ClientException('discards were disabled, but tried to send anyway');
}
if(!$this->didSubmit) {
throw new Exceptions\ClientException('no data submitted yet');
}

$request = $this->getRequest();
$request->method = Request::METHOD_POST;
$request->url .= '/discard';
Expand Down Expand Up @@ -224,6 +251,7 @@ private function _submit() {
if(count($this->_productData) == 0) { // no data, do not send request
return array();
}
$this->createBatchId();
$this->didSubmit = true;
$request = $this->getRequest();
$request->method = Request::METHOD_POST;
Expand Down

0 comments on commit 9f7836a

Please sign in to comment.