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

ClubUpdateCommand::execute()   D

Complexity

Conditions 10
Paths 14

Size

Total Lines 46
Code Lines 30

Duplication

Lines 14
Ratio 30.43 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 14
loc 46
rs 4.983
cc 10
eloc 30
nc 14
nop 2

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace KI\UserBundle\Command;
4
5
use KI\UserBundle\Entity\Club;
6
use KI\UserBundle\Entity\ClubUser;
7
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
8
use Symfony\Component\Console\Input\InputArgument;
9
use Symfony\Component\Console\Input\InputOption;
10
use Symfony\Component\Console\Input\InputInterface;
11
use Symfony\Component\Console\Output\OutputInterface;
12
13
class ClubUpdateCommand extends ContainerAwareCommand
14
{
15
    protected function configure()
16
    {
17
        $this
18
            ->setName('upont:update:clubs')
19
            ->setDescription('Enable or Disable listed clubs according to the existence of members in current associative promo')
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 129 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...
20
            ->addArgument('clubs', InputArgument::IS_ARRAY | InputArgument::OPTIONAL, 'Clubs to update')
21
            ->addOption('all', 'a', InputOption::VALUE_NONE, 'Update all clubs')
22
            ->addOption('preview', 'p', InputOption::VALUE_NONE, 'Show clubs to be updated without modification')
23
        ;
24
    }
25
26
    protected function execute(InputInterface $input, OutputInterface $output)
27
    {
28
        $em = $this->getContainer()->get('doctrine')->getManager();
29
        $userRepo = $this->getContainer()->get('doctrine')->getRepository(Club::class);
30
        $clubUserRepo = $this->getContainer()->get('doctrine')->getRepository(ClubUser::class);
31
        $assoPromo = $this->getContainer()->getParameter('upont')['promos']['assos'];
32
        $clubSlugs = $input->getArgument('clubs');
33
34
        if ($input->getOption('all')) {
35
            $clubsToUpdate = $userRepo->findAll();
36
        }
37
        else {
38
            $clubsToUpdate = array_map([$userRepo, 'findOneBySlug'], $clubSlugs);
39
        }
40
41
        $clubNumber = -1;
42
        foreach ($clubsToUpdate as $clubToUpdate) {
43
            $clubNumber++;
44
            if (count($clubToUpdate) == 0) {
45
                $output->writeln('<error>The slug "'.$clubSlugs[$clubNumber].'" doesn\'t match with any club</error>');
46
                continue;
47
            }
48
            $countUsers = $clubUserRepo->getCountUsersInClubWithPromo($clubToUpdate, $assoPromo);
49
50
            if ($countUsers == 0 && $clubToUpdate->getActive()) {
51 View Code Duplication
                if ($input->getOption('preview')) {
52
                    $output->writeln('<comment>'.$clubToUpdate->getFullName().' to be disabled'.'</comment>');
53
                }
54
                else {
55
                    $clubToUpdate->setActive(false);
56
                    $output->writeln('<comment>'.$clubToUpdate->getFullName().' disabled'.'</comment>');
57
                }
58
            }
59
            else if ($countUsers > 0 && !$clubToUpdate->getActive()) {
60 View Code Duplication
                if ($input->getOption('preview')) {
61
                    $output->writeln('<info>'.$clubToUpdate->getFullName().' to be enabled'.'</info>');
62
                }
63
                else {
64
                    $clubToUpdate->setActive(true);
65
                    $output->writeln('<info>'.$clubToUpdate->getFullName().' enabled'.'</info>');
66
                }
67
            }
68
        }
69
70
        $em->flush();
71
    }
72
}
73