Skip to content
Snippets Groups Projects
Commit 9f5ecc1e authored by Moreau Elise's avatar Moreau Elise
Browse files

update script to generate db with lib faker

parent cb4bd679
No related branches found
No related tags found
No related merge requests found
<?php <?php
declare(strict_types=1); declare(strict_types=1);
namespace App\Application\Actions\Search; namespace App\Application\Actions\Search;
// require_once 'vendor/autoload.php';
// require_once 'vendor/fazninotto/src/Faker/autoload.php';
use App\Application\Actions\Action; use App\Application\Actions\Action;
use Psr\Http\Message\ResponseInterface as Response; use Psr\Http\Message\ResponseInterface as Response;
...@@ -12,92 +13,115 @@ use Slim\Views\Twig; ...@@ -12,92 +13,115 @@ use Slim\Views\Twig;
use Slim\Flash\Messages; use Slim\Flash\Messages;
use App\Domain\User\User ; use App\Domain\User\User ;
use App\Domain\Group\Group ; use App\Domain\Group\Group ;
use App\Domain\Location\Location ;
use Faker;
use DateTime; use DateTime;
class CreateDB extends Action{ class CreateDB extends Action{
public function __construct(LoggerInterface $logger, EntityManager $em, Twig $twig, Messages $flash){ public function __construct(LoggerInterface $logger, EntityManager $em, Twig $twig, Messages $flash){
parent::__construct($logger); parent::__construct($logger);
$this->em = $em; $this->em = $em;
$this->userRepository = $em->getRepository('App\Domain\User\User'); $this->userRepository = $em->getRepository('App\Domain\User\User');
$this->groupRepository = $em->getRepository('App\Domain\Group\Group'); $this->groupRepository = $em->getRepository('App\Domain\Group\Group');
$this->number = rand(0,50); $this->number = rand(0,50);
$this->faker = Faker\Factory::create();
$this->usernames = [ }
'Rerunner', 'Poltergeisha', 'Urchinchilla', 'Dragonightmare', 'MerryLarva', 'GrizzlySeal',
'ExoticHorse', 'RapidOstrich', 'MerryPig', 'OriginalLark', public function create_localisation(){
'Sailorc', 'Toucanoe', 'Geckoco', 'Wrathhawk', 'IdenticalArmadillo', 'RedStitches', 'DeliriousSparks', $cities = [
'PrettySwan', 'FirstDoughnut', 'HandsomePrincess', 'Sultank', 'Specterminate', 'Troutwards', 'Bullfrogert', 'nancy' => [
'AdviseHurricane', 'ThroneFishy', 'BreakfastSheep', 'GloomySaint', 'FoolishAntelope', 'ArcticMutant', 'latitude' => (float) $this->faker->latitude(48.61, 48.72),
'Devile', 'Sailord', 'Unbandit', 'Aromaster', 'ThunderApe', 'MysteryMoose', 'GrumpyHooper', 'ConsultChick', 'longitude' => (float) $this->faker->longitude(6.098, 6.25)
'FrighteningRaider', 'FurApe', 'Herogue', 'Beastern', 'Bingoddess', 'Orangutango', 'FlameButterfly', ],
'AcrobaticPanther', 'MessyCherry', 'SleepingEmu', 'CannonLord', 'ActionWombat', 'SwallowRider', 'paris' => [
'Orangutitan', 'Parascythe', 'Sharctic', 'AmazingPetal', 'BadMeerkat', 'SmallFry', 'InsecureHydra', 'latitude' => (float) $this->faker->latitude(48.64, 49.1116),
'WarFoal', 'LostBloodElf', 'Ostritches', 'Eyewearwolf', 'Gorillala', 'Gangstereo', 'BigCat', 'longitude' => (float) $this->faker->longitude(1.9239, 2.7589)
'CopperQueen', 'SpotlessBot', 'ForceHerring', 'AmusingFledgling', 'HeartHatchling', ],
'Dinosause', 'Phantomfool', 'Woodpecan', 'Cowhale', 'PunyPirate', 'BakingPup', 'LoneOyster', 'AmazingHooper', 'center' => [
'ControlWarhawk', 'FlameRogue' ]; 'latitude' => (float) $this->faker->latitude(43.8899, 48.3051),
'longitude' => (float) $this->faker->longitude(-1.1260, 5.3228)
$this->firstnames = [ ],
'Hana', 'Zoya', 'Willie', 'Nettie', 'Kara', 'Lara', 'Halima', 'Laila', 'Alicia', 'Caroline',
'Carla', 'Julie', 'Katherine', 'Holly', 'Rebekah', 'Lachlan', 'Millie', 'Chantelle', 'Robin', 'Aminah', 'lyon' => [
'Ashley', 'Fern', 'Agnes', 'Harley', 'Rhiannon', 'Fannie', 'Brianna', 'Niamh', 'Tanisha', 'Sally', 'latitude' => (float) $this->faker->latitude(45.6178, 45.9702),
'Amelia', 'Molly', 'Aliyah', 'Violet', 'Aisha', 'Libby', 'Alana', 'Danielle', 'Tiana', 'Zaynab', 'longitude' => (float) $this->faker->longitude(4.6266, 5.1210)
'Rebecca', 'Anais', 'Ida', 'Hanna', 'Jean', 'Josie', 'Maya', 'Jodie', 'Joyce', 'Amina', ],
'Dewey', 'Simon', 'Kira', 'Taylor', 'Declan', 'Andrew', 'Brandon', 'Milo', 'Edwin', 'Miles',
'Hasan', 'Matthew', 'Seth', 'Antonio', 'Chester', 'Darren', 'Nicolas', 'Xavier', 'Abraham', 'toulouse' => [
'Jeremiah', 'Joel', 'Alexander', 'David', 'Zachary', 'Shane', 'Matteo', 'Jon', 'Amaan', 'Ryan', 'latitude' => (float) $this->faker->latitude(43.3850, 43.7710),
'Conner', 'Zachariah', 'Josiah', 'Stefan', 'Bryan', 'Alex', 'Aron', 'Charlie', 'Phillip', 'Clayton', 'longitude' => (float) $this->faker->longitude(1.1508, 1.7221)
'Calvin', 'Guy', 'Russell', 'Bailey', 'Kristian', 'Gerald', 'Christian', 'Jak', 'Aiden', 'Homer', 'Brett']; ],
$this->lastnames = [ 'nantes' => [
'Hughes', 'Cassidy', 'Farmer', 'Chavez', 'Santiago', 'Payne', 'Rice', 'Li', 'Hamilton', 'latitude' => (float) $this->faker->latitude(47.0626, 47.3481),
'Singh', 'Simmons', 'Little', 'Mcdaniel', 'Ramsey', 'Garner', 'Harrison', 'Fernandez', 'longitude' => (float) $this->faker->longitude(-1.8415, -1.2840)
'Strickland', 'Read', 'Gilbert', 'Owen', 'Maxwell', 'Schwartz', 'Crawford', 'Hubbard', 'Gibbs', ],
'Wilkes', 'O', 'Lindsey', 'Greene', 'Fisher', 'Park', 'Ortega', 'Manning', 'Logan', 'Woodward',
'Lewis', 'Huff', 'Gill', 'Terry', 'Riley', 'Powers', 'Cole', 'Flores', 'Huang', 'Barrett', 'brest' => [
'Cannon', 'Carr']; 'latitude' => (float) $this->faker->latitude(48.3603, 48.5020),
'longitude' => (float) $this->faker->longitude(-4.6643, -4.2894)
$this->groupnames = [ ],
'Abandoned Thugs', 'Desired Alliance', 'Bitter Devils', 'Killers of the Ocean', 'Relics of Life',
'Dawn of the Horse', 'Birth of Darkness', 'Solar Eagles', 'Rainstars', 'Highcry', 'Mislead Moguls', 'marseille' => [
'Brave Entities', 'Harmonious Anomalies', 'Sorcerers of Tricks', 'Patience of Disease', 'Shepherds of Shadows', 'latitude' => (float) $this->faker->latitude(43.2141, 43.4050),
'Attack of the Fallen', 'Shiver Hearts', 'Killingwell', 'Forgearrows', 'Searing Blow', 'Ordinary Intent', 'longitude' => (float) $this->faker->longitude(5.1876, 5.6723)
'Devoted Punks', 'Grudge of Trust', 'Illusions of the Furious', 'Archers of Promises', 'Witches of Tricks', ],
'Monsterlanders', 'Shimmerskulls', 'Twindeath'
'lille' => [
'latitude' => (float) $this->faker->latitude(50.5046, 50.7399),
'longitude' => (float) $this->faker->longitude(2.8742, 3.2560)
],
'all' => [
'latitude' => (float) $this->faker->latitude(42.4720, 51.0275),
'longitude' => (float) $this->faker->longitude(-1.8786, 6.07)
]
]; ];
$city = $cities[array_rand($cities)];
$location = new Location(null, $city['latitude'], $city['longitude']);
$this->em->persist($location);
$this->em->flush();
return $location;
} }
public function create_user(){ public function create_user(){
$date = new DateTime('now'); $date = new DateTime('now');
$username = $this->usernames[rand(0, count($this->usernames)- 1)]; $username = $this->faker->userName;
$firstname = $this->firstnames[rand(0, count($this->firstnames)- 1)]; $firstname = $this->faker->firstName;
$lastname = $this->lastnames[rand(0, count($this->lastnames)- 1)]; $lastname = $this->faker->lastName;
$mail = $firstname . '.' . $lastname . '@mail.com'; $mail = $firstname . '.' . $lastname . '@mail.com';
$password = password_hash($firstname . $lastname, PASSWORD_DEFAULT); $password = password_hash($firstname . $lastname, PASSWORD_DEFAULT);
$contaminated = rand(0,1) == 0 ? false : true ;
$location = $this->create_localisation();
while ($this->userRepository->findOneBy(array('username' => $username))){ while ($this->userRepository->findOneBy(array('username' => $username))){
$username = $this->usernames[rand(0, count($this->usernames)- 1)]; $username = $this->faker->userName;
} }
$user = new User(null, $username, $firstname, $lastname, $mail, $password, $date); while ($this->userRepository->findOneBy(array('mail' => $mail))){
$mail = $this->faker->email;
}
$user = new User(null, $username, $firstname, $lastname, $mail, $password, $date, $contaminated);
$user->setLocation($location);
$this->em->persist($user); $this->em->persist($user);
$this->em->flush(); $this->em->flush();
} }
public function create_group($groupname){ public function create_group(){
$groupname = $this->groupnames[rand(0, count($this->groupnames)- 1)]; $groupname = $this->faker->words(5, true);
$i = 0; $i = 0;
while ($this->groupRepository->findOneBy(array('name' => $groupname))){ while ($this->groupRepository->findOneBy(array('name' => $groupname))){
$groupname = $this->groupnames[rand(0, count($this->groupnames)- 1)]; $groupname = $this->faker->words(5, true);
} }
$description = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus dui elit, venenatis ac laoreet blandit, pretium at est. $description = $this->faker->text(300);
Cras dignissim vestibulum ornare. Donec eget pharetra dolor. Nunc egestas vitae magna vitae sollicitudin.
Suspendisse sodales suscipit elit vel dignissim. Praesent quis justo purus. Fusce gravida dictum nunc eu eleifend.
Proin id gravida orci, hendrerit egestas diam.';
$adminId = $this->userRepository->find($this->number); $adminId = $this->userRepository->find($this->number);
$group = new Group(null, $groupname, $description, $adminId, rand(0,1)); $group = new Group(null, $groupname, $description, $adminId, rand(0,1));
...@@ -108,30 +132,53 @@ class CreateDB extends Action{ ...@@ -108,30 +132,53 @@ class CreateDB extends Action{
protected function action(): Response protected function action(): Response
{ {
// for($i = 0; $i < count($this->usernames); $i += 1){ ini_set('max_execution_time', '150');
// for($i = 0; $i < 2000; $i += 1){
// $this->create_user(); // $this->create_user();
// } // }
// for($i = 0; $i < count($this->groupnames); $i += 1){ // for($i = 0; $i < 100; $i += 1){
// $this->create_group( $this->groupnames[$i]); // $this->create_group();
// } // }
$group_number = rand(0, count($this->groupnames) - 1); $nb_users = $this->userRepository->createQueryBuilder('u')
for ($i = 0; $i < 100; $i += 1){ ->select('count(u.id)')
$group_number = rand(0, count($this->groupnames) - 1); ->getQuery()
$group = $this->groupRepository->find($group_number); ->getSingleScalarResult();
$currentUser = $this->userRepository->find(rand(5,84)); $nb_groups = $this->groupRepository->createQueryBuilder('g')
while ($group->hasUser($currentUser->getId())){ ->select('count(g.id)')
$group_number = rand(0, count($this->groupnames) - 1); ->getQuery()
$group = $this->groupRepository->find($group_number); ->getSingleScalarResult();
for ($i = 1; $i <= $nb_users; $i += 1){
$groupId = $this->faker->numberBetween(1, $nb_groups);
$group = $this->groupRepository->find($groupId);
$currentUser = $this->userRepository->find(rand(1, (int) $nb_users));
while ($this->findAnUser($currentUser, $group) != null){
$groupId = $this->faker->numberBetween(1, $nb_groups);
$group = $this->groupRepository->find($groupId);
} }
$currentUser->addGroup($group); $currentUser->addGroup($group);
$this->em->persist($group); $this->em->persist($group);
$this->em->flush(); $this->em->flush();
} }
die;
return $this->respondWithData(); return $this->respondWithData();
} }
public function findAnUser(User $user, Group $group){
$qb = $this->em->createQueryBuilder()
->select('g')
->from('App\Domain\Group\Group', 'g')
->where('g.id = :groupID')
->andWhere(':targetUser MEMBER OF g.users')
->setParameter('targetUser', $user)
->setParameter('groupID', $group->getId());
return $qb->getQuery()->getResult();
}
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment