Completed
Push — master ( 4361e6...a02e55 )
by François
01:21
created

InitSessionPdoCommand::configure()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
cc 1
eloc 6
nc 1
nop 0
1
<?php
2
3
/*
4
 * This file is part of the Fxp package.
5
 *
6
 * (c) François Pluchino <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Fxp\Bundle\SessionBundle\Command;
13
14
use Fxp\Bundle\SessionBundle\Exception\InvalidConfigurationException;
15
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
16
use Symfony\Component\Console\Input\InputInterface;
17
use Symfony\Component\Console\Output\OutputInterface;
18
19
/**
20
 * This command initializes the session table in database.
21
 *
22
 * @author François Pluchino <[email protected]>
23
 */
24
class InitSessionPdoCommand extends ContainerAwareCommand
25
{
26
    /**
27
     * {@inheritdoc}
28
     */
29
    protected function configure()
30
    {
31
        $this->setName('init:session:pdo')
32
        ->setDescription('Initializes the PDO session storage')
33
        ->setHelp(<<<'EOT'
34
The <info>init:session:pdo</info> command initializes the PDO Session.
35
EOT
36
        );
37
    }
38
39
    /**
40
     * {@inheritdoc}
41
     */
42
    protected function execute(InputInterface $input, OutputInterface $output)
43
    {
44
        if (!$this->getContainer()->has('fxp_session.handler.pdo')) {
45
            throw new InvalidConfigurationException("The PDO Handler must be enabled in the config 'fxp_session.pdo.enabled'");
46
        }
47
48
        try {
49
            $handler = $this->getContainer()->get('fxp_session.handler.pdo');
50
            $handler->createTable();
51
            $output->writeln(array('', '  The table for PDO session is created.'));
52
        } catch (\PDOException $ex) {
53
            // Mysql and PostgreSQL already table exist code
54
            if (!in_array($ex->getCode(), array('42S01', '42P07'))) {
55
                throw $ex;
56
            }
57
58
            $output->writeln(array('', '  The table for PDO session is already exists.'));
59
        }
60
    }
61
}
62