CustomerBuilder.php 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. <?php
  2. /**
  3. * @copyright Vertex. All rights reserved. https://www.vertexinc.com/
  4. * @author Mediotype https://www.mediotype.com/
  5. */
  6. namespace Vertex\Tax\Test\Integration\Builder;
  7. use Magento\Customer\Api\CustomerRepositoryInterface;
  8. use Magento\Customer\Api\Data\CustomerInterface;
  9. use Magento\Customer\Api\Data\CustomerInterfaceFactory;
  10. /**
  11. * Build a customer entity
  12. */
  13. class CustomerBuilder
  14. {
  15. const EXAMPLE_CUSTOMER_EMAIL = 'jdoe@host.local';
  16. const EXAMPLE_CUSTOMER_FIRSTNAME = 'John';
  17. const EXAMPLE_CUSTOMER_LASTNAME = 'Doe';
  18. /** @var CustomerInterfaceFactory */
  19. private $customerFactory;
  20. /** @var CustomerRepositoryInterface */
  21. private $customerRepository;
  22. /**
  23. * @param CustomerInterfaceFactory $customerFactory
  24. * @param CustomerRepositoryInterface $customerRepository
  25. */
  26. public function __construct(
  27. CustomerInterfaceFactory $customerFactory,
  28. CustomerRepositoryInterface $customerRepository
  29. ) {
  30. $this->customerFactory = $customerFactory;
  31. $this->customerRepository = $customerRepository;
  32. }
  33. /**
  34. * Create a customer
  35. *
  36. * @param callable $customerConfiguration Receives 1 parameter of CustomerInterface.
  37. * Should return a CustomerInterface.
  38. * @return CustomerInterface
  39. * @throws \TypeError
  40. * @throws \Magento\Framework\Exception\InputException
  41. * @throws \Magento\Framework\Exception\State\InputMismatchException
  42. * @throws \Magento\Framework\Exception\LocalizedException
  43. */
  44. public function createCustomer(callable $customerConfiguration)
  45. {
  46. /** @var CustomerInterface $customer */
  47. $customer = $customerConfiguration($this->customerFactory->create());
  48. if (!($customer instanceof CustomerInterface)) {
  49. throw new \TypeError('Result of createCustomer callback must return a CustomerInterface');
  50. }
  51. return $this->customerRepository->save($customer);
  52. }
  53. /**
  54. * Creates a generic customer
  55. *
  56. * Identity: John Doe <jdoe@host.local>
  57. *
  58. * @param callable $customerConfiguration Receives 1 parameter of CustomerInterface.
  59. * Should return a CustomerInterface.
  60. * @return CustomerInterface
  61. * @throws \Magento\Framework\Exception\InputException
  62. * @throws \Magento\Framework\Exception\LocalizedException
  63. * @throws \Magento\Framework\Exception\State\InputMismatchException
  64. */
  65. public function createExampleCustomer(callable $customerConfiguration = null)
  66. {
  67. return $this->createCustomer(
  68. function (CustomerInterface $customer) use ($customerConfiguration) {
  69. $customer->setFirstname(static::EXAMPLE_CUSTOMER_FIRSTNAME);
  70. $customer->setLastname(static::EXAMPLE_CUSTOMER_LASTNAME);
  71. $customer->setEmail(static::EXAMPLE_CUSTOMER_EMAIL);
  72. return $customerConfiguration !== null ? $customerConfiguration($customer) : $customer;
  73. }
  74. );
  75. }
  76. }