AliasCommand   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Test Coverage

Coverage 16.66%

Importance

Changes 0
Metric Value
eloc 34
dl 0
loc 61
ccs 7
cts 42
cp 0.1666
rs 10
c 0
b 0
f 0
wmc 11

2 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 7 1
B execute() 0 44 10
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