Skip to content

Commit

Permalink
Handle registration for exist customer
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-schranz authored and lchrusciel committed Sep 22, 2019
1 parent 2897900 commit fd2951a
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
15 changes: 13 additions & 2 deletions src/Handler/Customer/RegisterCustomerHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Sylius\Component\Channel\Repository\ChannelRepositoryInterface;
use Sylius\Component\Core\Model\CustomerInterface;
use Sylius\Component\Core\Model\ShopUserInterface;
use Sylius\Component\Core\Repository\CustomerRepositoryInterface;
use Sylius\Component\Resource\Factory\FactoryInterface;
use Sylius\Component\User\Repository\UserRepositoryInterface;
use Sylius\ShopApiPlugin\Command\Customer\RegisterCustomer;
Expand All @@ -22,6 +23,9 @@ final class RegisterCustomerHandler
/** @var ChannelRepositoryInterface */
private $channelRepository;

/** @var CustomerRepositoryInterface */
private $customerRepository;

/** @var FactoryInterface */
private $userFactory;

Expand All @@ -34,12 +38,14 @@ final class RegisterCustomerHandler
public function __construct(
UserRepositoryInterface $userRepository,
ChannelRepositoryInterface $channelRepository,
CustomerRepositoryInterface $customerRepository,
FactoryInterface $userFactory,
FactoryInterface $customerFactory,
EventDispatcherInterface $eventDispatcher
) {
$this->userRepository = $userRepository;
$this->channelRepository = $channelRepository;
$this->customerRepository = $customerRepository;
$this->userFactory = $userFactory;
$this->customerFactory = $customerFactory;
$this->eventDispatcher = $eventDispatcher;
Expand All @@ -50,8 +56,13 @@ public function __invoke(RegisterCustomer $command): void
$this->assertEmailIsNotTaken($command->email());
$this->assertChannelExists($command->channelCode());

/** @var CustomerInterface $customer */
$customer = $this->customerFactory->createNew();
/** @var CustomerInterface|null $customer */
$customer = $this->customerRepository->findOneBy(['email' => $command->email()]);
if (!$customer) {
/** @var CustomerInterface $customer */
$customer = $this->customerFactory->createNew();
}

$customer->setFirstName($command->firstName());
$customer->setLastName($command->lastName());
$customer->setEmail($command->email());
Expand Down
1 change: 1 addition & 0 deletions src/Resources/config/services/handler/customer.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
class="Sylius\ShopApiPlugin\Handler\Customer\RegisterCustomerHandler">
<argument type="service" id="sylius.repository.shop_user"/>
<argument type="service" id="sylius.repository.channel"/>
<argument type="service" id="sylius.repository.customer"/>
<argument type="service" id="sylius.factory.shop_user"/>
<argument type="service" id="sylius.factory.customer"/>
<argument type="service" id="event_dispatcher"/>
Expand Down

0 comments on commit fd2951a

Please sign in to comment.