WebserviceMock.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. *
  44. * @return HttpResponse
  45. */
  46. public function makeRequest($url, $method = HttpRequestInterface::METHOD_GET, $data = array())
  47. {
  48. // $request = new HttpRequest($method, $url);
  49. // $headers = array();
  50. // if (!empty($data)) {
  51. // $headers[] = 'Content-Type: application/x-www-form-urlencoded';
  52. // $request->setContent(http_build_query($data));
  53. // }
  54. // $request->setHeaders($headers);
  55. // $response = new HttpResponse();
  56. // $this->httpClient->send($request, $response);
  57. //
  58. // return $response->getContent();
  59. throw new Exception("Not implemented.");
  60. }
  61. /**
  62. * @param string $url
  63. * @param string $method
  64. * @param array $data
  65. * @param array $credentials
  66. * @param array $cookies
  67. *
  68. * @return HttpResponse
  69. */
  70. public function makeGetRequest($url, $method = HttpRequestInterface::METHOD_GET, $data = array(), $credentials = array(), $cookies = array())
  71. {
  72. $response = '';
  73. foreach ((array)$this->dataResponse as $k => $v) {
  74. $key = null;
  75. if ($this->serviceContainer->hasParameter($k)) {
  76. $key = $this->serviceContainer->getParameter($k);
  77. }
  78. if (($key != null && strpos($url, $key) !== false) ||
  79. strpos($url, $k) !== false) {
  80. $response = $v;
  81. break;
  82. }
  83. }
  84. return $response;
  85. }
  86. /**
  87. * @param string $webservice
  88. * @param int $id
  89. * @param array $data
  90. *
  91. * @return array
  92. */
  93. public function putData($webservice, $id, $data)
  94. {
  95. // if ($this->serviceContainer->hasParameter($webservice)) {
  96. // $url = str_replace(".json", "/{$id}", $this->serviceContainer->getParameter($webservice));
  97. // $data_string = json_encode($data);
  98. //
  99. // $ch = curl_init($url);
  100. // curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
  101. // curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
  102. // curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json', 'Content-Length: ' . strlen($data_string)));
  103. // curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  104. //
  105. // try {
  106. // $return = curl_exec($ch);
  107. // } catch (\Exception $ex) {
  108. // return array("error" => "Connection Error.");
  109. // }
  110. //
  111. // if ($return) {
  112. // return json_decode($return, true);
  113. // } else {
  114. // return array("error" => "Transaction Error.");
  115. // }
  116. // }
  117. // return array("error" => "Webservice({$webservice}) not found.");
  118. throw new Exception("Not implemented.");
  119. }
  120. /**
  121. * @return array
  122. */
  123. public function getDataResponse()
  124. {
  125. return $this->dataResponse;
  126. }
  127. /**
  128. * @param array $dataResponse
  129. */
  130. public function setDataResponse($dataResponse)
  131. {
  132. $this->dataResponse = $dataResponse;
  133. }
  134. public function getData($url, $filters = array(), $order_by = array(), $limit = null, $offset = null)
  135. {
  136. if(isset($this->dataResponse[$url])){
  137. return $this->dataResponse[$url];
  138. }else{
  139. return array();
  140. }
  141. }
  142. }