SchemaUpdateCommand   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 72
Duplicated Lines 0 %

Test Coverage

Coverage 97.22%

Importance

Changes 0
Metric Value
wmc 7
eloc 38
dl 0
loc 72
ccs 35
cts 36
cp 0.9722
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
B execute() 0 53 6
A configure() 0 6 1
1
<?php
2
3
namespace Norsys\LogsBundle\Command;
4
5
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
6
use Symfony\Component\Console\Input\InputInterface;
7
use Symfony\Component\Console\Output\OutputInterface;
8
use Norsys\LogsBundle\Model\SchemaBuilder;
9
use Symfony\Component\Console\Question\ConfirmationQuestion;
10
11
/**
12
 * Class SchemaUpdateCommand
13
 */
14
class SchemaUpdateCommand extends ContainerAwareCommand
15
{
16
    /**
17
     * Configure command
18
     */
19
    protected function configure()
20
    {
21
        $this
22 1
            ->setName('norsys:logs:schema-update')
23 1
            ->setDescription('Update Monolog table from schema')
24 1
            ->addOption('force', 'f', null, 'Execute queries');
25 1
    }
26
27
    /**
28
     * @param InputInterface  $input
29
     * @param OutputInterface $output
30
     *
31
     * @return integer
32
     */
33
    protected function execute(InputInterface $input, OutputInterface $output)
34
    {
35 1
        $connection = $this->getContainer()->get('norsys_logs.doctrine_dbal.connection');
36 1
        $tableName = $this->getContainer()->getParameter('norsys_logs.doctrine.table_name');
37
38 1
        $schemaBuilder = $this->getContainer()->get('norsys_logs.model.log_schema_builder');
39 1
        $schemaDiffFactory = $this->getContainer()->get('norsys_logs.dbal.schema_diff_factory');
40
41 1
        $sqls = $schemaDiffFactory->getSchemaDiff()->toSql($connection->getDatabasePlatform());
42
43 1
        if (0 === count($sqls)) {
44 1
            $output->writeln('Nothing to update - your database is already in sync with the current Monolog schema.');
45
46 1
            return 0;
47
        }
48
49 1
        $output->writeln(
50 1
            '<comment>Warning</comment>: This operation may not be executed in a production environment'
51
        );
52 1
        $output->writeln(sprintf(
53 1
            '<info>SQL operations to execute to Monolog table "<comment>%s</comment>":</info>',
54 1
            $tableName
55
        ));
56
57 1
        $output->writeln(implode(';' . PHP_EOL, $sqls));
58
59 1
        if ($input->getOption('force') === false) {
60 1
            $helperQuestion = $this->getHelper('question');
61 1
            $question = new ConfirmationQuestion('Do you want to execute these SQL operations? (y/N) : ', false);
62
63 1
            if ($helperQuestion->ask($input, $output, $question) === false) {
64
                return;
65
            }
66
        }
67
68 1
        $error = false;
69
        try {
70 1
            $schemaBuilder->update();
71 1
            $output->writeln(sprintf(
72 1
                '<info>Successfully updated Monolog table "<comment>%s</comment>"! "%s" queries were executed</info>',
73 1
                $tableName,
74 1
                count($sqls)
75
            ));
76 1
        } catch (\Exception $e) {
77 1
            $output->writeln(sprintf(
78 1
                '<error>Could not update Monolog table "<comment>%s</comment>"...</error>',
79 1
                $tableName
80
            ));
81 1
            $output->writeln(sprintf('<error>%s</error>', $e->getMessage()));
82 1
            $error = true;
83
        }
84
85 1
        return ($error === true) ? 1 : 0;
86
    }
87
}
88