@@ -54,17 +54,39 @@ class ProductData extends Service {
5454 */
5555 public function __construct (Client $ Client , $ useShutdownHandler = true ) {
5656 parent ::__construct ($ Client );
57- $ this ->createBatchId ();
5857 if ($ useShutdownHandler ) {
5958 register_shutdown_function (array ($ this , 'shutdownHandler ' ));
6059 }
6160 }
6261
62+ /**
63+ * @return \Productsup\Service\ProductData
64+ * @throws \Productsup\Exceptions\ClientException
65+ */
66+ public function __clone ()
67+ {
68+ if ($ this ->_batchId ) {
69+ throw new Exceptions \ClientException ('a batch id is already generated, please create a new instance ' );
70+ }
71+ if (!empty ($ this ->_productData )) {
72+ throw new Exceptions \ClientException ('products are added already, please create a new instance ' );
73+ }
74+ if ($ this ->didSubmit ) {
75+ throw new Exceptions \ClientException ('the current batch is already submitted, please create a new instance ' );
76+ }
77+ if ($ this ->finished ) {
78+ throw new Exceptions \ClientException ('the current batch is already finished, please create a new instance ' );
79+ }
80+ }
81+
82+
6383 /**
6484 * creates a new batch id for multi-request submits
6585 */
6686 private function createBatchId () {
67- $ this ->_batchId = md5 (microtime ().uniqid ());
87+ if (is_null ($ this ->_batchId )) {
88+ $ this ->_batchId = md5 (microtime () . uniqid ());
89+ }
6890 }
6991
7092 public function disableDiscards () {
@@ -129,6 +151,7 @@ public function commit() {
129151 throw new Exceptions \ClientException ('no data submitted yet ' );
130152 }
131153
154+ $ this ->createBatchId ();
132155 $ request = $ this ->getRequest ();
133156 $ request ->method = Request::METHOD_POST ;
134157 $ request ->postBody = array (
@@ -150,6 +173,10 @@ public function discard() {
150173 if ($ this ->disableDiscards ) {
151174 throw new Exceptions \ClientException ('discards were disabled, but tried to send anyway ' );
152175 }
176+ if (!$ this ->didSubmit ) {
177+ throw new Exceptions \ClientException ('no data submitted yet ' );
178+ }
179+
153180 $ request = $ this ->getRequest ();
154181 $ request ->method = Request::METHOD_POST ;
155182 $ request ->url .= '/discard ' ;
@@ -224,6 +251,7 @@ private function _submit() {
224251 if (count ($ this ->_productData ) == 0 ) { // no data, do not send request
225252 return array ();
226253 }
254+ $ this ->createBatchId ();
227255 $ this ->didSubmit = true ;
228256 $ request = $ this ->getRequest ();
229257 $ request ->method = Request::METHOD_POST ;
0 commit comments