コード例 #1
0
 /**
  * {@inheritdoc}
  */
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     $output->writeln('<info>Starting former student synchronization process.</info>');
     $filter = $input->getArgument('filter');
     $formerStudents = $this->directory->findByLdapFilter($filter);
     if (!$formerStudents) {
         $output->writeln("<error>{$filter} returned no results.</error>");
         return;
     }
     $output->writeln('<info>Found ' . count($formerStudents) . ' former students in the directory.</info>');
     $formerStudentsCampusIds = array_map(function (array $arr) {
         return $arr['campusId'];
     }, $formerStudents);
     $notFormerStudents = $this->userManager->findUsersWhoAreNotFormerStudents($formerStudentsCampusIds);
     $usersToUpdate = $notFormerStudents->filter(function (UserInterface $user) {
         return !$user->isUserSyncIgnore();
     });
     if (!$usersToUpdate->count() > 0) {
         $output->writeln("<info>There are no students to update.</info>");
         return;
     }
     $output->writeln('<info>There are ' . $usersToUpdate->count() . ' students in Ilios who will be marked as a Former Student.</info>');
     $rows = $usersToUpdate->map(function (UserInterface $user) {
         return [$user->getCampusId(), $user->getFirstName(), $user->getLastName(), $user->getEmail()];
     })->toArray();
     $table = new Table($output);
     $table->setHeaders(array('Campus ID', 'First', 'Last', 'Email'))->setRows($rows);
     $table->render();
     $helper = $this->getHelper('question');
     $output->writeln('');
     $question = new ConfirmationQuestion('<question>Do you wish to mark these users as Former Students? </question>' . "\n", true);
     if ($helper->ask($input, $output, $question)) {
         $formerStudentRole = $this->userRoleManager->findUserRoleBy(array('title' => 'Former Student'));
         foreach ($usersToUpdate as $user) {
             $formerStudentRole->addUser($user);
             $user->addRole($formerStudentRole);
             $this->userManager->updateUser($user, false);
         }
         $this->userRoleManager->updateUserRole($formerStudentRole);
         $output->writeln('<info>Former students updated successfully!</info>');
     } else {
         $output->writeln('<comment>Update canceled,</comment>');
     }
 }