CustomerManagement.php 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. <?php
  2. /**
  3. * Copyright 2016 Amazon.com, Inc. or its affiliates. All Rights Reserved.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License").
  6. * You may not use this file except in compliance with the License.
  7. * A copy of the License is located at
  8. *
  9. * http://aws.amazon.com/apache2.0
  10. *
  11. * or in the "license" file accompanying this file. This file is distributed
  12. * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
  13. * express or implied. See the License for the specific language governing
  14. * permissions and limitations under the License.
  15. */
  16. namespace Amazon\Login\Model;
  17. use Amazon\Login\Api\CustomerLinkManagementInterface;
  18. use Magento\Customer\Api\Data\CustomerInterface;
  19. use Magento\Customer\Api\Data\CustomerExtensionFactory;
  20. class CustomerManagement implements \Amazon\Login\Api\CustomerManagementInterface
  21. {
  22. /**
  23. * @var CustomerLinkManagement
  24. */
  25. private $customerLinkManagement;
  26. /**
  27. * @var CustomerExtensionFactory
  28. */
  29. private $customerExtensionFactory;
  30. /**
  31. * @var string
  32. */
  33. private $amazonId;
  34. /**
  35. * @param CustomerLinkRepositoryInterface $customerLinkRepository
  36. * @param CustomerExtensionFactory $customerExtensionFactory
  37. */
  38. public function __construct(
  39. CustomerLinkManagementInterface $customerLinkManagement,
  40. CustomerExtensionFactory $customerExtensionFactory
  41. ) {
  42. $this->customerLinkManagement = $customerLinkManagement;
  43. $this->customerExtensionFactory = $customerExtensionFactory;
  44. }
  45. /**
  46. * {@inheritdoc}
  47. */
  48. public function setAmazonIdExtensionAttribute(CustomerInterface $customer)
  49. {
  50. $customerExtension = ($customer->getExtensionAttributes()) ?: $this->customerExtensionFactory->create();
  51. if (null === $this->amazonId) {
  52. $amazonCustomer = $this->customerLinkManagement->getByCustomerId($customer->getId());
  53. if ($amazonCustomer->getId()) {
  54. $this->amazonId = $amazonCustomer->getAmazonId();
  55. }
  56. }
  57. if ($this->amazonId) {
  58. $customerExtension->setAmazonId($this->amazonId);
  59. }
  60. $customer->setExtensionAttributes($customerExtension);
  61. }
  62. }