Failed Conditions
Pull Request — 2.11.x (#3956)
by David
12:14
created

ConsoleRunner   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Test Coverage

Coverage 12%

Importance

Changes 2
Bugs 1 Features 0
Metric Value
wmc 6
eloc 39
c 2
b 1
f 0
dl 0
loc 68
ccs 3
cts 25
cp 0.12
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A createHelperSet() 0 4 1
A printCliConfigTemplate() 0 3 1
A addCommands() 0 6 1
A run() 0 22 3
1
<?php
2
3
namespace Doctrine\DBAL\Tools\Console;
4
5
use Doctrine\DBAL\Connection;
6
use Doctrine\DBAL\Tools\Console\Command\ImportCommand;
7
use Doctrine\DBAL\Tools\Console\Command\ReservedWordsCommand;
8
use Doctrine\DBAL\Tools\Console\Command\RunSqlCommand;
9
use Doctrine\DBAL\Tools\Console\Helper\ConnectionHelper;
10
use Doctrine\DBAL\Version;
11
use Symfony\Component\Console\Application;
12
use Symfony\Component\Console\Command\Command;
13
use Symfony\Component\Console\Helper\HelperSet;
14
use TypeError;
15
use const E_USER_DEPRECATED;
16
use function sprintf;
17
use function trigger_error;
18
19
/**
20
 * Handles running the Console Tools inside Symfony Console context.
21
 */
22
class ConsoleRunner
23
{
24
    /**
25
     * Create a Symfony Console HelperSet
26
     *
27
     * @deprecated use a ConnectionProvider instead.
28
     *
29
     * @return HelperSet
30
     */
31 120
    public static function createHelperSet(Connection $connection)
32
    {
33 120
        return new HelperSet([
34 120
            'db' => new ConnectionHelper($connection),
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

34
            'db' => /** @scrutinizer ignore-deprecated */ new ConnectionHelper($connection),
Loading history...
35
        ]);
36
    }
37
38
    /**
39
     * Runs console with the given connection provider or helperset (deprecated).
40
     *
41
     * @param ConnectionProvider|HelperSet $helperSetOrConnectionProvider
42
     * @param Command[]                    $commands
43
     *
44
     * @return void
45
     */
46
    public static function run($helperSetOrConnectionProvider, $commands = [])
47
    {
48
        $cli = new Application('Doctrine Command Line Interface', Version::VERSION);
49
50
        $cli->setCatchExceptions(true);
51
52
        /** @var ConnectionProvider|null $connectionProvider */
53
        $connectionProvider = null;
54
        if ($helperSetOrConnectionProvider instanceof HelperSet) {
55
            @trigger_error(sprintf('Passing an instance of "%s" as the first argument is deprecated. Pass an instance of "%s" instead.', HelperSet::class, ConnectionProvider::class), E_USER_DEPRECATED);
56
            $connectionProvider = null;
57
            $cli->setHelperSet($helperSetOrConnectionProvider);
58
        } elseif ($helperSetOrConnectionProvider instanceof ConnectionProvider) {
0 ignored issues
show
introduced by
$helperSetOrConnectionProvider is always a sub-type of Doctrine\DBAL\Tools\Console\ConnectionProvider.
Loading history...
59
            $connectionProvider = $helperSetOrConnectionProvider;
60
        } else {
61
            throw new TypeError(sprintf('First argument must be an instance of "%s" or "%s"', HelperSet::class, ConnectionProvider::class));
62
        }
63
64
        self::addCommands($cli, $connectionProvider);
65
66
        $cli->addCommands($commands);
67
        $cli->run();
68
    }
69
70
    /**
71
     * @return void
72
     */
73
    public static function addCommands(Application $cli, ?ConnectionProvider $connectionProvider = null)
74
    {
75
        $cli->addCommands([
76
            new RunSqlCommand($connectionProvider),
77
            new ImportCommand(),
0 ignored issues
show
Deprecated Code introduced by
The class Doctrine\DBAL\Tools\Console\Command\ImportCommand has been deprecated: Use a database client application instead ( Ignorable by Annotation )

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

77
            /** @scrutinizer ignore-deprecated */ new ImportCommand(),
Loading history...
78
            new ReservedWordsCommand($connectionProvider),
79
        ]);
80
    }
81
82
    /**
83
     * Prints the instructions to create a configuration file
84
     *
85
     * @return void
86
     */
87
    public static function printCliConfigTemplate()
88
    {
89
        echo <<<'HELP'
90
You are missing a "cli-config.php" or "config/cli-config.php" file in your
91
project, which is required to get the Doctrine-DBAL Console working. You can use the
92
following sample as a template:
93
94
<?php
95
use Doctrine\DBAL\Tools\Console\ConnectionProvider\SingleConnectionProvider;
96
97
// You can append new commands to $commands array, if needed
98
99
// replace with the mechanism to retrieve DBAL connection(s) in your app
100
// and return a Doctrine\DBAL\Tools\Console\ConnectionProvider instance.
101
$connection = getDBALConnection();
102
103
// in case you have a single connection you can use SingleConnectionProvider
104
// otherwise you need to implement the Doctrine\DBAL\Tools\Console\ConnectionProvider interface with your custom logic
105
return new SingleConnectionProvider($connection);
106
107
HELP;
108
    }
109
}
110