ValidateEntityCommand   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Importance

Changes 5
Bugs 1 Features 1
Metric Value
eloc 30
c 5
b 1
f 1
dl 0
loc 65
rs 10
wmc 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 14 1
B execute() 0 15 7
A getAllEntities() 0 14 2
1
<?php
2
/**
3
 * @author Gerard van Helden <[email protected]>
4
 * @copyright Zicht Online <http://zicht.nl>
5
 */
6
namespace Zicht\Bundle\FrameworkExtraBundle\Command;
7
8
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
9
use Symfony\Component\Console\Input\InputArgument;
10
use Symfony\Component\Console\Input\InputOption;
11
use Symfony\Component\Console\Output\OutputInterface;
12
use Symfony\Component\Console\Input\InputInterface;
13
/* @codingStandardsIgnoreStart*/
14
use function Zicht\Itertools\filter;
15
/* @codingStandardsIgnoreEnd */
16
17
/**
18
 * Class ValidateEntityCommand
19
 *
20
 * @package Zicht\Bundle\FrameworkExtraBundle\Command
21
 */
22
class ValidateEntityCommand extends ContainerAwareCommand
23
{
24
    /**
25
     * @{inheritDoc}
26
     */
27
    protected function configure()
28
    {
29
        $this
30
            ->setName('zicht:entity:validate')
31
            ->addArgument('entity', InputArgument::IS_ARRAY|InputArgument::OPTIONAL)
32
            ->setHelp(
33
                'This command validates all entities in a repository, 
34
                useful to test the database for irregularities'
35
            )
36
            ->addOption(
37
                'group',
38
                'g',
39
                InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY,
40
                "Optional validation group(s)"
41
            );
42
    }
43
44
    /**
45
     * @{inheritDoc}
46
     */
47
    protected function execute(InputInterface $input, OutputInterface $output)
48
    {
49
        $entities = $input->getArgument('entity') ?: $this->getAllEntities();
50
51
        foreach ($entities as $entity) {
52
            $groups = $input->getOption('group');
53
            $repo = $this->getContainer()->get('doctrine')->getRepository($entity);
54
55
            foreach ($repo->findAll() as $entity) {
0 ignored issues
show
Comprehensibility Bug introduced by
$entity is overwriting a variable from outer foreach loop.
Loading history...
56
                $violations = $this->getContainer()->get('validator')->validate($entity, $groups ? $groups : null);
57
58
                if (count($violations)) {
59
                    $output->writeln(get_class($entity) . "::" . $entity->getId());
60
                    foreach ($violations as $error) {
61
                        $output->writeln(" -> {$error}");
62
                    }
63
                }
64
            }
65
        }
66
    }
67
68
    /**
69
     * get all entities
70
     *
71
     * @return \Zicht\Itertools\lib\FilterIterator
72
     */
73
    protected function getAllEntities()
74
    {
75
        $allMeta = $this
76
            ->getContainer()
77
            ->get('doctrine')
78
            ->getManager()
79
            ->getMetadataFactory()
80
            ->getAllMetadata();
81
82
        $isAcceptable = function ($meta) {
83
            return !$meta->isMappedSuperclass && empty($meta->subClasses);
84
        };
85
86
        return filter($isAcceptable, $allMeta);
87
    }
88
}
89