UserAccountBuilder.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Signifyd\Model\SignifydGateway\Request;
  7. use Magento\Sales\Model\Order;
  8. use Magento\Signifyd\Model\CustomerOrders;
  9. /**
  10. * Prepares details based on registered user account info
  11. */
  12. class UserAccountBuilder
  13. {
  14. /**
  15. * @var \Magento\Customer\Api\CustomerRepositoryInterface
  16. */
  17. private $customerRepository;
  18. /**
  19. * @var \Magento\Framework\Intl\DateTimeFactory
  20. */
  21. private $dateTimeFactory;
  22. /**
  23. * @var CustomerOrders
  24. */
  25. private $customerOrders;
  26. /**
  27. * @param \Magento\Customer\Api\CustomerRepositoryInterface $customerRepository
  28. * @param CustomerOrders $customerOrders
  29. * @param \Magento\Framework\Intl\DateTimeFactory $dateTimeFactory
  30. */
  31. public function __construct(
  32. \Magento\Customer\Api\CustomerRepositoryInterface $customerRepository,
  33. CustomerOrders $customerOrders,
  34. \Magento\Framework\Intl\DateTimeFactory $dateTimeFactory
  35. ) {
  36. $this->customerRepository = $customerRepository;
  37. $this->dateTimeFactory = $dateTimeFactory;
  38. $this->customerOrders = $customerOrders;
  39. }
  40. /**
  41. * Returns user account data params.
  42. * Only for registered customers.
  43. *
  44. * @param Order $order
  45. * @return array
  46. */
  47. public function build(Order $order)
  48. {
  49. $result = [];
  50. $customerId = $order->getCustomerId();
  51. if (null === $customerId) {
  52. return $result;
  53. }
  54. $customer = $this->customerRepository->getById($customerId);
  55. $result = [
  56. 'userAccount' => [
  57. 'email' => $customer->getEmail(),
  58. 'username' => $customer->getEmail(),
  59. 'phone' => $order->getBillingAddress()->getTelephone(),
  60. 'accountNumber' => $customerId,
  61. 'createdDate' => $this->formatDate($customer->getCreatedAt()),
  62. 'lastUpdateDate' => $this->formatDate($customer->getUpdatedAt())
  63. ]
  64. ];
  65. $ordersInfo = $this->customerOrders->getAggregatedOrdersInfo($customerId);
  66. if (isset($ordersInfo['aggregateOrderCount'])) {
  67. $result['userAccount']['aggregateOrderCount'] = $ordersInfo['aggregateOrderCount'];
  68. }
  69. if (isset($ordersInfo['aggregateOrderDollars'])) {
  70. $result['userAccount']['aggregateOrderDollars'] = $ordersInfo['aggregateOrderDollars'];
  71. }
  72. return $result;
  73. }
  74. /**
  75. * Returns date formatted according to ISO8601.
  76. *
  77. * @param string $date
  78. * @return string
  79. */
  80. private function formatDate($date)
  81. {
  82. $result = $this->dateTimeFactory->create(
  83. $date,
  84. new \DateTimeZone('UTC')
  85. );
  86. return $result->format(\DateTime::ATOM);
  87. }
  88. }