CreatorDb::create()   B
last analyzed

Complexity

Conditions 5
Paths 8

Size

Total Lines 23
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 5

Importance

Changes 4
Bugs 0 Features 0
Metric Value
c 4
b 0
f 0
dl 0
loc 23
ccs 12
cts 12
cp 1
rs 8.5906
cc 5
eloc 12
nc 8
nop 1
crap 5
1
<?php
2
3
namespace Velikonja\LabbyBundle\Database;
4
5
use Symfony\Component\Console\Output\BufferedOutput;
6
use Symfony\Component\Console\Output\NullOutput;
7
use Symfony\Component\Console\Output\OutputInterface;
8
use Velikonja\LabbyBundle\Executor\SymfonyCommandExecutor;
9
10
class CreatorDb
11
{
12
    /**
13
     * @var SymfonyCommandExecutor
14
     */
15
    private $executor;
16
17
    /**
18
     * @param SymfonyCommandExecutor $executor
19
     */
20 3
    public function __construct(SymfonyCommandExecutor $executor)
21
    {
22 3
        $this->executor = $executor;
23 3
    }
24
25
    /**
26
     * @param OutputInterface $output
27
     */
28 3
    public function create(OutputInterface $output = null)
29
    {
30 3
        $error     = false;
31 3
        $outputTmp = new NullOutput();
32
33
        // SF 2.3 does not have BufferedOutput
34 3
        if (class_exists('Symfony\Component\Console\Output\BufferedOutput')) {
35 3
            $outputTmp = new BufferedOutput(OutputInterface::VERBOSITY_NORMAL, true);
36
        }
37
38
        try {
39 3
            $this->executor->execute('doctrine:database:drop --force', $outputTmp);
40 1
        } catch (\RuntimeException $e) {
41
            // catches an error if database does not exist
42 1
            $error = true;
43
        }
44
45 3
        if (! $error && method_exists($outputTmp, 'fetch')) {
46 2
            $output->write($outputTmp->fetch());
47
        }
48
49 3
        $this->executor->execute('doctrine:database:create', $output);
50 3
    }
51
}
52