123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159 |
- <?php
- namespace WebserviceBundle\Services;
- use Buzz\Client\Curl;
- use Buzz\Message\Request as HttpRequest;
- use Buzz\Message\RequestInterface as HttpRequestInterface;
- use Buzz\Message\Response as HttpResponse;
- use Symfony\Component\DependencyInjection\ContainerInterface;
- use Symfony\Component\Security\Acl\Exception\Exception;
- use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
- class WebserviceMock extends Webservice
- {
- /**
- * @var array Contiene un arreglo con la url de peticion como key y como value el valor a retornar.
- */
- private $dataResponse;
- /**
- * @param ContainerInterface $serviceContainer
- * @param TokenStorageInterface $securityTokenStorage
- * @param Curl $httpClient
- * @param array $dataResponse Contiene los datos a retornar. La key es la url del peticion hasta el
- * simbolo "?" y como value el valor a retornar.
- */
- public function __construct(ContainerInterface $serviceContainer = null,
- TokenStorageInterface $securityTokenStorage = null,
- Curl $httpClient = null,
- $dataResponse)
- {
- if ($serviceContainer != null) {
- $this->serviceContainer = $serviceContainer;
- }
- if ($securityTokenStorage != null) {
- $this->securityTokenStorage = $securityTokenStorage;
- }
- if ($httpClient != null) {
- $this->httpClient = $httpClient;
- }
- $this->dataResponse = $dataResponse;
- }
- /**
- * @param string $url
- * @param string $method
- * @param array $data
- * @param int $timeout
- *
- * @return HttpResponse
- */
- public function makeRequest($url, $method = HttpRequestInterface::METHOD_GET, $data = array(), $timeout = 60)
- {
- // $request = new HttpRequest($method, $url);
- // $headers = array();
- // if (!empty($data)) {
- // $headers[] = 'Content-Type: application/x-www-form-urlencoded';
- // $request->setContent(http_build_query($data));
- // }
- // $request->setHeaders($headers);
- // $response = new HttpResponse();
- // $this->httpClient->send($request, $response);
- //
- // return $response->getContent();
- throw new Exception("Not implemented.");
- }
- /**
- * @param string $url
- * @param string $method
- * @param array $data
- * @param array $credentials
- * @param array $cookies
- * @param int $timeout
- *
- * @return HttpResponse
- */
- public function makeGetRequest($url, $method = HttpRequestInterface::METHOD_GET, $data = array(), $credentials = array(), $cookies = array(), $timeout = 60)
- {
- $response = '';
- foreach ((array)$this->dataResponse as $k => $v) {
- $key = null;
- if ($this->serviceContainer->hasParameter($k)) {
- $key = $this->serviceContainer->getParameter($k);
- }
- if (($key != null && strpos($url, $key) !== false) ||
- strpos($url, $k) !== false) {
- $response = $v;
- break;
- }
- }
- return $response;
- }
- /**
- * @param string $webservice
- * @param int $id
- * @param array $data
- *
- * @return array
- */
- public function putData($webservice, $id, $data)
- {
- // if ($this->serviceContainer->hasParameter($webservice)) {
- // $url = str_replace(".json", "/{$id}", $this->serviceContainer->getParameter($webservice));
- // $data_string = json_encode($data);
- //
- // $ch = curl_init($url);
- // curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
- // curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
- // curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json', 'Content-Length: ' . strlen($data_string)));
- // curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
- //
- // try {
- // $return = curl_exec($ch);
- // } catch (\Exception $ex) {
- // return array("error" => "Connection Error.");
- // }
- //
- // if ($return) {
- // return json_decode($return, true);
- // } else {
- // return array("error" => "Transaction Error.");
- // }
- // }
- // return array("error" => "Webservice({$webservice}) not found.");
- throw new Exception("Not implemented.");
- }
- /**
- * @return array
- */
- public function getDataResponse()
- {
- return $this->dataResponse;
- }
- /**
- * @param array $dataResponse
- */
- public function setDataResponse($dataResponse)
- {
- $this->dataResponse = $dataResponse;
- }
- public function getData($url, $filters = array(), $order_by = array(), $limit = null, $offset = null)
- {
- if(isset($this->dataResponse[$url])){
- return $this->dataResponse[$url];
- }else{
- return array();
- }
- }
- }
|