function it_signs_the_user_up(SignUpUserCommand $command, UserRepository $repository, UserFactorySignUp $factory, User $user) { $command->id()->shouldBeCalled()->willReturn('user-id'); $id = new UserId('user-id'); $repository->userOfId($id)->shouldBeCalled()->willReturn(null); $command->email()->shouldBeCalled()->willReturn('*****@*****.**'); $email = new UserEmail('*****@*****.**'); $repository->userOfEmail($email)->shouldBeCalled()->willReturn(null); $command->password()->shouldBeCalled()->willReturn('plain-password'); $command->roles()->shouldBeCalled()->willReturn(['ROLE_USER']); $roles = [new UserRole('ROLE_USER')]; $factory->build($id, $email, Argument::type(UserPassword::class), $roles)->shouldBeCalled()->willReturn($user); $user->enableAccount()->shouldBeCalled(); $repository->persist($user)->shouldBeCalled(); $this->__invoke($command); }
/** * Handles the given command. * * @param SignUpUserCommand $aCommand The command * * @throws UserAlreadyExistException when the user id is already exists */ public function __invoke(SignUpUserCommand $aCommand) { $id = new UserId($aCommand->id()); if (null !== $this->repository->userOfId($id)) { throw new UserAlreadyExistException(); } $email = new UserEmail($aCommand->email()); if (null !== $this->repository->userOfEmail($email)) { throw new UserAlreadyExistException(); } $userRoles = array_map(function ($role) { return new UserRole($role); }, $aCommand->roles()); $user = $this->factory->build($id, $email, UserPassword::fromPlain($aCommand->password(), $this->encoder), $userRoles); $user->enableAccount(); $this->repository->persist($user); }