Completed
Push — master ( c8ccae...53133f )
by Asmir
16s queued 14s
created

ConsoleRunner   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 85
Duplicated Lines 0 %

Test Coverage

Coverage 94%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 46
dl 0
loc 85
ccs 47
cts 50
cp 0.94
rs 10
c 1
b 0
f 0
wmc 12

4 Methods

Rating   Name   Duplication   Size   Complexity  
A addCommands() 0 22 3
A createApplication() 0 8 1
A run() 0 4 1
B findDependencyFactory() 0 41 7
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\Migrations\Tools\Console;
6
7
use Doctrine\Migrations\DependencyFactory;
8
use Doctrine\Migrations\Tools\Console\Command\CurrentCommand;
9
use Doctrine\Migrations\Tools\Console\Command\DiffCommand;
10
use Doctrine\Migrations\Tools\Console\Command\DoctrineCommand;
11
use Doctrine\Migrations\Tools\Console\Command\DumpSchemaCommand;
12
use Doctrine\Migrations\Tools\Console\Command\ExecuteCommand;
13
use Doctrine\Migrations\Tools\Console\Command\GenerateCommand;
14
use Doctrine\Migrations\Tools\Console\Command\LatestCommand;
15
use Doctrine\Migrations\Tools\Console\Command\ListCommand;
16
use Doctrine\Migrations\Tools\Console\Command\MigrateCommand;
17
use Doctrine\Migrations\Tools\Console\Command\RollupCommand;
18
use Doctrine\Migrations\Tools\Console\Command\StatusCommand;
19
use Doctrine\Migrations\Tools\Console\Command\SyncMetadataCommand;
20
use Doctrine\Migrations\Tools\Console\Command\UpToDateCommand;
21
use Doctrine\Migrations\Tools\Console\Command\VersionCommand;
22
use PackageVersions\Versions;
23
use RuntimeException;
24
use Symfony\Component\Console\Application;
25
use function file_exists;
26
use function getcwd;
27
use function is_readable;
28
use function sprintf;
29
use const DIRECTORY_SEPARATOR;
30
31
/**
32
 * The ConsoleRunner class is used to create the Symfony Console application for the Doctrine Migrations console.
33
 *
34
 * @internal
35
 *
36
 * @see bin/doctrine-migrations.php
37
 */
38
class ConsoleRunner
39
{
40 4
    public static function findDependencyFactory() : ?DependencyFactory
41
    {
42
        // Support for using the Doctrine ORM convention of providing a `cli-config.php` file.
43
        $configurationDirectories = [
44 4
            getcwd(),
45 4
            getcwd() . DIRECTORY_SEPARATOR . 'config',
46
        ];
47
48 4
        $configurationFile = null;
49 4
        foreach ($configurationDirectories as $configurationDirectory) {
50 4
            $configurationFilePath = $configurationDirectory . DIRECTORY_SEPARATOR . 'cli-config.php';
51
52 4
            if (! file_exists($configurationFilePath)) {
53 2
                continue;
54
            }
55
56 3
            $configurationFile = $configurationFilePath;
57 3
            break;
58
        }
59
60 4
        $dependencyFactory = null;
61 4
        if ($configurationFile !== null) {
62 3
            if (! is_readable($configurationFile)) {
63
                throw new RuntimeException(sprintf(
64
                    'Configuration file "%s" cannot be read.',
65
                    $configurationFile
66
                ));
67
            }
68
69 3
            $dependencyFactory = require $configurationFile;
70
        }
71
72 4
        if ($dependencyFactory !== null && ! ($dependencyFactory instanceof DependencyFactory)) {
73 1
            throw new RuntimeException(sprintf(
74 1
                'Configuration file "%s" must return an instance of "%s"',
75 1
                $configurationFile,
76 1
                DependencyFactory::class
77
            ));
78
        }
79
80 3
        return $dependencyFactory;
81
    }
82
83
    /** @param DoctrineCommand[] $commands */
84 1
    public static function run(array $commands = [], ?DependencyFactory $dependencyFactory = null) : void
85
    {
86 1
        $cli = static::createApplication($commands, $dependencyFactory);
87 1
        $cli->run();
88 1
    }
89
90
    /** @param DoctrineCommand[] $commands */
91 2
    public static function createApplication(array $commands = [], ?DependencyFactory $dependencyFactory = null) : Application
92
    {
93 2
        $cli = new Application('Doctrine Migrations', Versions::getVersion('doctrine/migrations'));
94 2
        $cli->setCatchExceptions(true);
95 2
        self::addCommands($cli, $dependencyFactory);
96 2
        $cli->addCommands($commands);
97
98 2
        return $cli;
99
    }
100
101 11
    public static function addCommands(Application $cli, ?DependencyFactory $dependencyFactory = null) : void
102
    {
103 11
        $cli->addCommands([
104 11
            new CurrentCommand($dependencyFactory),
105 11
            new DumpSchemaCommand($dependencyFactory),
106 11
            new ExecuteCommand($dependencyFactory),
107 11
            new GenerateCommand($dependencyFactory),
108 11
            new LatestCommand($dependencyFactory),
109 11
            new MigrateCommand($dependencyFactory),
110 11
            new RollupCommand($dependencyFactory),
111 11
            new StatusCommand($dependencyFactory),
112 11
            new VersionCommand($dependencyFactory),
113 11
            new UpToDateCommand($dependencyFactory),
114 11
            new SyncMetadataCommand($dependencyFactory),
115 11
            new ListCommand($dependencyFactory),
116
        ]);
117
118 11
        if ($dependencyFactory === null || ! $dependencyFactory->hasEntityManager()) {
119 9
            return;
120
        }
121
122 2
        $cli->add(new DiffCommand($dependencyFactory));
123 2
    }
124
}
125