Test Failed
Push — master ( 9a0773...1fd30e )
by Sébastien
07:48
created

RunSqlCommand::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 6
c 1
b 0
f 0
dl 0
loc 8
rs 10
cc 2
nc 2
nop 1
1
<?php
2
3
namespace Bdf\Prime\Console;
4
5
use Bdf\Prime\ConnectionManager;
6
use Bdf\Prime\Console\ConnectionProvider\DoctrineConnectionProviderAdapter;
7
use Bdf\Util\Console\BdfStyle;
8
use Doctrine\DBAL\Connection as DoctrineConnection;
9
use Doctrine\DBAL\Tools\Console\Command\RunSqlCommand as DoctrineRunSqlCommand;
10
use Doctrine\DBAL\Tools\Console\ConnectionProvider;
11
use Doctrine\DBAL\Tools\Console\Helper\ConnectionHelper;
12
use Symfony\Component\Console\Input\InputInterface;
13
use Symfony\Component\Console\Output\OutputInterface;
14
15
/**
16
 * Run SQL on a doctrine connection. This will display a dbal result.
17
 */
18
class RunSqlCommand extends DoctrineRunSqlCommand
19
{
20
    protected static $defaultName = 'prime:run:sql';
0 ignored issues
show
Coding Style Documentation introduced by
Missing member variable doc comment
Loading history...
21
22
    /**
23
     * @var ConnectionManager
24
     */
25
    private $connectionManager;
26
27
    /**
28
     * @param ConnectionManager $connectionManager
29
     */
30
    public function __construct(ConnectionManager $connectionManager)
31
    {
32
        if (interface_exists(ConnectionProvider::class)) {
33
            parent::__construct(new DoctrineConnectionProviderAdapter($connectionManager));
34
            $this->connectionManager = null;
35
        } else {
36
            parent::__construct();
37
            $this->connectionManager = $connectionManager;
38
        }
39
    }
40
41
    /**
0 ignored issues
show
Coding Style introduced by
Doc comment is empty
Loading history...
42
     *
43
     */
44
    protected function configure()
45
    {
46
        parent::configure();
47
48
        // Override the doctrine name to set the prime namespace.
49
        $this->setName(self::$defaultName);
50
    }
51
52
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $input should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $output should have a doc-comment as per coding-style.
Loading history...
53
     * {@inheritDoc}
54
     */
55
    protected function execute(InputInterface $input, OutputInterface $output)
56
    {
57
        if ($this->connectionManager !== null) {
58
            $connection = $this->connectionManager->getConnection($input->getOption('connection'));
59
60
            if (!$connection instanceof DoctrineConnection) {
61
                $io = new BdfStyle($input, $output);
62
                $io->line('Connection <comment>%s</comment> is ignored: only doctrine connection can be managed', $connection->getName());
63
                return 1;
64
            }
65
66
            $helperSet = $this->getHelperSet();
67
            $helperSet->set(new ConnectionHelper($connection), 'db');
0 ignored issues
show
Deprecated Code introduced by
The class Doctrine\DBAL\Tools\Cons...Helper\ConnectionHelper has been deprecated: use a ConnectionProvider instead. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

67
            $helperSet->set(/** @scrutinizer ignore-deprecated */ new ConnectionHelper($connection), 'db');
Loading history...
68
        }
69
70
        return parent::execute($input, $output);
71
    }
72
}
73