RequestSender.php 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Signifyd\Model\SignifydGateway\Client;
  7. use Magento\Signifyd\Model\SignifydGateway\Debugger\DebuggerFactory;
  8. use Magento\Signifyd\Model\SignifydGateway\ApiCallException;
  9. use Magento\Framework\HTTP\ZendClient;
  10. /**
  11. * Class RequestSender
  12. * Gets HTTP client end sends request to Signifyd API
  13. */
  14. class RequestSender
  15. {
  16. /**
  17. * @var DebuggerFactory
  18. */
  19. private $debuggerFactory;
  20. /**
  21. * RequestSender constructor.
  22. *
  23. * @param DebuggerFactory $debuggerFactory
  24. */
  25. public function __construct(
  26. DebuggerFactory $debuggerFactory
  27. ) {
  28. $this->debuggerFactory = $debuggerFactory;
  29. }
  30. /**
  31. * Sends HTTP request to Signifyd API with configured client.
  32. *
  33. * Each request/response pair is handled by debugger.
  34. * If debug mode for Signifyd integration enabled in configuration
  35. * debug information is recorded to debug.log.
  36. *
  37. * @param ZendClient $client
  38. * @param int|null $storeId
  39. * @return \Zend_Http_Response
  40. * @throws ApiCallException
  41. */
  42. public function send(ZendClient $client, $storeId = null): \Zend_Http_Response
  43. {
  44. try {
  45. $response = $client->request();
  46. $this->debuggerFactory->create($storeId)->success(
  47. $client->getUri(true),
  48. $client->getLastRequest(),
  49. $response->getStatus() . ' ' . $response->getMessage(),
  50. $response->getBody()
  51. );
  52. return $response;
  53. } catch (\Exception $e) {
  54. $this->debuggerFactory->create($storeId)->failure(
  55. $client->getUri(true),
  56. $client->getLastRequest(),
  57. $e
  58. );
  59. throw new ApiCallException(
  60. 'Unable to process Signifyd API: ' . $e->getMessage(),
  61. $e->getCode(),
  62. $e,
  63. $client->getLastRequest()
  64. );
  65. }
  66. }
  67. }