Webservice.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. <?php
  2. namespace WebserviceBundle\Services;
  3. use Buzz\Client\Curl;
  4. use Buzz\Message\Request as HttpRequest;
  5. use Buzz\Message\RequestInterface as HttpRequestInterface;
  6. use Buzz\Message\Response as HttpResponse;
  7. use Symfony\Component\DependencyInjection\ContainerInterface;
  8. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  9. class Webservice
  10. {
  11. /**
  12. * @var ContainerInterface
  13. */
  14. protected $serviceContainer;
  15. /**
  16. * @var TokenStorage
  17. */
  18. protected $securityTokenStorage;
  19. /**
  20. * @var Curl
  21. */
  22. protected $httpClient;
  23. /**
  24. * @param ContainerInterface $serviceContainer
  25. * @param TokenStorageInterface $securityTokenStorage
  26. * @param Curl $httpClient
  27. */
  28. public function __construct(ContainerInterface $serviceContainer, TokenStorageInterface $securityTokenStorage, Curl $httpClient)
  29. {
  30. $this->serviceContainer = $serviceContainer;
  31. $this->securityTokenStorage = $securityTokenStorage;
  32. $this->httpClient = $httpClient;
  33. }
  34. /**
  35. * @param string $webservice
  36. * @param array $params
  37. *
  38. * @return array
  39. */
  40. public function getChoices($webservice, $params = array())
  41. {
  42. $choices = array();
  43. $results = $this->getArray($webservice, $params);
  44. foreach ($results as $row) {
  45. if (isset($row['name']) && isset($row['id'])) {
  46. $choices[$row['name']] = $row['id'];
  47. }
  48. }
  49. return $choices;
  50. }
  51. /**
  52. * @param string $webservice
  53. * @param array $params
  54. *
  55. * @return array
  56. */
  57. public function getArray($webservice, $params = array())
  58. {
  59. $results = array();
  60. if ($this->serviceContainer->hasParameter($webservice)) {
  61. $url = $this->serviceContainer->getParameter($webservice);
  62. $url .= '?filters[qb-criteria]';
  63. foreach ($params as $param => $value) {
  64. $url .= "&filters[{$param}]=$value";
  65. }
  66. try {
  67. $results = json_decode($this->makeGetRequest($url), true);
  68. } catch (\Exception $ex) {
  69. }
  70. }
  71. return (array)$results;
  72. }
  73. /**
  74. * @param string $url
  75. * @param string $method
  76. * @param array $data
  77. *
  78. * @return HttpResponse
  79. */
  80. public function makeRequest($url, $method = HttpRequestInterface::METHOD_GET, $data = array())
  81. {
  82. $request = new HttpRequest($method, $url);
  83. $headers = array();
  84. if (!empty($data)) {
  85. $headers[] = 'Content-Type: application/x-www-form-urlencoded';
  86. $request->setContent(http_build_query($data));
  87. }
  88. $request->setHeaders($headers);
  89. $response = new HttpResponse();
  90. $this->httpClient->send($request, $response);
  91. return $response->getContent();
  92. }
  93. /**
  94. * @param string $url
  95. * @param string $method
  96. *
  97. * @return HttpResponse
  98. */
  99. public function makeGetRequest($url, $method = HttpRequestInterface::METHOD_GET, $data = array())
  100. {
  101. $response = '';
  102. if ($token = $this->securityTokenStorage->getToken()) {
  103. $headers = array();
  104. $request = new HttpRequest($method, $url);
  105. if (!empty($data)) {
  106. $headers[] = 'Content-Type: application/x-www-form-urlencoded';
  107. $request->setContent(http_build_query($data));
  108. }
  109. $response = new HttpResponse();
  110. if (method_exists($token, 'getAccessToken')) {
  111. $headers[] = 'Authorization: Bearer ' . $token->getAccessToken();
  112. }
  113. $request->setHeaders($headers);
  114. $this->httpClient->send($request, $response);
  115. $response = $response->getContent();
  116. }
  117. return $response;
  118. }
  119. /**
  120. * @param string $webservice
  121. * @param array $filters
  122. * @param array $order_by
  123. * @param integer $limit
  124. * @param integer $offset
  125. *
  126. * @return array
  127. */
  128. public function getData($webservice, $filters = array(), $order_by = array(), $limit = null, $offset = null)
  129. {
  130. if ($this->serviceContainer->hasParameter($webservice)) {
  131. $url = $this->buildUrl($webservice, $filters, $order_by, $limit, $offset);
  132. $data = array();
  133. try {
  134. $data = json_decode($this->makeGetRequest($url), true);
  135. } catch (\Exception $ex) {
  136. }
  137. return $data;
  138. }
  139. return array();
  140. }
  141. /**
  142. * @param string $url
  143. * @param array $filters
  144. * @param array $order_by
  145. * @param integer $limit
  146. * @param integer $offset
  147. *
  148. * @return array
  149. */
  150. public function get($url, $filters = array(), $order_by = array(), $limit = null, $offset = null)
  151. {
  152. $data = array();
  153. try {
  154. $url = $this->buildUrl($url, $filters, $order_by, $limit, $offset);
  155. $data = json_decode($this->makeRequest($url), true);
  156. } catch (\Exception $ex) {
  157. }
  158. return $data;
  159. }
  160. /**
  161. * @param string $webservice
  162. * @param array $filters
  163. * @param array $order_by
  164. * @param int $limit
  165. * @param int $offset
  166. *
  167. * @return string
  168. */
  169. public function buildUrl($webservice, $filters = array(), $order_by = array(), $limit = null, $offset = null)
  170. {
  171. $url = $webservice . '?';
  172. if ($this->serviceContainer->hasParameter($webservice)) {
  173. $url = $this->serviceContainer->getParameter($webservice) . '?';
  174. }
  175. if ($filters) {
  176. $url .= http_build_query(array('filters' => $filters));
  177. }
  178. if ($order_by) {
  179. $url .= '&' . http_build_query(array('order_by' => $order_by));
  180. }
  181. if ($limit) {
  182. $url .= "&limit={$limit}";
  183. }
  184. if ($offset) {
  185. $url .= "&offset={$offset}";
  186. }
  187. return $url;
  188. }
  189. /**
  190. * @param string $webservice
  191. * @param int $id
  192. *
  193. * @return string
  194. */
  195. public function getById($webservice, $id)
  196. {
  197. $result = $this->getArray($webservice, array(
  198. 'id' => $id
  199. ));
  200. return isset($result[0]) && isset($result[0]['id']) && isset($result[0]['name'])
  201. ? "{$result[0]['id']} - {$result[0]['name']}"
  202. : $id;
  203. }
  204. /**
  205. * @param string $webservice
  206. * @param int $id
  207. * @param array $data
  208. *
  209. * @return array
  210. */
  211. public function putData($webservice, $id, $data)
  212. {
  213. if ($this->serviceContainer->hasParameter($webservice)) {
  214. $url = str_replace(".json", "/{$id}", $this->serviceContainer->getParameter($webservice));
  215. $data_string = json_encode($data);
  216. $ch = curl_init($url);
  217. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
  218. curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
  219. curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json', 'Content-Length: ' . strlen($data_string)));
  220. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  221. try {
  222. $return = curl_exec($ch);
  223. } catch (\Exception $ex) {
  224. return array("error" => "Connection Error.");
  225. }
  226. if ($return) {
  227. return json_decode($return, true);
  228. } else {
  229. return array("error" => "Transaction Error.");
  230. }
  231. }
  232. return array("error" => "Webservice({$webservice}) not found.");
  233. }
  234. /**
  235. * @return ContainerInterface
  236. */
  237. public function getServiceContainer()
  238. {
  239. return $this->serviceContainer;
  240. }
  241. /**
  242. * @return TokenStorage
  243. */
  244. public function getSecurityTokenStorage()
  245. {
  246. return $this->securityTokenStorage;
  247. }
  248. /**
  249. * @return Curl
  250. */
  251. public function getHttpClient()
  252. {
  253. return $this->httpClient;
  254. }
  255. }