Passed
Pull Request — master (#51)
by Damien
02:21
created

UpdateSchemaCommand   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 120
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 53
dl 0
loc 120
rs 10
c 0
b 0
f 0
wmc 11

4 Methods

Rating   Name   Duplication   Size   Complexity  
A unlock() 0 3 1
A setContainer() 0 3 1
A configure() 0 5 1
B execute() 0 94 8
1
<?php
2
3
namespace DH\DoctrineAuditBundle\Command;
4
5
use DH\DoctrineAuditBundle\AuditManager;
6
use DH\DoctrineAuditBundle\Exception\UpdateException;
7
use DH\DoctrineAuditBundle\Helper\UpdateHelper;
8
use Doctrine\DBAL\Connection;
9
use Doctrine\DBAL\Schema\AbstractSchemaManager;
10
use Doctrine\ORM\EntityManager;
11
use Symfony\Bridge\Doctrine\RegistryInterface;
12
use Symfony\Component\Console\Command\Command;
13
use Symfony\Component\Console\Command\LockableTrait;
14
use Symfony\Component\Console\Helper\ProgressBar;
15
use Symfony\Component\Console\Input\InputInterface;
16
use Symfony\Component\Console\Output\OutputInterface;
17
use Symfony\Component\Console\Style\SymfonyStyle;
18
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
19
use Symfony\Component\DependencyInjection\ContainerInterface;
20
21
class UpdateSchemaCommand extends Command implements ContainerAwareInterface
22
{
23
    use LockableTrait;
24
25
    private $container;
26
27
    protected static $defaultName = 'audit:schema:update';
28
29
    protected function configure()
30
    {
31
        $this
32
            ->setDescription('Update audit tables structure')
33
            ->setName(self::$defaultName)
34
        ;
35
    }
36
37
    protected function execute(InputInterface $input, OutputInterface $output)
38
    {
39
        if (!$this->lock()) {
40
            $output->writeln('The command is already running in another process.');
41
42
            return 0;
43
        }
44
45
        $io = new SymfonyStyle($input, $output);
46
47
        /**
48
         * @var AuditManager $manager
49
         */
50
        $manager = $this->container->get('dh_doctrine_audit.manager');
51
52
        /**
53
         * @var UpdateHelper $updater
54
         */
55
        $updater = new UpdateHelper($manager);
56
57
        /**
58
         * @var RegistryInterface $registry
59
         */
60
        $registry = $this->container->get('doctrine');
61
62
        /**
63
         * @var EntityManager $em
64
         */
65
        $em = $registry->getManager();
66
67
        /**
68
         * @var Connection $connection
69
         */
70
        $connection = $em->getConnection();
71
72
        /**
73
         * @var AbstractSchemaManager
74
         */
75
        $schemaManager = $connection->getSchemaManager();
76
        $tables = $schemaManager->listTables();
77
        $audits = [];
78
        $errors = [];
79
80
        $regex = sprintf(
81
            '#^%s(.*)%s$#',
82
            preg_quote($updater->getConfiguration()->getTablePrefix(), '#'),
83
            preg_quote($updater->getConfiguration()->getTableSuffix(), '#')
84
        );
85
86
        foreach ($tables as $table) {
87
            if (preg_match($regex, $table->getName())) {
88
                $audits[] = $table;
89
            }
90
        }
91
92
        $progressBar = new ProgressBar($output, \count($audits));
93
        $progressBar->setBarWidth(70);
94
        $progressBar->setFormat("%message%\n".$progressBar->getFormatDefinition('debug'));
95
96
        $progressBar->setMessage('Starting...');
97
        $progressBar->start();
98
99
        foreach ($audits as $table) {
100
            $progressBar->setMessage("Processing audit tables... (<info>{$table->getName()}</info>)");
101
            $progressBar->display();
102
103
            try {
104
                $updater->updateAuditTable($schemaManager, $table, $em);
105
            } catch (UpdateException $e) {
106
                $errors[] = $e->getMessage();
107
                $io->error($e->getMessage());
108
            }
109
110
            $progressBar->advance();
111
        }
112
113
        $progressBar->setMessage('Processing audit tables... (<info>done</info>)');
114
        $progressBar->display();
115
116
        $io->newLine(2);
117
118
        if (empty($errors)) {
119
            $io->success('Success.');
120
        } else {
121
            foreach ($errors as $error) {
122
                $io->error($error);
123
            }
124
        }
125
126
        // if not released explicitly, Symfony releases the lock
127
        // automatically when the execution of the command ends
128
        $this->release();
129
130
        return (int) empty($errors);
131
    }
132
133
    public function setContainer(ContainerInterface $container = null): void
134
    {
135
        $this->container = $container;
136
    }
137
138
    public function unlock()
139
    {
140
        return $this->release();
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->release() targeting DH\DoctrineAuditBundle\C...chemaCommand::release() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
141
    }
142
}
143