Merged in feature_users (pull request #5)

#8 done. NEW_USER_INVITES env variable added. Now new users will have defined amount of invites.
This commit is contained in:
Alexey Eschenko 2018-06-28 22:42:26 +00:00
commit aa25a2172b
5 changed files with 58 additions and 15 deletions

View file

@ -3,6 +3,7 @@
parameters: parameters:
locale: 'en' locale: 'en'
env(TRACKER_LIST_FILE): '%kernel.project_dir%/config/public_trackers.json' env(TRACKER_LIST_FILE): '%kernel.project_dir%/config/public_trackers.json'
env(NEW_USER_INVITES): 10
services: services:
# default configuration for services in *this* file # default configuration for services in *this* file
@ -14,6 +15,7 @@ services:
# The best practice is to be explicit about your dependencies anyway. # The best practice is to be explicit about your dependencies anyway.
bind: bind:
$publicTrackers: '%env(json:file:resolve:TRACKER_LIST_FILE)%' $publicTrackers: '%env(json:file:resolve:TRACKER_LIST_FILE)%'
$newUserInvites: '%env(NEW_USER_INVITES)%'
App\: App\:
resource: '../src/*' resource: '../src/*'

View file

@ -2,9 +2,8 @@
namespace App\Command; namespace App\Command;
use App\Entity\Invite; use App\Repository\UserRepository;
use App\Repository\{InviteRepository, UserRepository}; use App\User\{InviteManager, UserManager};
use App\User\UserManager;
use Doctrine\ORM\EntityManagerInterface; use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Helper\QuestionHelper; use Symfony\Component\Console\Helper\QuestionHelper;
@ -23,17 +22,17 @@ class AddUserCommand extends Command
/** @var UserRepository */ /** @var UserRepository */
private $userRepo; private $userRepo;
/** @var InviteRepository */ /** @var InviteManager */
private $inviteRepo; 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(); parent::__construct();
$this->em = $em; $this->em = $em;
$this->userManager = $userManager; $this->userManager = $userManager;
$this->userRepo = $userRepo; $this->userRepo = $userRepo;
$this->inviteRepo = $inviterepo; $this->inviteManager = $inviteManager;
} }
protected function configure() protected function configure()
@ -82,10 +81,7 @@ class AddUserCommand extends Command
$this->userRepo->add($user); $this->userRepo->add($user);
if ($invites) { if ($invites) {
for ($i = 0; $i < $invites; $i++) { $this->inviteManager->createInvitesForUser($user, $invites);
$invite = new Invite($user);
$this->inviteRepo->add($invite);
}
} }
$this->em->flush(); $this->em->flush();

View file

@ -4,9 +4,8 @@ namespace App\Controller;
use App\Form\{CreateUserRequestType}; use App\Form\{CreateUserRequestType};
use App\FormRequest\CreateUserRequest; use App\FormRequest\CreateUserRequest;
use App\Repository\{UserRepository};
use App\User\Exception\InvalidInviteException; use App\User\Exception\InvalidInviteException;
use App\User\UserManager; use App\User\{InviteManager, UserManager};
use Doctrine\ORM\EntityManagerInterface; use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\Controller; use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\Form\Extension\Core\Type\SubmitType; use Symfony\Component\Form\Extension\Core\Type\SubmitType;
@ -20,7 +19,7 @@ class UserController extends Controller
Request $request, Request $request,
EntityManagerInterface $em, EntityManagerInterface $em,
UserManager $userManager, UserManager $userManager,
UserRepository $userRepository InviteManager $inviteManager
): Response { ): Response {
$createUserRequest = new CreateUserRequest(); $createUserRequest = new CreateUserRequest();
$createUserRequest->inviteCode = $inviteCode; $createUserRequest->inviteCode = $inviteCode;
@ -36,6 +35,8 @@ class UserController extends Controller
$createUserRequest->email, $createUserRequest->email,
$createUserRequest->inviteCode $createUserRequest->inviteCode
); );
$inviteManager->createInvitesForUser($user);
} catch (InvalidInviteException $ex) { } catch (InvalidInviteException $ex) {
// @FIXME refactor InvalidInviteException to proper validator // @FIXME refactor InvalidInviteException to proper validator
$form->get('inviteCode')->addError(new FormError('Invalid invite code')); $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()]); return $this->render('User/register.html.twig', ['form' => $form->createView()]);
} }
$userRepository->add($user);
$em->flush(); $em->flush();
return $this->redirectToRoute('index'); return $this->redirectToRoute('index');

View file

@ -0,0 +1,43 @@
<?php
namespace App\User;
use App\Entity\{Invite, User};
use App\Repository\InviteRepository;
class InviteManager
{
/** @var InviteRepository */
private $inviteRepo;
/** @var int Which amount of invites we need to give to the new user */
private $newUserInvites;
public function __construct(InviteRepository $inviteRepo, int $newUserInvites = 0)
{
$this->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;
}
}

View file

@ -38,6 +38,8 @@ class UserManager
$roles $roles
); );
$this->userRepo->add($user);
return $user; return $user;
} }