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

ClubUpdateCommand   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 60
Duplicated Lines 23.33 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 11
c 1
b 0
f 0
lcom 1
cbo 4
dl 14
loc 60
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 10 1
D execute() 14 46 10

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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