1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace KI\UserBundle\Command; |
4
|
|
|
|
5
|
|
|
use KI\UserBundle\Entity\User; |
6
|
|
|
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; |
7
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
8
|
|
|
use Symfony\Component\Console\Input\InputOption; |
9
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
10
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
11
|
|
|
|
12
|
|
|
class DepartmentUpdateCommand extends ContainerAwareCommand |
13
|
|
|
{ |
14
|
|
|
protected function configure() |
15
|
|
|
{ |
16
|
|
|
$this |
17
|
|
|
->setName('upont:update:department') |
18
|
|
|
->setDescription('Update users\' departments for the given list of usernames : "username1,username2..."') |
19
|
|
|
->addArgument('department', InputArgument::REQUIRED, 'The department.') |
20
|
|
|
->addArgument('usernames', InputArgument::REQUIRED, 'The usernames of the users whose departments are to be updated or the file that contains it if -f is set') |
|
|
|
|
21
|
|
|
->addOption('file', 'f', InputOption::VALUE_NONE, 'If set, the absolute path of the file that contains the list of usernames must be given as second argument instead of the list itself') |
|
|
|
|
22
|
|
|
; |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
26
|
|
|
{ |
27
|
|
|
$em = $this->getContainer()->get('doctrine')->getManager(); |
28
|
|
|
$repo = $this->getContainer()->get('doctrine')->getRepository(User::class); |
29
|
|
|
$successCount = 0; |
30
|
|
|
|
31
|
|
|
if ($input->getOption('file')) { |
32
|
|
|
$list = fopen($input->getArgument('usernames'), 'r+'); |
33
|
|
|
$usernames = str_replace(["\r", "\n"], ['', ''], fgets($list)); |
34
|
|
|
$usernameArray = explode(',', $usernames); |
35
|
|
|
} |
36
|
|
|
else { |
37
|
|
|
$usernameArray = explode(',', $input->getArgument('usernames')); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
foreach ($usernameArray as $username) { |
41
|
|
|
$user = $repo->findOneByUsername($username); |
42
|
|
|
if ($user) { |
43
|
|
|
$user->setDepartment($input->getArgument('department')); |
44
|
|
|
$successCount++; |
45
|
|
|
} |
46
|
|
|
else { |
47
|
|
|
$output->writeln('<error>Username '.$username.' n\'existe pas</error>'); |
48
|
|
|
} |
49
|
|
|
} |
50
|
|
|
$em->flush(); |
51
|
|
|
|
52
|
|
|
$output->writeln('<comment>'.$input->getArgument('department').' : '.$successCount.' élève'.($successCount >= 2 ? 's' : '').' sur '.count($usernameArray).' mis à jour'.'</comment>'); |
|
|
|
|
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
|
Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.