SyncSchemaCommand   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 11

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
c 1
b 0
f 0
lcom 0
cbo 11
dl 0
loc 63
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 7 1
B execute() 0 46 5
1
<?php
2
3
namespace Padam87\AttributeBundle\Command;
4
5
use Doctrine\Common\Annotations\AnnotationReader;
6
use Doctrine\Common\Persistence\ManagerRegistry;
7
use Doctrine\Common\Persistence\Mapping\ClassMetadata;
8
use Padam87\AttributeBundle\Entity\Schema;
9
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
10
use Symfony\Component\Console\Helper\Table;
11
use Symfony\Component\Console\Helper\TableSeparator;
12
use Symfony\Component\Console\Input\InputInterface;
13
use Symfony\Component\Console\Output\OutputInterface;
14
15
class SyncSchemaCommand extends ContainerAwareCommand
16
{
17
    /**
18
     * {@inheritdoc}
19
     */
20
    protected function configure()
21
    {
22
        $this
23
            ->setName('eav:schema:sync')
24
            ->setDescription('Syncs the existing schemas in the database with the current metadata')
25
        ;
26
    }
27
28
    /**
29
     * {@inheritdoc}
30
     */
31
    protected function execute(InputInterface $input, OutputInterface $output)
32
    {
33
        $reader = new AnnotationReader();
34
        /** @var ManagerRegistry $doctrine */
35
        $doctrine = $this->getContainer()->get('doctrine');
36
        $em = $doctrine->getManager();
37
        $cmf = $em->getMetadataFactory();
38
39
        $existing = [];
40
        $created = [];
41
42
        /** @var ClassMetadata $metadata */
43
        foreach ($cmf->getAllMetadata() as $metadata) {
44
            $refl = $metadata->getReflectionClass();
45
46
            if ($refl === null) {
47
                $refl = new \ReflectionClass($metadata->getName());
48
            }
49
50
            if ($reader->getClassAnnotation($refl, 'Padam87\AttributeBundle\Annotation\Entity') != null) {
51
                $schema = $em->getRepository('Padam87AttributeBundle:Schema')->findOneBy([
52
                    'className' => $metadata->getName(),
53
                ]);
54
55
                if ($schema === null) {
56
                    $schema = new Schema();
57
                    $schema->setClassName($metadata->getName());
58
59
                    $em->persist($schema);
60
                    $em->flush($schema);
61
62
                    $created[] = $metadata->getName();
63
                } else {
64
                    $existing[] = $metadata->getName();
65
                }
66
            }
67
        }
68
69
        $table = new Table($output);
70
71
        $table->addRow(['Created:', implode(PHP_EOL, $created)]);
72
        $table->addRow(new TableSeparator());
73
        $table->addRow(['Existing:', implode(PHP_EOL, $existing)]);
74
75
        $table->render();
76
    }
77
}
78