You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
animegame/src/DataFixtures/UserFixtures.php

50 lines
1.5 KiB

<?php
namespace App\DataFixtures;
use App\Entity\User;
use Doctrine\Bundle\FixturesBundle\Fixture;
use Doctrine\Common\Persistence\ObjectManager;
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
class UserFixtures extends Fixture
{
public const ADMIN_USER_REFERENCE = 'admin-user';
public const ADMIN_USER_TOKEN = 'ItsHammerTime!';
public const DUDE_USER_REFERENCE = 'dummy-user';
public const DUDE_USER_TOKEN = 'ItsDuderzeit!';
private $passwordEncoder;
public function __construct(UserPasswordEncoderInterface $passwordEncoder)
{
$this->passwordEncoder = $passwordEncoder;
}
public function load(ObjectManager $manager)
{
$userAdmin = new User();
$userAdmin->setUsername('admin');
$this->setPassword($userAdmin, '123456789');
$userAdmin->setApiToken(self::ADMIN_USER_TOKEN);
$manager->persist($userAdmin);
$userDude = new User();
$userDude->setUsername('dude');
$this->setPassword($userDude, '1234');
$userDude->setApiToken(self::DUDE_USER_TOKEN);
$manager->persist($userDude);
$manager->flush();
$this->addReference(self::ADMIN_USER_REFERENCE, $userAdmin);
$this->addReference(self::DUDE_USER_REFERENCE, $userDude);
}
private function setPassword(User& $user, string $plainPassword): void
{
$user->setPassword($this->passwordEncoder->encodePassword($user, $plainPassword));
}
}