RepairNestedTreeCommand::execute()   A
last analyzed

Complexity

Conditions 5
Paths 6

Size

Total Lines 27
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 2
Metric Value
cc 5
eloc 21
c 2
b 1
f 2
nc 6
nop 2
dl 0
loc 27
rs 9.2728
1
<?php
2
/**
3
 * @author Rik van der Kemp <[email protected]>
4
 * @copyright Zicht Online <http://www.zicht.nl>
5
 */
6
7
namespace Zicht\Bundle\FrameworkExtraBundle\Command;
8
9
use Gedmo\Tree\Entity\Repository\NestedTreeRepository;
0 ignored issues
show
Bug introduced by
The type Gedmo\Tree\Entity\Repository\NestedTreeRepository was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
10
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
11
use Symfony\Component\Console\Input\InputArgument;
12
use Symfony\Component\Console\Input\InputInterface;
13
use Symfony\Component\Console\Input\InputOption;
14
use Symfony\Component\Console\Output\OutputInterface;
15
16
/**
17
 * Checks if a NestedTree set is verified (correct), if not repairs the tree
18
 * using the NestedSet methods.
19
 *
20
 * @package Zicht\Bundle\FrameworkExtraBundle\Command
21
 */
22
class RepairNestedTreeCommand extends ContainerAwareCommand
23
{
24
    /**
25
     * This is const because it's referred by the exception thrown when a validation error occurs
26
     */
27
    const COMMAND_NAME = 'zicht:repair:nested-tree';
28
29
    /**
30
     * @{inheritDoc}
31
     */
32
    protected function configure()
33
    {
34
        $this
35
            ->setName(self::COMMAND_NAME)
36
            ->setDescription('Repair a NestedTreeSet')
37
            ->addOption('dry-run', 'd', InputOption::VALUE_NONE, 'Do a dry run, i.e. only report the problems without doing any changes')
38
            ->addArgument('entity', InputArgument::REQUIRED, 'The entity to be repaired, must be of nested tree set. E.g. ZichtMenuBundle:MenuItem')
39
            ->setHelp(
40
                'This command will try to repair broken nested set.' . PHP_EOL .
41
                'Pass --dry-run to see if the nested set is broken (use --verbose to see which items are broken)' . PHP_EOL
42
            );
43
    }
44
45
    /**
46
     * @{inheritDoc}
47
     */
48
    protected function execute(InputInterface $input, OutputInterface $output)
49
    {
50
        $doctrine = $this->getContainer()->get('doctrine');
51
        $entity = $input->getArgument('entity');
52
        $formatter = $this->getHelperSet()->get('formatter');
53
        $repository = $doctrine->getRepository($entity);
54
55
        if ($repository instanceof NestedTreeRepository) {
56
            if (true !== ($issues = $repository->verify())) {
57
                if ($output->getVerbosity() > 0) {
58
                    $output->writeln("Errors found in tree:");
59
                    $output->writeln($formatter->formatBlock($issues, 'comment', true));
0 ignored issues
show
Bug introduced by
The method formatBlock() does not exist on Symfony\Component\Console\Helper\Helper. It seems like you code against a sub-type of Symfony\Component\Console\Helper\Helper such as Symfony\Component\Console\Helper\FormatterHelper. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

59
                    $output->writeln($formatter->/** @scrutinizer ignore-call */ formatBlock($issues, 'comment', true));
Loading history...
60
                } else {
61
                    $output->writeln("Errors found in tree");
62
                }
63
                if ($input->getOption('dry-run')) {
64
                    $output->writeln("Dry run, so no changes are made");
65
                } else {
66
                    $output->writeln("Recovering");
67
                    $repository->recover();
68
                    $doctrine->getManager()->flush();
69
                }
70
            } else {
71
                $output->writeln("No issues found");
72
            }
73
        } else {
74
            $output->writeln("Given entity is not of instance NestedTreeRepository");
75
        }
76
    }
77
}
78