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

UpdateSchemaCommand::setContainer()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace DH\DoctrineAuditBundle\Command;
4
5
use DH\DoctrineAuditBundle\AuditManager;
6
use DH\DoctrineAuditBundle\Helper\UpdateHelper;
7
use Doctrine\DBAL\Connection;
8
use Doctrine\DBAL\Schema\AbstractSchemaManager;
9
use Symfony\Bridge\Doctrine\RegistryInterface;
10
use Symfony\Component\Console\Command\Command;
11
use Symfony\Component\Console\Command\LockableTrait;
12
use Symfony\Component\Console\Helper\ProgressBar;
13
use Symfony\Component\Console\Input\InputInterface;
14
use Symfony\Component\Console\Output\OutputInterface;
15
use Symfony\Component\Console\Style\SymfonyStyle;
16
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
17
use Symfony\Component\DependencyInjection\ContainerInterface;
18
19
class UpdateSchemaCommand extends Command implements ContainerAwareInterface
20
{
21
    use LockableTrait;
22
23
    private $container;
24
25
    protected static $defaultName = 'audit:schema:update';
26
27
    protected function configure()
28
    {
29
        $this
30
            ->setDescription('Update audit tables structure')
31
            ->setName(self::$defaultName)
32
        ;
33
    }
34
35
    protected function execute(InputInterface $input, OutputInterface $output)
36
    {
37
        if (!$this->lock()) {
38
            $output->writeln('The command is already running in another process.');
39
40
            return 0;
41
        }
42
43
        $io = new SymfonyStyle($input, $output);
44
45
        /**
46
         * @var AuditManager
47
         */
48
        $manager = $this->container->get('dh_doctrine_audit.manager');
49
50
        /**
51
         * @var UpdateHelper
52
         */
53
        $updater = new UpdateHelper($manager);
54
55
        /**
56
         * @var RegistryInterface
57
         */
58
        $registry = $this->container->get('doctrine');
59
60
        /**
61
         * @var Connection
62
         */
63
        $connection = $registry->getManager()->getConnection();
64
65
        /**
66
         * @var AbstractSchemaManager
67
         */
68
        $schemaManager = $connection->getSchemaManager();
69
        $tables = $schemaManager->listTables();
70
        $audits = [];
71
72
        $regex = sprintf(
73
            '#^%s(.*)%s$#',
74
            preg_quote($updater->getConfiguration()->getTablePrefix(), '#'),
75
            preg_quote($updater->getConfiguration()->getTableSuffix(), '#')
76
        );
77
78
        foreach ($tables as $table) {
79
            if (preg_match($regex, $table->getName())) {
80
                $audits[] = $table;
81
            }
82
        }
83
84
        $progressBar = new ProgressBar($output, \count($audits));
85
        $progressBar->setBarWidth(70);
86
        $progressBar->setFormat("%message%\n".$progressBar->getFormatDefinition('debug'));
87
88
        $progressBar->setMessage('Starting...');
89
        $progressBar->start();
90
91
        foreach ($audits as $table) {
92
            $progressBar->setMessage("Processing audit tables... (<info>{$table->getName()}</info>)");
93
            $progressBar->display();
94
95
            $operations = $updater->checkAuditTable($schemaManager, $table);
96
            if (isset($operations['add']) || isset($operations['update']) || isset($operations['remove'])) {
97
                $updater->updateAuditTable($schemaManager, $table, $operations, $registry->getManager());
98
            }
99
100
            $progressBar->advance();
101
        }
102
103
        $progressBar->setMessage('Processing audit tables... (<info>done</info>)');
104
        $progressBar->display();
105
106
        $io->newLine(2);
107
108
        $io->success('Success.');
109
110
        // if not released explicitly, Symfony releases the lock
111
        // automatically when the execution of the command ends
112
        $this->release();
113
114
        return 0;
115
    }
116
117
    public function setContainer(ContainerInterface $container = null): void
118
    {
119
        $this->container = $container;
120
    }
121
122
    public function unlock()
123
    {
124
        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...
125
    }
126
}
127