Failed Conditions
Pull Request — master (#989)
by Asmir
02:19
created

ConsoleRunner::checkLegacyConfiguration()   A

Complexity

Conditions 6
Paths 4

Size

Total Lines 23
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 24.432

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
eloc 15
c 1
b 0
f 0
nc 4
nop 2
dl 0
loc 23
ccs 3
cts 15
cp 0.2
crap 24.432
rs 9.2222
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\Migrations\Tools\Console;
6
7
use Doctrine\DBAL\Tools\Console\Helper\ConnectionHelper;
8
use Doctrine\Migrations\Configuration\Connection\ExistingConnection;
9
use Doctrine\Migrations\Configuration\EntityManager\ExistingEntityManager;
10
use Doctrine\Migrations\Configuration\Migration\ConfigurationFileWithFallback;
11
use Doctrine\Migrations\DependencyFactory;
12
use Doctrine\Migrations\Tools\Console\Command\CurrentCommand;
13
use Doctrine\Migrations\Tools\Console\Command\DiffCommand;
14
use Doctrine\Migrations\Tools\Console\Command\DoctrineCommand;
15
use Doctrine\Migrations\Tools\Console\Command\DumpSchemaCommand;
16
use Doctrine\Migrations\Tools\Console\Command\ExecuteCommand;
17
use Doctrine\Migrations\Tools\Console\Command\GenerateCommand;
18
use Doctrine\Migrations\Tools\Console\Command\LatestCommand;
19
use Doctrine\Migrations\Tools\Console\Command\ListCommand;
20
use Doctrine\Migrations\Tools\Console\Command\MigrateCommand;
21
use Doctrine\Migrations\Tools\Console\Command\RollupCommand;
22
use Doctrine\Migrations\Tools\Console\Command\StatusCommand;
23
use Doctrine\Migrations\Tools\Console\Command\SyncMetadataCommand;
24
use Doctrine\Migrations\Tools\Console\Command\UpToDateCommand;
25
use Doctrine\Migrations\Tools\Console\Command\VersionCommand;
26
use Doctrine\ORM\Tools\Console\Helper\EntityManagerHelper;
27
use PackageVersions\Versions;
28
use RuntimeException;
29
use Symfony\Component\Console\Application;
30
use Symfony\Component\Console\Helper\HelperSet;
31
use function file_exists;
32
use function getcwd;
33
use function is_readable;
34
use function sprintf;
35
use const DIRECTORY_SEPARATOR;
36
37
/**
38
 * The ConsoleRunner class is used to create the Symfony Console application for the Doctrine Migrations console.
39
 *
40
 * @internal
41
 *
42
 * @see bin/doctrine-migrations.php
43
 */
44
class ConsoleRunner
45
{
46 4
    public static function findDependencyFactory() : ?DependencyFactory
47
    {
48
        // Support for using the Doctrine ORM convention of providing a `cli-config.php` file.
49
        $configurationDirectories = [
50 4
            getcwd(),
51 4
            getcwd() . DIRECTORY_SEPARATOR . 'config',
52
        ];
53
54 4
        $configurationFile = null;
55 4
        foreach ($configurationDirectories as $configurationDirectory) {
56 4
            $configurationFilePath = $configurationDirectory . DIRECTORY_SEPARATOR . 'cli-config.php';
57
58 4
            if (! file_exists($configurationFilePath)) {
59 2
                continue;
60
            }
61
62 3
            $configurationFile = $configurationFilePath;
63 3
            break;
64
        }
65
66 4
        $dependencyFactory = null;
67 4
        if ($configurationFile !== null) {
68 3
            if (! is_readable($configurationFile)) {
69
                throw new RuntimeException(sprintf(
70
                    'Configuration file "%s" cannot be read.',
71
                    $configurationFile
72
                ));
73
            }
74
75 3
            $dependencyFactory = require $configurationFile;
76 3
            $dependencyFactory = self::checkLegacyConfiguration($dependencyFactory, $configurationFile);
77
        }
78
79 4
        if ($dependencyFactory !== null && ! ($dependencyFactory instanceof DependencyFactory)) {
80 1
            throw new RuntimeException(sprintf(
81 1
                'Configuration file "%s" must return an instance of "%s"',
82 1
                $configurationFile,
83 1
                DependencyFactory::class
84
            ));
85
        }
86
87 3
        return $dependencyFactory;
88
    }
89
90
    /** @param DoctrineCommand[] $commands */
91 1
    public static function run(array $commands = [], ?DependencyFactory $dependencyFactory = null) : void
92
    {
93 1
        $cli = static::createApplication($commands, $dependencyFactory);
94 1
        $cli->run();
95 1
    }
96
97
    /** @param DoctrineCommand[] $commands */
98 2
    public static function createApplication(array $commands = [], ?DependencyFactory $dependencyFactory = null) : Application
99
    {
100 2
        $cli = new Application('Doctrine Migrations', Versions::getVersion('doctrine/migrations'));
101 2
        $cli->setCatchExceptions(true);
102 2
        self::addCommands($cli, $dependencyFactory);
103 2
        $cli->addCommands($commands);
104
105 2
        return $cli;
106
    }
107
108 11
    public static function addCommands(Application $cli, ?DependencyFactory $dependencyFactory = null) : void
109
    {
110 11
        $cli->addCommands([
111 11
            new CurrentCommand($dependencyFactory),
112 11
            new DumpSchemaCommand($dependencyFactory),
113 11
            new ExecuteCommand($dependencyFactory),
114 11
            new GenerateCommand($dependencyFactory),
115 11
            new LatestCommand($dependencyFactory),
116 11
            new MigrateCommand($dependencyFactory),
117 11
            new RollupCommand($dependencyFactory),
118 11
            new StatusCommand($dependencyFactory),
119 11
            new VersionCommand($dependencyFactory),
120 11
            new UpToDateCommand($dependencyFactory),
121 11
            new SyncMetadataCommand($dependencyFactory),
122 11
            new ListCommand($dependencyFactory),
123
        ]);
124
125 11
        if ($dependencyFactory === null || ! $dependencyFactory->hasEntityManager()) {
126 9
            return;
127
        }
128
129 2
        $cli->add(new DiffCommand($dependencyFactory));
130 2
    }
131
132
    /**
133
     * @param mixed $dependencyFactory
134
     *
135
     * @return mixed
136
     */
137 3
    private static function checkLegacyConfiguration($dependencyFactory, string $configurationFile)
138
    {
139 3
        if ($dependencyFactory instanceof HelperSet) {
140
            $configurations = new ConfigurationFileWithFallback();
141
            if ($dependencyFactory->has('em') && $dependencyFactory->get('em') instanceof EntityManagerHelper) {
142
                $dependencyFactory = DependencyFactory::fromEntityManager(
143
                    $configurations,
144
                    new ExistingEntityManager($dependencyFactory->get('em')->getEntityManager())
0 ignored issues
show
Bug introduced by
The method getEntityManager() does not exist on Symfony\Component\Console\Helper\Helper. It seems like you code against a sub-type of Symfony\Component\Console\Helper\Helper such as Doctrine\ORM\Tools\Conso...per\EntityManagerHelper. ( Ignorable by Annotation )

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

144
                    new ExistingEntityManager($dependencyFactory->get('em')->/** @scrutinizer ignore-call */ getEntityManager())
Loading history...
145
                );
146
            } elseif ($dependencyFactory->has('db') && $dependencyFactory->get('db') instanceof ConnectionHelper) {
147
                $dependencyFactory = DependencyFactory::fromConnection(
148
                    $configurations,
149
                    new ExistingConnection($dependencyFactory->get('db')->getConnection())
0 ignored issues
show
Bug introduced by
The method getConnection() does not exist on Symfony\Component\Console\Helper\Helper. It seems like you code against a sub-type of Symfony\Component\Console\Helper\Helper such as Doctrine\DBAL\Tools\Cons...Helper\ConnectionHelper. ( Ignorable by Annotation )

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

149
                    new ExistingConnection($dependencyFactory->get('db')->/** @scrutinizer ignore-call */ getConnection())
Loading history...
150
                );
151
            } else {
152
                throw new RuntimeException(sprintf(
153
                    'Configuration HelperSet returned by "%s" does not have neither the "em" or the "db" helper.',
154
                    $configurationFile
155
                ));
156
            }
157
        }
158
159 3
        return $dependencyFactory;
160
    }
161
}
162