66
77namespace Zoho \Subscriptions \Service ;
88
9+ use Traversable ;
910use DomainException ;
1011use Zend \Http ;
1112use Zend \InputFilter \InputFilterAwareInterface ;
1213use Zend \InputFilter \InputFilterAwareTrait ;
14+ use Zend \Stdlib \ArrayUtils ;
1315use Zend \Stdlib \Hydrator \HydratorInterface ;
1416use Zoho \Subscriptions \Entity \EntityInterface ;
1517
@@ -198,6 +200,9 @@ public function fetch($id)
198200 {
199201 curl_setopt ($ this ->curl , CURLOPT_URL , self ::ZOHO_API_ENDPOINT . $ this ->getPath () . '/ ' . $ id );
200202 $ result = curl_exec ($ this ->curl );
203+
204+ $ api_response_info = curl_getinfo ($ this ->curl );
205+
201206 $ result = json_decode ($ result );
202207 curl_close ($ this ->curl );
203208 $ entityName = $ this ->getEntityName ();
@@ -206,28 +211,64 @@ public function fetch($id)
206211 }
207212
208213 /**
209- * @param EntityInterface $entity
214+ * @param mixed $data
210215 * @return EntityInterface
211216 */
212- public function create (EntityInterface $ entity )
217+ public function create ($ data )
213218 {
214- $ this ->httpClient ->setMethod (Http \Request::METHOD_POST )
215- ->setUri (self ::ZOHO_API_ENDPOINT . $ this ->getPath ());
216- $ this ->processData ($ entity );
217- return $ this ->processRequest ();
219+ if ($ data instanceof EntityInterface) {
220+ $ data = $ this ->getHydrator ()->extract ($ data );
221+ } elseif ($ data instanceof Traversable) {
222+ $ data = ArrayUtils::iteratorToArray ($ data );
223+ }
224+
225+ if (!is_array ($ data )) {
226+ // throw 422
227+ }
228+
229+ $ fields = http_build_query ($ data );
230+
231+ curl_setopt ($ this ->curl , CURLOPT_URL , self ::ZOHO_API_ENDPOINT . $ this ->getPath ());
232+ curl_setopt ($ this ->curl , CURLOPT_POST , count ($ data ));
233+ curl_setopt ($ this ->curl , CURLOPT_POSTFIELDS , $ fields );
234+
235+ $ result = curl_exec ($ this ->curl );
236+ $ result = json_decode ($ result );
237+ curl_close ($ this ->curl );
238+
239+ $ entityName = $ this ->getEntityName ();
240+ return $ result ->$ entityName ;
241+
218242 }
219243
220244 /**
221245 * @param $id
222- * @param EntityInterface $entity
246+ * @param mixed $data
223247 * @return EntityInterface
224248 */
225- public function update ($ id , EntityInterface $ entity )
249+ public function update ($ id , $ data )
226250 {
227- $ this ->httpClient ->setMethod (Http \Request::METHOD_PUT )
228- ->setUri (self ::ZOHO_API_ENDPOINT . $ this ->getPath () . '/ ' . $ id );
229- $ this ->processData ($ entity );
230- return $ this ->processRequest ();
251+ if ($ data instanceof EntityInterface) {
252+ $ data = $ this ->getHydrator ()->extract ($ data );
253+ } elseif ($ data instanceof Traversable) {
254+ $ data = ArrayUtils::iteratorToArray ($ data );
255+ }
256+
257+ if (!is_array ($ data )) {
258+ // throw 422
259+ }
260+ $ fields = http_build_query ($ data );
261+
262+ curl_setopt ($ this ->curl , CURLOPT_URL , self ::ZOHO_API_ENDPOINT . $ this ->getPath () . '/ ' . $ id );
263+ curl_setopt ($ this ->curl , CURLOPT_CUSTOMREQUEST , "PUT " );
264+ curl_setopt ($ this ->curl , CURLOPT_POSTFIELDS , $ fields );
265+
266+ $ result = curl_exec ($ this ->curl );
267+ $ result = json_decode ($ result );
268+ curl_close ($ this ->curl );
269+
270+ $ entityName = $ this ->getEntityName ();
271+ return $ result ->$ entityName ;
231272 }
232273
233274 /**
@@ -236,20 +277,10 @@ public function update($id, EntityInterface $entity)
236277 */
237278 public function delete ($ id )
238279 {
239- $ this ->httpClient ->setMethod (Http \Request::METHOD_DELETE )
240- ->setUri (self ::ZOHO_API_ENDPOINT . $ this ->getPath () . '/ ' . $ id );
241- $ response = $ this ->httpClient ->send ();
242- if ($ response ->isSuccess ()) {
243- return true ;
244- } elseif ($ response ->isClientError () || $ response ->isServerError ()) {
245- throw new DomainException (
246- sprintf (
247- 'An error occured while requesting Zoho API for %s ' ,
248- __METHOD__
249- ),
250- $ response ->getStatusCode ()
251- );
252- }
280+ curl_setopt ($ this ->curl , CURLOPT_CUSTOMREQUEST , 'DELETE ' );
281+ curl_setopt ($ this ->curl , CURLOPT_URL , self ::ZOHO_API_ENDPOINT . $ this ->getPath () . '/ ' . $ id );
282+ curl_exec ($ this ->curl );
283+ return true ;
253284 }
254285
255286 /**
0 commit comments