CustomerLinkManagement.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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\Core\Api\Data\AmazonCustomerInterface;
  18. use Amazon\Login\Model\CustomerLinkRepositryFactory;
  19. use Amazon\Login\Api\CustomerLinkRepositoryInterface;
  20. use Amazon\Login\Api\Data\CustomerLinkInterfaceFactory;
  21. use Magento\Customer\Api\AccountManagementInterface;
  22. use Magento\Customer\Api\Data\CustomerInterfaceFactory;
  23. use Magento\Customer\Api\Data\CustomerInterface;
  24. use Magento\Customer\Model\Session;
  25. use Magento\Framework\Math\Random;
  26. class CustomerLinkManagement implements \Amazon\Login\Api\CustomerLinkManagementInterface
  27. {
  28. /**
  29. * @var CustomerLinkRepositoryInterface
  30. */
  31. private $customerLinkRepository;
  32. /**
  33. * @var CustomerLinkFactory
  34. */
  35. private $customerLinkFactory;
  36. /**
  37. * @var CustomerInterface
  38. */
  39. private $customerInterface;
  40. /**
  41. * @var CustomerInterfaceFactory
  42. */
  43. private $customerDataFactory;
  44. /**
  45. * @var AccountManagementInterface
  46. */
  47. private $accountManagement;
  48. /**
  49. * @var Random
  50. */
  51. private $random;
  52. /**
  53. * @param CustomerLinkRepositoryInterface $customerLinkRepository
  54. * @param CustomerLinkFactory $customerLinkFactory
  55. * @param CustomerInterface $customerInterface
  56. * @param CustomerInterfaceFactory $customerDataFactory
  57. * @param AccountManagementInterface $accountManagement
  58. * @param Random $random
  59. */
  60. public function __construct(
  61. CustomerLinkRepositoryInterface $customerLinkRepository,
  62. CustomerLinkFactory $customerLinkFactory,
  63. CustomerInterface $customerInterface,
  64. CustomerInterfaceFactory $customerDataFactory,
  65. AccountManagementInterface $accountManagement,
  66. Random $random
  67. ) {
  68. $this->customerLinkRepository = $customerLinkRepository;
  69. $this->customerLinkFactory = $customerLinkFactory;
  70. $this->customerInterface = $customerInterface;
  71. $this->customerDataFactory = $customerDataFactory;
  72. $this->accountManagement = $accountManagement;
  73. $this->random = $random;
  74. }
  75. /**
  76. * {@inheritdoc}
  77. */
  78. public function getByCustomerId($customerId)
  79. {
  80. return $this->customerLinkRepository->get($customerId);
  81. }
  82. /**
  83. * {@inheritdoc}
  84. */
  85. public function create(AmazonCustomerInterface $amazonCustomer)
  86. {
  87. $customerData = $this->customerDataFactory->create();
  88. $customerData->setFirstname($amazonCustomer->getFirstName());
  89. $customerData->setLastname($amazonCustomer->getLastName());
  90. $customerData->setEmail($amazonCustomer->getEmail());
  91. $password = $this->random->getRandomString(64);
  92. $customer = $this->accountManagement->createAccount($customerData, $password);
  93. return $customer;
  94. }
  95. /**
  96. * {@inheritdoc}
  97. */
  98. public function updateLink($customerId, $amazonId)
  99. {
  100. $customerLink = $this->customerLinkFactory->create();
  101. $customerLink
  102. ->load($customerId, 'customer_id')
  103. ->setAmazonId($amazonId)
  104. ->setCustomerId($customerId);
  105. $this->customerLinkRepository->save($customerLink);
  106. }
  107. }