Failed Conditions
Pull Request — 2.10.x (#4011)
by
unknown
03:07
created

ConsoleRunner   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Test Coverage

Coverage 12%

Importance

Changes 0
Metric Value
wmc 6
eloc 39
dl 0
loc 67
ccs 3
cts 25
cp 0.12
rs 10
c 0
b 0
f 0

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 21 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 function sprintf;
16
use function trigger_error;
17
use const E_USER_DEPRECATED;
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
        $connectionProvider = null;
53
        if ($helperSetOrConnectionProvider instanceof HelperSet) {
54
            @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);
55
            $connectionProvider = null;
56
            $cli->setHelperSet($helperSetOrConnectionProvider);
57
        } elseif ($helperSetOrConnectionProvider instanceof ConnectionProvider) {
0 ignored issues
show
introduced by
$helperSetOrConnectionProvider is always a sub-type of Doctrine\DBAL\Tools\Console\ConnectionProvider.
Loading history...
58
            $connectionProvider = $helperSetOrConnectionProvider;
59
        } else {
60
            throw new TypeError(sprintf('First argument must be an instance of "%s" or "%s"', HelperSet::class, ConnectionProvider::class));
61
        }
62
63
        self::addCommands($cli, $connectionProvider);
64
65
        $cli->addCommands($commands);
66
        $cli->run();
67
    }
68
69
    /**
70
     * @return void
71
     */
72
    public static function addCommands(Application $cli, ?ConnectionProvider $connectionProvider = null)
73
    {
74
        $cli->addCommands([
75
            new RunSqlCommand($connectionProvider),
76
            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

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