*/ protected function getAllUsers(ObjectManager $manager): array { $all = []; /** @var User[] $entries */ $entries = $manager->getRepository(User::class)->findAll(); foreach ($entries as $temp) { $all[$temp->getId()] = $temp; } return $all; } /** * @param ObjectManager $manager * @return array */ protected function getAllProjects(ObjectManager $manager): array { $all = []; /** @var Project[] $entries */ $entries = $manager->getRepository(Project::class)->findAll(); foreach ($entries as $temp) { $all[$temp->getId()] = $temp; } return $all; } /** * {@inheritdoc} */ public function load(ObjectManager $manager) { $allUsers = $this->getAllUsers($manager); $allProjects = $this->getAllProjects($manager); $faker = Factory::create(); for ($i = 1; $i <= self::AMOUNT_TEAMS; $i++) { $maxUsers = \count($allUsers) - 1; if (self::MAX_USERS_PER_TEAM < $maxUsers) { $maxUsers = self::MAX_USERS_PER_TEAM; } $userCount = mt_rand(1, $maxUsers); $maxProjects = \count($allProjects) - 1; if (self::MAX_PROJECTS_PER_TEAM < $maxProjects) { $maxProjects = self::MAX_PROJECTS_PER_TEAM; } $projectCount = mt_rand(0, $maxProjects); $team = new Team(); $team->setName($faker->company . ' ' . $i); $team->addTeamlead($allUsers[array_rand($allUsers)]); if ($userCount > 0) { $userKeys = array_rand($allUsers, $userCount); if (!\is_array($userKeys)) { $userKeys = [$userKeys]; } foreach ($userKeys as $userKey) { $team->addUser($allUsers[$userKey]); } } if ($projectCount > 0) { $projectKeys = array_rand($allProjects, $projectCount); if (!\is_array($projectKeys)) { $projectKeys = [$projectKeys]; } foreach ($projectKeys as $projectKey) { $team->addProject($allProjects[$projectKey]); } } $manager->persist($team); } $manager->flush(); $manager->clear(Team::class); } }