WebserviceMock.php 4.7 KB

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