/**
  * Set up
  */
 public function setUp()
 {
     $this->webClient = $this->getMock('Paysera_WalletApi_Http_ClientInterface');
     $dispatcher = new Paysera_WalletApi_EventDispatcher_EventDispatcher();
     $dispatcher->addSubscriber(new Paysera_WalletApi_Listener_InvalidResponseListener());
     $this->service = new Paysera_WalletApi_Client_BasicClient($this->webClient, $dispatcher);
 }
 public function handle($post)
 {
     if (!isset($post['event']) || !isset($post['sign'])) {
         throw new Paysera_WalletApi_Exception_CallbackException('At least one of required parameters is missing');
     }
     $event = $post['event'];
     $sign = $post['sign'];
     if (!$this->callbackSignChecker->checkSign($event, $sign)) {
         throw new Paysera_WalletApi_Exception_CallbackException('Sign validation failed');
     }
     $eventData = json_decode($event, true);
     try {
         if ($eventData['object'] === 'transaction') {
             $subject = new Paysera_WalletApi_Event_TransactionEvent($this->mapper->decodeTransaction($eventData['data']));
         } elseif ($eventData['object'] === 'payment') {
             $subject = new Paysera_WalletApi_Event_PaymentEvent($this->mapper->decodePayment($eventData['data']));
         } elseif ($eventData['object'] === 'allowance') {
             $subject = new Paysera_WalletApi_Event_AllowanceEvent($this->mapper->decodeAllowance($eventData['data']));
         } else {
             throw new Paysera_WalletApi_Exception_CallbackUnsupportedException('Unknown event object');
         }
     } catch (Paysera_WalletApi_Exception_CallbackException $exception) {
         throw $exception;
         // just pass callback exceptions
     } catch (Exception $exception) {
         throw new Paysera_WalletApi_Exception_CallbackException('Exception caught while trying to decode event subject', 0, $exception);
     }
     $eventKey = $eventData['object'] . '.' . $eventData['type'];
     $this->eventDispatcher->dispatch($eventKey, $subject);
 }
 /**
  * @param string                                                     $basePath
  * @param Paysera_WalletApi_EventDispatcher_EventSubscriberInterface $requestSigner
  * @param array                                                      $parameters
  *
  * @return Paysera_WalletApi_EventDispatcher_EventDispatcher
  */
 public function createDispatcherForClient($basePath, Paysera_WalletApi_EventDispatcher_EventSubscriberInterface $requestSigner, array $parameters = array())
 {
     $dispatcher = new Paysera_WalletApi_EventDispatcher_EventDispatcher();
     $dispatcher->mergeDispatcher($this->getEventDispatcher());
     $dispatcher->addSubscriber(new Paysera_WalletApi_Listener_EndpointSetter($basePath));
     if (count($parameters) > 0) {
         $dispatcher->addSubscriber(new Paysera_WalletApi_Listener_ParameterSetter($parameters));
     }
     $dispatcher->addSubscriber($requestSigner);
     $dispatcher->addSubscriber($this->createInvalidResponseListener());
     return $dispatcher;
 }
 /**
  * Makes specified request.
  * URI in request object can be relative to current context (without endpoint and API path).
  * Content of request is not encoded or otherwise modified by the client
  *
  * @param Paysera_WalletApi_Http_Request $request
  * @param array                          $options
  *
  * @throws Paysera_WalletApi_Exception_ApiException
  * @return mixed|null
  */
 public function makeRequest(Paysera_WalletApi_Http_Request $request, $options = array())
 {
     $originalRequest = clone $request;
     $event = new Paysera_WalletApi_Event_RequestEvent($request, $options);
     $this->eventDispatcher->dispatch(Paysera_WalletApi_Events::BEFORE_REQUEST, $event);
     $options = $event->getOptions();
     try {
         $response = $this->webClient->makeRequest($request);
     } catch (Paysera_WalletApi_Exception_HttpException $exception) {
         $event = new Paysera_WalletApi_Event_HttpExceptionEvent($exception, $request, $options);
         $this->eventDispatcher->dispatch(Paysera_WalletApi_Events::ON_HTTP_EXCEPTION, $event);
         if ($event->getResponse() !== null) {
             $response = $event->getResponse();
         } else {
             throw $event->getException();
         }
     }
     $response->setRequest($request);
     $event = new Paysera_WalletApi_Event_ResponseEvent($response, $options);
     $this->eventDispatcher->dispatch(Paysera_WalletApi_Events::AFTER_RESPONSE, $event);
     $options = $event->getOptions();
     try {
         $responseContent = $response->getContent();
         $result = $responseContent !== '' ? json_decode($responseContent, true) : null;
         if ($response->getStatusCode() === 200 && $responseContent === '' || $result === null && $responseContent !== '' && $responseContent !== 'null') {
             throw new Paysera_WalletApi_Exception_ResponseException(array('error' => 'internal_server_error', 'error_description' => sprintf('Bad response from server! Response: %s', $responseContent)), $response->getStatusCode(), $response->getStatusCodeMessage());
         }
         if ($response->isSuccessful()) {
             return $result;
         } else {
             throw new Paysera_WalletApi_Exception_ResponseException(is_array($result) ? $result : array(), $response->getStatusCode(), $response->getStatusCodeMessage());
         }
     } catch (Paysera_WalletApi_Exception_ResponseException $exception) {
         $event = new Paysera_WalletApi_Event_ResponseExceptionEvent($exception, $response, $options);
         $this->eventDispatcher->dispatch(Paysera_WalletApi_Events::ON_RESPONSE_EXCEPTION, $event);
         if ($event->getResult() !== null) {
             return $event->getResult();
         } elseif ($event->isRepeatRequest()) {
             return $this->makeRequest($originalRequest, $event->getOptions());
         } else {
             throw $event->getException();
         }
     }
 }
 /**
  * Makes specified request with options reference.
  * URI in request object can be relative to current context (without endpoint and API path).
  * Content of request is not encoded or otherwise modified by the client
  *
  * @param Paysera_WalletApi_Http_Request $request
  * @param array                          $options
  *
  * @return Paysera_WalletApi_Http_Response
  *
  * @throws Paysera_WalletApi_Exception_ResponseException
  */
 private function makePlainRequestWithReference(Paysera_WalletApi_Http_Request $request, &$options = array())
 {
     $event = new Paysera_WalletApi_Event_RequestEvent($request, $options);
     $this->eventDispatcher->dispatch(Paysera_WalletApi_Events::BEFORE_REQUEST, $event);
     $options = $event->getOptions();
     try {
         $response = $this->webClient->makeRequest($request);
     } catch (Paysera_WalletApi_Exception_HttpException $exception) {
         $event = new Paysera_WalletApi_Event_HttpExceptionEvent($exception, $request, $options);
         $this->eventDispatcher->dispatch(Paysera_WalletApi_Events::ON_HTTP_EXCEPTION, $event);
         if ($event->getResponse() !== null) {
             $response = $event->getResponse();
         } else {
             throw $event->getException();
         }
     }
     $response->setRequest($request);
     $event = new Paysera_WalletApi_Event_ResponseEvent($response, $options);
     $this->eventDispatcher->dispatch(Paysera_WalletApi_Events::AFTER_RESPONSE, $event);
     return $response;
 }