diff --git a/config/services.yaml b/config/services.yaml index da562e0..885287c 100644 --- a/config/services.yaml +++ b/config/services.yaml @@ -3,6 +3,7 @@ parameters: locale: 'en' env(TRACKER_LIST_FILE): '%kernel.project_dir%/config/public_trackers.json' + env(NEW_USER_INVITES): 10 services: # default configuration for services in *this* file @@ -14,6 +15,7 @@ services: # The best practice is to be explicit about your dependencies anyway. bind: $publicTrackers: '%env(json:file:resolve:TRACKER_LIST_FILE)%' + $newUserInvites: '%env(NEW_USER_INVITES)%' App\: resource: '../src/*' diff --git a/src/Command/AddUserCommand.php b/src/Command/AddUserCommand.php index f6361a0..e84536b 100644 --- a/src/Command/AddUserCommand.php +++ b/src/Command/AddUserCommand.php @@ -2,9 +2,8 @@ namespace App\Command; -use App\Entity\Invite; -use App\Repository\{InviteRepository, UserRepository}; -use App\User\UserManager; +use App\Repository\UserRepository; +use App\User\{InviteManager, UserManager}; use Doctrine\ORM\EntityManagerInterface; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Helper\QuestionHelper; @@ -23,17 +22,17 @@ class AddUserCommand extends Command /** @var UserRepository */ private $userRepo; - /** @var InviteRepository */ - private $inviteRepo; + /** @var InviteManager */ + private $inviteManager; - public function __construct(EntityManagerInterface $em, UserManager $userManager, UserRepository $userRepo, InviteRepository $inviterepo) + public function __construct(EntityManagerInterface $em, UserManager $userManager, UserRepository $userRepo, InviteManager $inviteManager) { parent::__construct(); $this->em = $em; $this->userManager = $userManager; $this->userRepo = $userRepo; - $this->inviteRepo = $inviterepo; + $this->inviteManager = $inviteManager; } protected function configure() @@ -82,10 +81,7 @@ class AddUserCommand extends Command $this->userRepo->add($user); if ($invites) { - for ($i = 0; $i < $invites; $i++) { - $invite = new Invite($user); - $this->inviteRepo->add($invite); - } + $this->inviteManager->createInvitesForUser($user, $invites); } $this->em->flush(); diff --git a/src/Controller/UserController.php b/src/Controller/UserController.php index b4fee2f..eb2e892 100644 --- a/src/Controller/UserController.php +++ b/src/Controller/UserController.php @@ -4,9 +4,8 @@ namespace App\Controller; use App\Form\{CreateUserRequestType}; use App\FormRequest\CreateUserRequest; -use App\Repository\{UserRepository}; use App\User\Exception\InvalidInviteException; -use App\User\UserManager; +use App\User\{InviteManager, UserManager}; use Doctrine\ORM\EntityManagerInterface; use Symfony\Bundle\FrameworkBundle\Controller\Controller; use Symfony\Component\Form\Extension\Core\Type\SubmitType; @@ -20,7 +19,7 @@ class UserController extends Controller Request $request, EntityManagerInterface $em, UserManager $userManager, - UserRepository $userRepository + InviteManager $inviteManager ): Response { $createUserRequest = new CreateUserRequest(); $createUserRequest->inviteCode = $inviteCode; @@ -36,6 +35,8 @@ class UserController extends Controller $createUserRequest->email, $createUserRequest->inviteCode ); + + $inviteManager->createInvitesForUser($user); } catch (InvalidInviteException $ex) { // @FIXME refactor InvalidInviteException to proper validator $form->get('inviteCode')->addError(new FormError('Invalid invite code')); @@ -43,7 +44,6 @@ class UserController extends Controller return $this->render('User/register.html.twig', ['form' => $form->createView()]); } - $userRepository->add($user); $em->flush(); return $this->redirectToRoute('index'); diff --git a/src/User/InviteManager.php b/src/User/InviteManager.php new file mode 100644 index 0000000..8c86779 --- /dev/null +++ b/src/User/InviteManager.php @@ -0,0 +1,43 @@ +inviteRepo = $inviteRepo; + $this->newUserInvites = $newUserInvites; + } + + /** + * @return Invite[] + */ + public function createInvitesForUser(User $user, int $forceAmount = null): iterable + { + if (!in_array('ROLE_USER', $user->getRoles(), true)) { + return []; + } + + $amount = (null !== $forceAmount) ? $forceAmount : $this->newUserInvites; + + $invites = []; + + for ($i = 0; $i < $amount; $i++) { + $invite = new Invite($user); + $this->inviteRepo->add($invite); + $invites[] = $invite; + } + + return $invites; + } +} \ No newline at end of file diff --git a/src/User/UserManager.php b/src/User/UserManager.php index 498bcc0..cb686de 100644 --- a/src/User/UserManager.php +++ b/src/User/UserManager.php @@ -38,6 +38,8 @@ class UserManager $roles ); + $this->userRepo->add($user); + return $user; }