Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Customer] Extract customer enabling logic to custom handler #487

Merged
merged 1 commit into from
Jul 24, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions spec/Command/Customer/EnableCustomerSpec.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

declare(strict_types=1);

namespace spec\Sylius\ShopApiPlugin\Command\Customer;

use PhpSpec\ObjectBehavior;

final class EnableCustomerSpec extends ObjectBehavior
{
function let(): void
{
$this->beConstructedWith('example@customer.com');
}

function it_has_email(): void
{
$this->email()->shouldReturn('example@customer.com');
}
}
9 changes: 4 additions & 5 deletions spec/EventListener/UserRegistrationListenerSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Sylius\Component\Core\Model\ChannelInterface;
use Sylius\Component\Core\Model\ShopUserInterface;
use Sylius\Component\User\Repository\UserRepositoryInterface;
use Sylius\ShopApiPlugin\Command\Customer\EnableCustomer;
use Sylius\ShopApiPlugin\Command\Customer\GenerateVerificationToken;
use Sylius\ShopApiPlugin\Command\Customer\SendVerificationToken;
use Sylius\ShopApiPlugin\Event\CustomerRegistered;
Expand Down Expand Up @@ -51,9 +52,9 @@ function it_generates_and_sends_verification_token_if_channel_requires_verificat
}

function it_enables_user_if_channel_does_not_require_verification(
MessageBusInterface $bus,
ChannelRepositoryInterface $channelRepository,
UserRepositoryInterface $userRepository,
ObjectManager $userManager,
ShopUserInterface $user,
ChannelInterface $channel
): void {
Expand All @@ -62,10 +63,8 @@ function it_enables_user_if_channel_does_not_require_verification(

$channel->isAccountVerificationRequired()->willReturn(false);

$user->enable()->shouldBeCalled();

$userManager->persist($user)->shouldBeCalled();
$userManager->flush()->shouldBeCalled();
$command = new EnableCustomer('shop@example.com');
$bus->dispatch($command)->willReturn(new Envelope($command))->shouldBeCalled();

$this->handleUserVerification(new CustomerRegistered(
'shop@example.com',
Expand Down
36 changes: 36 additions & 0 deletions spec/Handler/Customer/EnableCustomerHandlerSpec.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

declare(strict_types=1);

namespace spec\Sylius\ShopApiPlugin\Handler\Customer;

use PhpSpec\ObjectBehavior;
use Sylius\Component\Core\Model\ShopUserInterface;
use Sylius\Component\User\Repository\UserRepositoryInterface;
use Sylius\ShopApiPlugin\Command\Customer\EnableCustomer;

final class EnableCustomerHandlerSpec extends ObjectBehavior
{
function let(UserRepositoryInterface $userRepository): void
{
$this->beConstructedWith($userRepository);
}

function it_handles_enabling_user(UserRepositoryInterface $userRepository, ShopUserInterface $user): void
{
$userRepository->findOneByEmail('shop@example.com')->willReturn($user);

$user->enable()->shouldBeCalled();

$this(new EnableCustomer('shop@example.com'));
}

function it_throws_an_exception_if_user_cannot_be_found(UserRepositoryInterface $userRepository): void
{
$userRepository->findOneByEmail('shop@example.com')->willReturn(null);

$this->shouldThrow(\InvalidArgumentException::class)->during('__invoke', [
new EnableCustomer('shop@example.com'),
]);
}
}
21 changes: 21 additions & 0 deletions src/Command/Customer/EnableCustomer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

declare(strict_types=1);

namespace Sylius\ShopApiPlugin\Command\Customer;

class EnableCustomer
{
/** @var string */
protected $email;

public function __construct(string $email)
{
$this->email = $email;
}

public function email(): string
{
return $this->email;
}
}
26 changes: 4 additions & 22 deletions src/EventListener/UserRegistrationListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@

namespace Sylius\ShopApiPlugin\EventListener;

use Doctrine\Common\Persistence\ObjectManager;
use Sylius\Component\Channel\Repository\ChannelRepositoryInterface;
use Sylius\Component\Core\Model\ChannelInterface;
use Sylius\Component\User\Repository\UserRepositoryInterface;
use Sylius\ShopApiPlugin\Command\Customer\EnableCustomer;
use Sylius\ShopApiPlugin\Command\Customer\GenerateVerificationToken;
use Sylius\ShopApiPlugin\Command\Customer\SendVerificationToken;
use Sylius\ShopApiPlugin\Event\CustomerRegistered;
Expand All @@ -22,22 +21,10 @@ final class UserRegistrationListener
/** @var ChannelRepositoryInterface */
private $channelRepository;

/** @var UserRepositoryInterface */
private $userRepository;

/** @var ObjectManager */
private $userManager;

public function __construct(
MessageBusInterface $bus,
ChannelRepositoryInterface $channelRepository,
UserRepositoryInterface $userRepository,
ObjectManager $userManager
) {
public function __construct(MessageBusInterface $bus, ChannelRepositoryInterface $channelRepository)
{
$this->bus = $bus;
$this->channelRepository = $channelRepository;
$this->userRepository = $userRepository;
$this->userManager = $userManager;
}

public function handleUserVerification(CustomerRegistered $event): void
Expand All @@ -48,12 +35,7 @@ public function handleUserVerification(CustomerRegistered $event): void
Assert::isInstanceOf($channel, ChannelInterface::class);

if (!$channel->isAccountVerificationRequired()) {
$user = $this->userRepository->findOneByEmail($event->email());
$user->enable();

// TODO: Get rid of implementation details here
$this->userManager->persist($user);
$this->userManager->flush();
$this->bus->dispatch(new EnableCustomer($event->email()));

return;
}
Expand Down
31 changes: 31 additions & 0 deletions src/Handler/Customer/EnableCustomerHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

declare(strict_types=1);

namespace Sylius\ShopApiPlugin\Handler\Customer;

use Sylius\Component\Core\Model\ShopUserInterface;
use Sylius\Component\User\Repository\UserRepositoryInterface;
use Sylius\ShopApiPlugin\Command\Customer\EnableCustomer;
use Webmozart\Assert\Assert;

final class EnableCustomerHandler
{
/** @var UserRepositoryInterface */
private $userRepository;

public function __construct(UserRepositoryInterface $userRepository)
{
$this->userRepository = $userRepository;
}

public function __invoke(EnableCustomer $event): void
{
/** @var ShopUserInterface|null $user */
$user = $this->userRepository->findOneByEmail($event->email());

Assert::notNull($user);

$user->enable();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't we need a flush to the database? It was there in the previous implementation

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, as we have doctrine transaction over command bus and it will be flushed there.

}
}
6 changes: 6 additions & 0 deletions src/Resources/config/services/handler/customer.xml
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,11 @@
<argument type="service" id="sylius.shop_user.token_generator.email_verification"/>
<tag name="messenger.message_handler" />
</service>

<service id="sylius.shop_api_plugin.handler.enable_customer_handler"
class="Sylius\ShopApiPlugin\Handler\Customer\EnableCustomerHandler">
<argument type="service" id="sylius.repository.shop_user"/>
<tag name="messenger.message_handler" />
</service>
</services>
</container>