-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Encapsulate user creation with a factory
- Loading branch information
1 parent
cc35898
commit f5d386b
Showing
3 changed files
with
57 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |