WebserviceMock.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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\Acl\Exception\Exception;
  9. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  10. class WebserviceMock extends Webservice
  11. {
  12. /**
  13. * @var array Contiene un arreglo con la url de peticion como key y como value el valor a retornar.
  14. */
  15. private $dataResponse;
  16. /**
  17. * @param ContainerInterface $serviceContainer
  18. * @param TokenStorageInterface $securityTokenStorage
  19. * @param Curl $httpClient
  20. * @param array $dataResponse Contiene los datos a retornar. La key es la url del peticion hasta el
  21. * simbolo "?" y como value el valor a retornar.
  22. */
  23. public function __construct(ContainerInterface $serviceContainer = null,
  24. TokenStorageInterface $securityTokenStorage = null,
  25. Curl $httpClient = null,
  26. $dataResponse)
  27. {
  28. if ($serviceContainer != null) {
  29. $this->serviceContainer = $serviceContainer;
  30. }
  31. if ($securityTokenStorage != null) {
  32. $this->securityTokenStorage = $securityTokenStorage;
  33. }
  34. if ($httpClient != null) {
  35. $this->httpClient = $httpClient;
  36. }
  37. $this->dataResponse = $dataResponse;
  38. }
  39. /**
  40. * @param string $url
  41. * @param string $method
  42. * @param array $data
  43. * @param int $timeout
  44. *
  45. * @return HttpResponse
  46. */
  47. public function makeRequest($url, $method = HttpRequestInterface::METHOD_GET, $data = array(), $timeout = 60)
  48. {
  49. // $request = new HttpRequest($method, $url);
  50. // $headers = array();
  51. // if (!empty($data)) {
  52. // $headers[] = 'Content-Type: application/x-www-form-urlencoded';
  53. // $request->setContent(http_build_query($data));
  54. // }
  55. // $request->setHeaders($headers);
  56. // $response = new HttpResponse();
  57. // $this->httpClient->send($request, $response);
  58. //
  59. // return $response->getContent();
  60. throw new Exception("Not implemented.");
  61. }
  62. /**
  63. * @param string $url
  64. * @param string $method
  65. * @param array $data
  66. * @param array $credentials
  67. * @param array $cookies
  68. * @param int $timeout
  69. *
  70. * @return HttpResponse
  71. */
  72. public function makeGetRequest($url, $method = HttpRequestInterface::METHOD_GET, $data = array(), $credentials = array(), $cookies = array(), $timeout = 60)
  73. {
  74. $response = '';
  75. foreach ((array)$this->dataResponse as $k => $v) {
  76. $key = null;
  77. if ($this->serviceContainer->hasParameter($k)) {
  78. $key = $this->serviceContainer->getParameter($k);
  79. }
  80. if (($key != null && strpos($url, $key) !== false) ||
  81. strpos($url, $k) !== false) {
  82. $response = $v;
  83. break;
  84. }
  85. }
  86. return $response;
  87. }
  88. /**
  89. * @param string $webservice
  90. * @param int $id
  91. * @param array $data
  92. *
  93. * @return array
  94. */
  95. public function putData($webservice, $id, $data)
  96. {
  97. // if ($this->serviceContainer->hasParameter($webservice)) {
  98. // $url = str_replace(".json", "/{$id}", $this->serviceContainer->getParameter($webservice));
  99. // $data_string = json_encode($data);
  100. //
  101. // $ch = curl_init($url);
  102. // curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
  103. // curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
  104. // curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json', 'Content-Length: ' . strlen($data_string)));
  105. // curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  106. //
  107. // try {
  108. // $return = curl_exec($ch);
  109. // } catch (\Exception $ex) {
  110. // return array("error" => "Connection Error.");
  111. // }
  112. //
  113. // if ($return) {
  114. // return json_decode($return, true);
  115. // } else {
  116. // return array("error" => "Transaction Error.");
  117. // }
  118. // }
  119. // return array("error" => "Webservice({$webservice}) not found.");
  120. throw new Exception("Not implemented.");
  121. }
  122. /**
  123. * @return array
  124. */
  125. public function getDataResponse()
  126. {
  127. return $this->dataResponse;
  128. }
  129. /**
  130. * @param array $dataResponse
  131. */
  132. public function setDataResponse($dataResponse)
  133. {
  134. $this->dataResponse = $dataResponse;
  135. }
  136. public function getData($url, $filters = array(), $order_by = array(), $limit = null, $offset = null)
  137. {
  138. if(isset($this->dataResponse[$url])){
  139. return $this->dataResponse[$url];
  140. }else{
  141. return array();
  142. }
  143. }
  144. }