ValidateCommand::configure()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 0
cts 11
cp 0
rs 9.9
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\ODM\MongoDB\Tools\Console\Command\Schema;
6
7
use Doctrine\Common\Cache\VoidCache;
8
use Doctrine\ODM\MongoDB\DocumentManager;
9
use Symfony\Component\Console\Command\Command;
10
use Symfony\Component\Console\Input\InputInterface;
11
use Symfony\Component\Console\Output\OutputInterface;
12
use function serialize;
13
use function sprintf;
14
use function unserialize;
15
16
class ValidateCommand extends Command
17
{
18
    /**
19
     * @see \Symfony\Component\Console\Command\Command
20
     */
21
    protected function configure()
22
    {
23
        $this
24
            ->setName('odm:schema:validate')
25
            ->setDescription('Validates if document mapping stays the same after serializing into cache.')
26
            ->setDefinition([])
27
            ->setHelp(<<<EOT
28
Validates if document mapping stays the same after serializing into cache.
29
EOT
30
            );
31
    }
32
33
    /**
34
     * @see \Symfony\Component\Console\Command\Command
35
     */
36
    protected function execute(InputInterface $input, OutputInterface $output)
37
    {
38
        /** @var DocumentManager $dm */
39
        $dm              = $this->getHelper('documentManager')->getDocumentManager();
40
        $metadataFactory = $dm->getMetadataFactory();
41
        $metadataFactory->setCacheDriver(new VoidCache());
42
43
        $errors = 0;
44
        foreach ($metadataFactory->getAllMetadata() as $meta) {
45
            if ($meta === unserialize(serialize($meta))) {
46
                continue;
47
            }
48
49
            ++$errors;
50
            $output->writeln(sprintf('%s has mapping issues.', $meta->getName()));
51
        }
52
        if ($errors) {
53
            $output->writeln(sprintf('<error>%d document(s) have mapping issues.</error>', $errors));
54
        } else {
55
            $output->writeln('All documents are OK!');
56
        }
57
58
        return $errors ? 255 : 0;
59
    }
60
}
61