WebserviceMock.php 4.7 KB

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