Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
UserAccountBuilder.php
Go to the documentation of this file.
1 <?php
7 
10 
15 {
19  private $customerRepository;
20 
24  private $dateTimeFactory;
25 
29  private $customerOrders;
30 
36  public function __construct(
37  \Magento\Customer\Api\CustomerRepositoryInterface $customerRepository,
38  CustomerOrders $customerOrders,
39  \Magento\Framework\Intl\DateTimeFactory $dateTimeFactory
40  ) {
41  $this->customerRepository = $customerRepository;
42  $this->dateTimeFactory = $dateTimeFactory;
43  $this->customerOrders = $customerOrders;
44  }
45 
53  public function build(Order $order)
54  {
55  $result = [];
56 
57  $customerId = $order->getCustomerId();
58  if (null === $customerId) {
59  return $result;
60  }
61 
62  $customer = $this->customerRepository->getById($customerId);
63  $result = [
64  'userAccount' => [
65  'email' => $customer->getEmail(),
66  'username' => $customer->getEmail(),
67  'phone' => $order->getBillingAddress()->getTelephone(),
68  'accountNumber' => $customerId,
69  'createdDate' => $this->formatDate($customer->getCreatedAt()),
70  'lastUpdateDate' => $this->formatDate($customer->getUpdatedAt())
71  ]
72  ];
73 
74  $ordersInfo = $this->customerOrders->getAggregatedOrdersInfo($customerId);
75  if (isset($ordersInfo['aggregateOrderCount'])) {
76  $result['userAccount']['aggregateOrderCount'] = $ordersInfo['aggregateOrderCount'];
77  }
78  if (isset($ordersInfo['aggregateOrderDollars'])) {
79  $result['userAccount']['aggregateOrderDollars'] = $ordersInfo['aggregateOrderDollars'];
80  }
81 
82  return $result;
83  }
84 
91  private function formatDate($date)
92  {
93  $result = $this->dateTimeFactory->create(
94  $date,
95  new \DateTimeZone('UTC')
96  );
97 
98  return $result->format(\DateTime::ATOM);
99  }
100 }
$customer
Definition: customers.php:11
$order
Definition: order.php:55
$customerRepository
__construct(\Magento\Customer\Api\CustomerRepositoryInterface $customerRepository, CustomerOrders $customerOrders, \Magento\Framework\Intl\DateTimeFactory $dateTimeFactory)