#8 done. NEW_USER_INVITES env variable added. Now new users will have defined amount of invites.

This commit is contained in:
Alexey Skobkin 2018-06-29 01:40:14 +03:00
parent 719e552088
commit 866685c90c
5 changed files with 58 additions and 15 deletions

View File

@ -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/*'

View File

@ -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();

View File

@ -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');

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
);
$this->userRepo->add($user);
return $user;
}