CleanAliasCommand::configure()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 0
dl 0
loc 6
ccs 0
cts 6
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * @author Rik van der Kemp <[email protected]>
4
 * @copyright Zicht Online <http://www.zicht.nl>
5
 */
6
namespace Zicht\Bundle\PageBundle\Command;
7
8
use Doctrine\DBAL\Connection;
9
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
10
use Symfony\Component\Console\Helper\Table;
11
use Symfony\Component\Console\Input\InputArgument;
12
use Symfony\Component\Console\Input\InputInterface;
13
use Symfony\Component\Console\Output\OutputInterface;
14
use Symfony\Component\Console\Question\ConfirmationQuestion;
15
16
/**
17
 * Class CleanAliasCommand
18
 *
19
 * Remove aliases referencing pages that do not exist anymore.
20
 */
21
class CleanAliasCommand extends ContainerAwareCommand
22
{
23
    /**
24
     * {@inheritDoc}
25
     */
26
    protected function configure()
27
    {
28
        $this
29
            ->setName('zicht:page:clean:alias')
30
            ->addArgument('locale', InputArgument::REQUIRED, 'Page locale to clean')
31
            ->setDescription('Clean aliases that are not attached to pages any more.');
32
    }
33
34
    /**
35
     * {@inheritDoc}
36
     */
37
    protected function execute(InputInterface $input, OutputInterface $output)
38
    {
39
        $locale = $input->getArgument('locale');
40
41
        /** @var Connection $connection */
42
        $connection = $this->getContainer()->get('doctrine')->getConnection();
43
        $stmt = $connection->prepare(
44
            'SELECT 
45
                page.id as original_page_id, 
46
                url_alias.id, 
47
                url_alias.internal_url, 
48
                url_alias.public_url, 
49
                REPLACE(url_alias.internal_url, :url_pre_id, "") AS page_id 
50
            FROM url_alias 
51
            LEFT JOIN page ON page.id = REPLACE(url_alias.internal_url, :url_pre_id, \'\') 
52
            WHERE internal_url LIKE :url_like
53
            AND page.id IS NULL'
54
        );
55
        $stmt->execute(
56
            [
57
                ':url_pre_id' => sprintf('/%s/page/', $locale),
58
                ':url_like' => sprintf('/%s/page/', sprintf('/%s/page/', $locale) . '%'),
59
            ]
60
        );
61
62
        $table = new Table($output);
63
        $table->setHeaders(['Alias', 'Internal URL', 'Stripped Id', 'Original Page Id']);
64
65
        $records = $stmt->fetchAll();
66
67
        if (!count($records)) {
68
            $output->writeln('No obsolete aliases found!');
69
            return 0;
70
        }
71
72
        foreach ($records as $record) {
73
            $table->addRow(
74
                [
75
                   $record['public_url'],
76
                   $record['internal_url'],
77
                   $record['page_id'],
78
                   $record['original_page_id'],
79
                ]
80
            );
81
        }
82
83
        $table->render();
84
85
        $helper = $this->getHelper('question');
86
        $confirm = new ConfirmationQuestion('Would you like to remove the above obsolete url aliases? [Y/n] : ', false);
87
88
        if (!$helper->ask($input, $output, $confirm)) {
89
            $output->writeln('Okay, bye');
90
            return 0;
91
        }
92
93
        $stmt->execute();
94
95
        $aliasIds = [];
96
        foreach ($stmt->fetchAll() as $record) {
97
            $aliasIds[] = $record['id'];
98
        }
99
100
        $connection->query(sprintf('DELETE FROM url_alias WHERE id IN (%s)', implode(', ', $aliasIds)));
101
        $output->writeln(sprintf('Removed %d aliases', count($records)));
102
103
        return 0;
104
    }
105
}
106