Pārlūkot izejas kodu

Refactory Service Webservice

Guillermo Espinoza 8 gadi atpakaļ
vecāks
revīzija
4c0ea5fbf0
1 mainītis faili ar 74 papildinājumiem un 21 dzēšanām
  1. 74 21
      Services/Webservice.php

+ 74 - 21
Services/Webservice.php

@@ -84,6 +84,21 @@ class Webservice
         return (array)$results;
     }
     
+    /**
+     * @param string $url
+     * @param string $method
+     * 
+     * @return HttpResponse
+     */
+    public function makeRequest($url, $method = HttpRequestInterface::METHOD_GET)
+    {
+        $request = new HttpRequest($method, $url);
+        $response = new HttpResponse();
+        $this->httpClient->send($request, $response);
+        
+        return $response->getContent();
+    }
+    
     /**
      * @param string $url
      * @param string $method
@@ -119,30 +134,12 @@ class Webservice
     public function getData($webservice, $filters = array(), $order_by = array(), $limit = null, $offset = null)
     {
         if ($this->serviceContainer->hasParameter($webservice)) {
-            $url = $this->serviceContainer->getParameter($webservice) . "?";
-            
-            if($filters) {
-
-                $url .= http_build_query(array('filters' => $filters));
-            }
-            
-            if($order_by) {
-                $url .= http_build_query(array('order_by' => $order_by));
-            }
-            
-            if($limit) {
-                $url .= "&limit={$limit}";
-            }
-            
-            if($offset) {
-                $url .= "&offset={$offset}";
-            }
-
+            $url = $this->buildUrl($webservice, $filters, $order_by, $limit, $offset);
             $data = array();
             try {
                 $data = json_decode($this->makeGetRequest($url), true);
             } catch (\Exception $ex) {
-
+                
             }
 
             return $data;
@@ -150,7 +147,63 @@ class Webservice
 
         return array();
     }
-    
+
+    /**
+     * @param string $url
+     * @param array $filters
+     * @param array $order_by
+     * @param integer $limit
+     * @param integer $offset
+     * 
+     * @return array
+     */
+    public function get($url, $filters = array(), $order_by = array(), $limit = null, $offset = null)
+    {
+        $data = array();
+        try {
+            $url = $this->buildUrl($url, $filters, $order_by, $limit, $offset);
+            $data = json_decode($this->makeRequest($url), true);
+        } catch (\Exception $ex) {
+        }
+
+        return $data;
+    }
+
+    /**
+     * @param string $webservice
+     * @param array $filters
+     * @param array $order_by
+     * @param int $limit
+     * @param int $offset
+     * 
+     * @return string
+     */
+    public function buildUrl($webservice, $filters = array(), $order_by = array(), $limit = null, $offset = null)
+    {
+        $url = $webservice . '?';
+        if ($this->serviceContainer->hasParameter($webservice)) {
+            $url = $this->serviceContainer->getParameter($webservice) . '?';
+        }
+
+        if ($filters) {
+            $url .= http_build_query(array('filters' => $filters));
+        }
+
+        if ($order_by) {
+            $url .= http_build_query(array('order_by' => $order_by));
+        }
+
+        if ($limit) {
+            $url .= "&limit={$limit}";
+        }
+
+        if ($offset) {
+            $url .= "&offset={$offset}";
+        }
+        
+        return $url;
+    }
+
     /**
      * @param string $webservice
      * @param int $id