AliasCommand::execute()   B
last analyzed

Complexity

Conditions 10
Paths 160

Size

Total Lines 44
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 110

Importance

Changes 0
Metric Value
cc 10
eloc 28
nc 160
nop 2
dl 0
loc 44
ccs 0
cts 35
cp 0
crap 110
rs 7.1666
c 0
b 0
f 0

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
 * @author Gerard van Helden <[email protected]>
4
 * @copyright Zicht Online <http://zicht.nl>
5
 */
6
namespace Zicht\Bundle\PageBundle\Command;
7
8
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
9
use Symfony\Component\Console\Helper\ProgressBar;
10
use Symfony\Component\Console\Input\InputArgument;
11
use Symfony\Component\Console\Input\InputOption;
12
use Symfony\Component\Console\Output\OutputInterface;
13
use Symfony\Component\Console\Input\InputInterface;
14
use Zicht\Bundle\UrlBundle\Aliasing\Aliasing;
15
16
/**
17
 * Generates URL aliases for all pages.
18
 */
19
class AliasCommand extends ContainerAwareCommand
20
{
21
    /**
22
     * @{inheritDoc}
23
     */
24 2
    protected function configure()
25
    {
26 2
        $this->setDescription("Creates aliases for all pages")
27 2
            ->setName('zicht:page:alias')
28 2
            ->addArgument('entity', InputArgument::OPTIONAL, 'Only do a specific entity')
29 2
            ->addOption('where', 'w', InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, 'Add a WHERE query')
30 2
            ->addOption('move', '', InputOption::VALUE_NONE, 'Force regeneration (use MOVE strategy for new aliases)');
31 2
    }
32
33
    /**
34
     * @{inheritDoc}
35
     */
36
    protected function execute(InputInterface $input, OutputInterface $output)
37
    {
38
        $aliaser = $this->getContainer()->get('zicht_page.page_aliaser');
39
40
        if ($input->getOption('move')) {
41
            $aliaser->setConflictingInternalUrlStrategy(Aliasing::STRATEGY_MOVE_PREVIOUS_TO_NEW);
42
        } else {
43
            $aliaser->setConflictingInternalUrlStrategy(Aliasing::STRATEGY_IGNORE);
44
        }
45
46
        $onDone = $aliaser->setIsBatch(true);
47
48
        if ($entity = $input->getArgument('entity')) {
49
            $repo = $this->getContainer()->get('doctrine')->getRepository($entity);
50
        } else {
51
            $repo = $this->getContainer()->get('zicht_page.page_manager')->getBaseRepository();
52
        }
53
        $q = $repo->createQueryBuilder('p');
54
55
        foreach ($input->getOption('where') as $filter) {
56
            $q->andWhere($filter);
57
        }
58
59
        if ($output->getVerbosity() == OutputInterface::VERBOSITY_VERBOSE) {
60
            $output->writeln("Querying records ...");
61
        }
62
        $items = $q->getQuery()->execute();
63
64
        $progress = new ProgressBar($output, count($items));
65
        if ($output->getVerbosity() == OutputInterface::VERBOSITY_VERBOSE) {
66
            $progress->display();
67
        }
68
        foreach ($items as $page) {
69
            $progress->advance(1);
70
            if ($output->getVerbosity() > OutputInterface::VERBOSITY_VERBOSE) {
71
                $output->writeln("Aliasing page \"{$page}\"");
72
            }
73
            $result = $aliaser->createAlias($page);
74
            if ($output->getVerbosity() > OutputInterface::VERBOSITY_VERBOSE) {
75
                $output->writeln(" -> " . ($result ? '[created]' : '[already aliased]'));
76
            }
77
        }
78
        $progress->finish();
79
        call_user_func($onDone);
80
    }
81
}
82