Completed
Push — master ( cc9b98...5f169a )
by Louis
14s
created

DepartmentUpdateCommand::configure()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 10
rs 9.4285
cc 1
eloc 7
nc 1
nop 0
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')
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 171 characters

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.

Loading history...
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')
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 198 characters

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.

Loading history...
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>');
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 190 characters

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.

Loading history...
53
    }
54
}
55