serviceContainer = $serviceContainer; $this->securityTokenStorage = $securityTokenStorage; $this->httpClient = $httpClient; } /** * @param string $webservice * @param array $params * * @return array */ public function getChoices($webservice, $params = array()) { $choices = array(); $results = $this->getArray($webservice, $params); foreach ($results as $row) { if (isset($row['name']) && isset($row['id'])) { $choices[$row['name']] = $row['id']; } } return $choices; } /** * @param string $webservice * @param array $params * * @return array */ public function getArray($webservice, $params = array()) { $results = array(); if ($this->serviceContainer->hasParameter($webservice)) { $url = $this->serviceContainer->getParameter($webservice); $url .= '?filters[qb-criteria]'; foreach ($params as $param => $value) { $url .= "&filters[{$param}]=$value"; } try { $results = json_decode($this->makeGetRequest($url), true); } catch (\Exception $ex) { } } return (array)$results; } /** * @param string $url * @param string $method * @param array $data * * @return HttpResponse */ public function makeRequest($url, $method = HttpRequestInterface::METHOD_GET, $data = array()) { $request = new HttpRequest($method, $url); $headers = array(); if (!empty($data)) { $headers[] = 'Content-Type: application/x-www-form-urlencoded'; $request->setContent(http_build_query($data)); } $request->setHeaders($headers); $response = new HttpResponse(); $this->httpClient->send($request, $response); return $response->getContent(); } /** * @param string $url * @param string $method * * @return HttpResponse */ public function makeGetRequest($url, $method = HttpRequestInterface::METHOD_GET, $data = array()) { $response = ''; if ($token = $this->securityTokenStorage->getToken()) { $headers = array(); $request = new HttpRequest($method, $url); if (!empty($data)) { $headers[] = 'Content-Type: application/x-www-form-urlencoded'; $request->setContent(http_build_query($data)); } $response = new HttpResponse(); if (method_exists($token, 'getAccessToken')) { $headers[] = 'Authorization: Bearer ' . $token->getAccessToken(); } $request->setHeaders($headers); $this->httpClient->send($request, $response); $response = $response->getContent(); } return $response; } /** * @param string $url * @param array $filters * @param array $order_by * @param integer $limit * @param integer $offset * * @return array */ public function getData($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->makeGetRequest($url), true); } catch (\Exception $ex) {} return $data; } /** * @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 * * @return string */ public function getById($webservice, $id) { $result = $this->getArray($webservice, array( 'id' => $id )); return isset($result[0]) && isset($result[0]['id']) && isset($result[0]['name']) ? "{$result[0]['id']} - {$result[0]['name']}" : $id; } /** * @param string $webservice * @param int $id * @param array $data * * @return array */ public function putData($webservice, $id, $data) { if ($this->serviceContainer->hasParameter($webservice)) { $url = str_replace(".json", "/{$id}", $this->serviceContainer->getParameter($webservice)); $data_string = json_encode($data); $ch = curl_init($url); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT"); curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string); curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json','Content-Length: '.strlen($data_string))); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); try { $return = curl_exec($ch); } catch (\Exception $ex) { return array("error"=>"Connection Error."); } if($return) { return json_decode($return, true); } else { return array("error"=>"Transaction Error."); } } return array("error"=>"Webservice({$webservice}) not found."); } }