From 2e38e8baa69612b2966a2b3fffdc5f315dbf1235 Mon Sep 17 00:00:00 2001 From: Oleg Voronkovich Date: Sat, 22 Dec 2018 17:26:30 +0300 Subject: [PATCH] Encapsulate user creation with a factory --- src/Command/AddUserCommand.php | 19 ++++--------- src/DataFixtures/AppFixtures.php | 16 ++++------- src/User/UserFactory.php | 47 ++++++++++++++++++++++++++++++++ 3 files changed, 57 insertions(+), 25 deletions(-) create mode 100644 src/User/UserFactory.php diff --git a/src/Command/AddUserCommand.php b/src/Command/AddUserCommand.php index 4c9ae398d..8fa58876c 100644 --- a/src/Command/AddUserCommand.php +++ b/src/Command/AddUserCommand.php @@ -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; @@ -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; /** @@ -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; } @@ -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(); diff --git a/src/DataFixtures/AppFixtures.php b/src/DataFixtures/AppFixtures.php index 37a39553c..883083d00 100644 --- a/src/DataFixtures/AppFixtures.php +++ b/src/DataFixtures/AppFixtures.php @@ -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) @@ -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); diff --git a/src/User/UserFactory.php b/src/User/UserFactory.php new file mode 100644 index 000000000..6f0ee1352 --- /dev/null +++ b/src/User/UserFactory.php @@ -0,0 +1,47 @@ + + * + * 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 + */ +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; + } +}