Skip to content

Commit

Permalink
Encapsulate user creation with a factory
Browse files Browse the repository at this point in the history
  • Loading branch information
voronkovich committed Dec 22, 2018
1 parent cc35898 commit f5d386b
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 24 deletions.
19 changes: 5 additions & 14 deletions src/Command/AddUserCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use App\Entity\User;
use App\Repository\UserRepository;
use App\User\UserFactory;
use App\Utils\Validator;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\Console\Command\Command;
Expand All @@ -22,7 +23,6 @@
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
use Symfony\Component\Stopwatch\Stopwatch;

/**
Expand Down Expand Up @@ -56,16 +56,16 @@ class AddUserCommand extends Command
private $io;

private $entityManager;
private $passwordEncoder;
private $userFactory;
private $validator;
private $users;

public function __construct(EntityManagerInterface $em, UserPasswordEncoderInterface $encoder, Validator $validator, UserRepository $users)
public function __construct(EntityManagerInterface $em, UserFactory $userFactory, Validator $validator, UserRepository $users)
{
parent::__construct();

$this->entityManager = $em;
$this->passwordEncoder = $encoder;
$this->userFactory = $userFactory;
$this->validator = $validator;
$this->users = $users;
}
Expand Down Expand Up @@ -181,16 +181,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
// make sure to validate the user data is correct
$this->validateUserData($username, $plainPassword, $email, $fullName);

// create the user and encode its password
$user = new User();
$user->setFullName($fullName);
$user->setUsername($username);
$user->setEmail($email);
$user->setRoles([$isAdmin ? 'ROLE_ADMIN' : 'ROLE_USER']);

// See https://symfony.com/doc/current/book/security.html#security-encoding-password
$encodedPassword = $this->passwordEncoder->encodePassword($user, $plainPassword);
$user->setPassword($encodedPassword);
$user = $this->userFactory->createUser($username, $email, $fullName, $plainPassword, [$isAdmin ? 'ROLE_ADMIN' : 'ROLE_USER']);

$this->entityManager->persist($user);
$this->entityManager->flush();
Expand Down
15 changes: 5 additions & 10 deletions src/DataFixtures/AppFixtures.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,18 @@
use App\Entity\Post;
use App\Entity\Tag;
use App\Entity\User;
use App\User\UserFactory;
use App\Utils\Slugger;
use Doctrine\Bundle\FixturesBundle\Fixture;
use Doctrine\Common\Persistence\ObjectManager;
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;

class AppFixtures extends Fixture
{
private $passwordEncoder;
private $userFactory;

public function __construct(UserPasswordEncoderInterface $passwordEncoder)
public function __construct(UserFactory $userFactory)
{
$this->passwordEncoder = $passwordEncoder;
$this->userFactory = $userFactory;
}

public function load(ObjectManager $manager)
Expand All @@ -39,12 +39,7 @@ public function load(ObjectManager $manager)
private function loadUsers(ObjectManager $manager)
{
foreach ($this->getUserData() as [$fullname, $username, $password, $email, $roles]) {
$user = new User();
$user->setFullName($fullname);
$user->setUsername($username);
$user->setPassword($this->passwordEncoder->encodePassword($user, $password));
$user->setEmail($email);
$user->setRoles($roles);
$user = $this->userFactory->createUser($username, $email, $fullname, $password, $roles);

$manager->persist($user);
$this->addReference($username, $user);
Expand Down
47 changes: 47 additions & 0 deletions src/User/UserFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace App\User;

use App\Entity\User;
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;

/**
* Creates User instances.
*
* @author Oleg Voronkovich <[email protected]>
*/
class UserFactory
{
/**
* @var UserPasswordEncoderInterface
*/
private $passwordEncoder;

public function __construct(UserPasswordEncoderInterface $passwordEncoder)
{
$this->passwordEncoder = $passwordEncoder;
}

public function createUser(string $username, string $email, string $fullname, string $password, array $roles = ['ROLE_USER']): User
{
$user = new User();

$user->setUsername($username);
$user->setEmail($email);
$user->setFullName($fullname);
// See https://symfony.com/doc/current/book/security.html#security-encoding-password
$user->setPassword($this->passwordEncoder->encodePassword($user, $password));
$user->setRoles($roles);

return $user;
}
}

0 comments on commit f5d386b

Please sign in to comment.