SchemaCreateCommand::execute()   A
last analyzed

Complexity

Conditions 4
Paths 12

Size

Total Lines 42
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 24
CRAP Score 4.001

Importance

Changes 0
Metric Value
eloc 26
dl 0
loc 42
ccs 24
cts 25
cp 0.96
rs 9.504
c 0
b 0
f 0
cc 4
nc 12
nop 2
crap 4.001
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
9
/**
10
 * Class SchemaCreateCommand
11
 */
12
class SchemaCreateCommand extends ContainerAwareCommand
13
{
14
    /**
15
     * Configure command
16
     */
17
    protected function configure()
18
    {
19
        $this
20 1
            ->setName('norsys:logs:schema-create')
21 1
            ->setDescription('Create schema to log Monolog entries')
22 1
            ->addOption('force', 'f', null, 'Execute queries');
23 1
    }
24
25
    /**
26
     * @param InputInterface  $input
27
     * @param OutputInterface $output
28
     *
29
     * @return integer
30
     */
31
    protected function execute(InputInterface $input, OutputInterface $output)
32
    {
33 1
        $loggerClosure = function ($message) use ($output) {
34
            $output->writeln($message);
35 1
        };
36
37 1
        $tableName = $this->getContainer()->getParameter('norsys_logs.doctrine.table_name');
38
39 1
        $schemaBuilder = $this->getContainer()->get('norsys_logs.model.log_schema_builder');
40
41 1
        if ($input->getOption('force') === true) {
42
            try {
43 1
                $schemaBuilder->drop($loggerClosure);
44 1
                $output->writeln(sprintf(
45 1
                    '<info>Dropped table <comment>%s</comment> for Doctrine Monolog connection.</info>',
46 1
                    $tableName
47
                ));
48 1
            } catch (\Exception $e) {
49 1
                $output->writeln(sprintf(
50 1
                    '<info>Dropped table ignored for Doctrine Monolog connection (not necessary).</info>',
51 1
                    $tableName
52
                ));
53
            }
54
        }
55
56
        try {
57 1
            $schemaBuilder->create($loggerClosure);
58 1
            $output->writeln(sprintf(
59 1
                '<info>Created table <comment>%s</comment> for Doctrine Monolog connection.</info>',
60 1
                $tableName
61
            ));
62 1
        } catch (\Exception $e) {
63 1
            $output->writeln(sprintf(
64 1
                '<error>Could not create table <comment>%s</comment> for Doctrine Monolog connection.</error>',
65 1
                $tableName
66
            ));
67 1
            $output->writeln(sprintf('<error>%s</error>', $e->getMessage()));
68
69 1
            return 1;
70
        }
71
72 1
        return 0;
73
    }
74
}
75