Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Encapsulate user creation with a factory #920

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
16 changes: 5 additions & 11 deletions src/DataFixtures/AppFixtures.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,18 @@
use App\Entity\Comment;
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 +38,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;
}
}