Skip to content

Instantly share code, notes, and snippets.

@potfur
Created October 5, 2015 16:39
Show Gist options
  • Save potfur/7b19fc451ad633cb533a to your computer and use it in GitHub Desktop.
Save potfur/7b19fc451ad633cb533a to your computer and use it in GitHub Desktop.
interface UserRegistrationVerification
{
public function execute(User $user);
}
final class NoVerification implements UserRegistrationVerification
{
public function execute(User $user)
{
$user->activate();
}
}
final class EmailVerification implements UserRegistrationVerification
{
private $mailer;
public function __construct(UserMailer $mailer)
{
$this->mailer = $mailer;
}
public function execute(User $user)
{
$this->mailer->sendActivationEmail($user);
}
}
final class LoggingRegistrationService
{
private $service;
public function __construct(LoggerInterface $logger, RegistrationService $service)
{
$this->logger = $logger;
$this->service = $service;
}
public function register(array $data, UserRegistrationVerification $verification)
{
$this->service->register($data, $verification);
$this->logger->info(
sprintf("User account with email \"%s\" was successfully registered.", (string) $user->getEmail())
);
}
}
final class RegistrationService
{
private $users;
private $userFactory;
public function __construct(UserRepository $users, UserFactory $factory)
{
$this->users = $users;
$this->userFactory = $factory;
}
public function register(array $data, UserRegistrationVerification $verification)
{
$user = $this->userFactory->createUser($data);
$this->users->add($user);
$verification->execute($user);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment