diff --git a/src/Application/Actions/Search/CreateDB.php b/src/Application/Actions/Search/CreateDB.php index 3927d909a3517d5e158eca539e3bc0f3dabcdb6f..a8995395c7516d05027cd0678262d0d2ecb94e0c 100644 --- a/src/Application/Actions/Search/CreateDB.php +++ b/src/Application/Actions/Search/CreateDB.php @@ -1,7 +1,8 @@ <?php declare(strict_types=1); - namespace App\Application\Actions\Search; +// require_once 'vendor/autoload.php'; +// require_once 'vendor/fazninotto/src/Faker/autoload.php'; use App\Application\Actions\Action; use Psr\Http\Message\ResponseInterface as Response; @@ -12,92 +13,115 @@ use Slim\Views\Twig; use Slim\Flash\Messages; use App\Domain\User\User ; use App\Domain\Group\Group ; - +use App\Domain\Location\Location ; +use Faker; use DateTime; class CreateDB extends Action{ public function __construct(LoggerInterface $logger, EntityManager $em, Twig $twig, Messages $flash){ - + parent::__construct($logger); $this->em = $em; $this->userRepository = $em->getRepository('App\Domain\User\User'); $this->groupRepository = $em->getRepository('App\Domain\Group\Group'); $this->number = rand(0,50); + $this->faker = Faker\Factory::create(); - $this->usernames = [ - 'Rerunner', 'Poltergeisha', 'Urchinchilla', 'Dragonightmare', 'MerryLarva', 'GrizzlySeal', - 'ExoticHorse', 'RapidOstrich', 'MerryPig', 'OriginalLark', - 'Sailorc', 'Toucanoe', 'Geckoco', 'Wrathhawk', 'IdenticalArmadillo', 'RedStitches', 'DeliriousSparks', - 'PrettySwan', 'FirstDoughnut', 'HandsomePrincess', 'Sultank', 'Specterminate', 'Troutwards', 'Bullfrogert', - 'AdviseHurricane', 'ThroneFishy', 'BreakfastSheep', 'GloomySaint', 'FoolishAntelope', 'ArcticMutant', - 'Devile', 'Sailord', 'Unbandit', 'Aromaster', 'ThunderApe', 'MysteryMoose', 'GrumpyHooper', 'ConsultChick', - 'FrighteningRaider', 'FurApe', 'Herogue', 'Beastern', 'Bingoddess', 'Orangutango', 'FlameButterfly', - 'AcrobaticPanther', 'MessyCherry', 'SleepingEmu', 'CannonLord', 'ActionWombat', 'SwallowRider', - 'Orangutitan', 'Parascythe', 'Sharctic', 'AmazingPetal', 'BadMeerkat', 'SmallFry', 'InsecureHydra', - 'WarFoal', 'LostBloodElf', 'Ostritches', 'Eyewearwolf', 'Gorillala', 'Gangstereo', 'BigCat', - 'CopperQueen', 'SpotlessBot', 'ForceHerring', 'AmusingFledgling', 'HeartHatchling', - 'Dinosause', 'Phantomfool', 'Woodpecan', 'Cowhale', 'PunyPirate', 'BakingPup', 'LoneOyster', 'AmazingHooper', - 'ControlWarhawk', 'FlameRogue' ]; - - $this->firstnames = [ - 'Hana', 'Zoya', 'Willie', 'Nettie', 'Kara', 'Lara', 'Halima', 'Laila', 'Alicia', 'Caroline', - 'Carla', 'Julie', 'Katherine', 'Holly', 'Rebekah', 'Lachlan', 'Millie', 'Chantelle', 'Robin', 'Aminah', - 'Ashley', 'Fern', 'Agnes', 'Harley', 'Rhiannon', 'Fannie', 'Brianna', 'Niamh', 'Tanisha', 'Sally', - 'Amelia', 'Molly', 'Aliyah', 'Violet', 'Aisha', 'Libby', 'Alana', 'Danielle', 'Tiana', 'Zaynab', - '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', - 'Jeremiah', 'Joel', 'Alexander', 'David', 'Zachary', 'Shane', 'Matteo', 'Jon', 'Amaan', 'Ryan', - 'Conner', 'Zachariah', 'Josiah', 'Stefan', 'Bryan', 'Alex', 'Aron', 'Charlie', 'Phillip', 'Clayton', - 'Calvin', 'Guy', 'Russell', 'Bailey', 'Kristian', 'Gerald', 'Christian', 'Jak', 'Aiden', 'Homer', 'Brett']; - - $this->lastnames = [ - 'Hughes', 'Cassidy', 'Farmer', 'Chavez', 'Santiago', 'Payne', 'Rice', 'Li', 'Hamilton', - 'Singh', 'Simmons', 'Little', 'Mcdaniel', 'Ramsey', 'Garner', 'Harrison', 'Fernandez', - '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', - 'Cannon', 'Carr']; - - $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', - 'Brave Entities', 'Harmonious Anomalies', 'Sorcerers of Tricks', 'Patience of Disease', 'Shepherds of Shadows', - 'Attack of the Fallen', 'Shiver Hearts', 'Killingwell', 'Forgearrows', 'Searing Blow', 'Ordinary Intent', - 'Devoted Punks', 'Grudge of Trust', 'Illusions of the Furious', 'Archers of Promises', 'Witches of Tricks', - 'Monsterlanders', 'Shimmerskulls', 'Twindeath' + } + + public function create_localisation(){ + $cities = [ + 'nancy' => [ + 'latitude' => (float) $this->faker->latitude(48.61, 48.72), + 'longitude' => (float) $this->faker->longitude(6.098, 6.25) + ], + 'paris' => [ + 'latitude' => (float) $this->faker->latitude(48.64, 49.1116), + 'longitude' => (float) $this->faker->longitude(1.9239, 2.7589) + ], + 'center' => [ + 'latitude' => (float) $this->faker->latitude(43.8899, 48.3051), + 'longitude' => (float) $this->faker->longitude(-1.1260, 5.3228) + ], + + 'lyon' => [ + 'latitude' => (float) $this->faker->latitude(45.6178, 45.9702), + 'longitude' => (float) $this->faker->longitude(4.6266, 5.1210) + ], + + 'toulouse' => [ + 'latitude' => (float) $this->faker->latitude(43.3850, 43.7710), + 'longitude' => (float) $this->faker->longitude(1.1508, 1.7221) + ], + + 'nantes' => [ + 'latitude' => (float) $this->faker->latitude(47.0626, 47.3481), + 'longitude' => (float) $this->faker->longitude(-1.8415, -1.2840) + ], + + 'brest' => [ + 'latitude' => (float) $this->faker->latitude(48.3603, 48.5020), + 'longitude' => (float) $this->faker->longitude(-4.6643, -4.2894) + ], + + 'marseille' => [ + 'latitude' => (float) $this->faker->latitude(43.2141, 43.4050), + 'longitude' => (float) $this->faker->longitude(5.1876, 5.6723) + ], + + '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(){ $date = new DateTime('now'); - $username = $this->usernames[rand(0, count($this->usernames)- 1)]; - $firstname = $this->firstnames[rand(0, count($this->firstnames)- 1)]; - $lastname = $this->lastnames[rand(0, count($this->lastnames)- 1)]; + $username = $this->faker->userName; + $firstname = $this->faker->firstName; + $lastname = $this->faker->lastName; $mail = $firstname . '.' . $lastname . '@mail.com'; $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))){ - $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->flush(); } - public function create_group($groupname){ - $groupname = $this->groupnames[rand(0, count($this->groupnames)- 1)]; + public function create_group(){ + $groupname = $this->faker->words(5, true); $i = 0; 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. - 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.'; + $description = $this->faker->text(300); $adminId = $this->userRepository->find($this->number); $group = new Group(null, $groupname, $description, $adminId, rand(0,1)); @@ -108,30 +132,53 @@ class CreateDB extends Action{ 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(); // } - // for($i = 0; $i < count($this->groupnames); $i += 1){ - // $this->create_group( $this->groupnames[$i]); + // for($i = 0; $i < 100; $i += 1){ + // $this->create_group(); // } - $group_number = rand(0, count($this->groupnames) - 1); - for ($i = 0; $i < 100; $i += 1){ - $group_number = rand(0, count($this->groupnames) - 1); - $group = $this->groupRepository->find($group_number); - $currentUser = $this->userRepository->find(rand(5,84)); - while ($group->hasUser($currentUser->getId())){ - $group_number = rand(0, count($this->groupnames) - 1); - $group = $this->groupRepository->find($group_number); + $nb_users = $this->userRepository->createQueryBuilder('u') + ->select('count(u.id)') + ->getQuery() + ->getSingleScalarResult(); + $nb_groups = $this->groupRepository->createQueryBuilder('g') + ->select('count(g.id)') + ->getQuery() + ->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); $this->em->persist($group); $this->em->flush(); } + die; 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(); + } }