Failed Conditions
Pull Request — master (#6556)
by Luís
08:42
created

ConsoleRunner   C

Complexity

Total Complexity 5

Size/Duplication

Total Lines 111
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 26

Test Coverage

Coverage 73.81%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
c 1
b 0
f 0
lcom 0
cbo 26
dl 0
loc 111
ccs 31
cts 42
cp 0.7381
rs 5

5 Methods

Rating   Name   Duplication   Size   Complexity  
A createHelperSet() 0 9 1
A run() 0 5 1
A createApplication() 0 10 1
B addCommands() 0 31 1
A printCliConfigTemplate() 0 21 1
1
<?php
2
/*
3
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14
 *
15
 * This software consists of voluntary contributions made by many individuals
16
 * and is licensed under the MIT license. For more information, see
17
 * <http://www.doctrine-project.org>.
18
 */
19
20
namespace Doctrine\ORM\Tools\Console;
21
22
use Symfony\Component\Console\Application;
23
use Symfony\Component\Console\Helper\HelperSet;
24
use Doctrine\ORM\Version;
25
use Doctrine\ORM\EntityManagerInterface;
26
27
use Doctrine\DBAL\Tools\Console\Helper\ConnectionHelper;
28
use Doctrine\ORM\Tools\Console\Helper\EntityManagerHelper;
29
30
/**
31
 * Handles running the Console Tools inside Symfony Console context.
32
 */
33
class ConsoleRunner
34
{
35
    /**
36
     * Create a Symfony Console HelperSet
37
     *
38
     * @param EntityManagerInterface $entityManager
39
     * @return HelperSet
40
     */
41
    public static function createHelperSet(EntityManagerInterface $entityManager)
42
    {
43
        return new HelperSet(
44
            [
45
                'db' => new ConnectionHelper($entityManager->getConnection()),
46
                'em' => new EntityManagerHelper($entityManager)
47
            ]
48
        );
49
    }
50
51
    /**
52
     * Runs console with the given helperset.
53
     *
54
     * @param \Symfony\Component\Console\Helper\HelperSet  $helperSet
55
     * @param \Symfony\Component\Console\Command\Command[] $commands
56
     *
57
     * @return void
58
     */
59
    static public function run(HelperSet $helperSet, $commands = [])
0 ignored issues
show
Coding Style introduced by
As per PSR2, the static declaration should come after the visibility declaration.
Loading history...
60
    {
61
        $cli = self::createApplication($helperSet, $commands);
62
        $cli->run();
63
    }
64
65
    /**
66
     * Creates a console application with the given helperset and
67
     * optional commands.
68
     *
69
     * @param \Symfony\Component\Console\Helper\HelperSet $helperSet
70
     * @param array                                       $commands
71
     *
72
     * @return \Symfony\Component\Console\Application
73
     */
74 1
    static public function createApplication(HelperSet $helperSet, $commands = [])
0 ignored issues
show
Coding Style introduced by
As per PSR2, the static declaration should come after the visibility declaration.
Loading history...
75
    {
76 1
        $cli = new Application('Doctrine Command Line Interface', Version::VERSION);
77 1
        $cli->setCatchExceptions(true);
78 1
        $cli->setHelperSet($helperSet);
79 1
        self::addCommands($cli);
80 1
        $cli->addCommands($commands);
81
82 1
        return $cli;
83
    }
84
85
    /**
86
     * @param Application $cli
87
     *
88
     * @return void
89
     */
90 1
    static public function addCommands(Application $cli)
0 ignored issues
show
Coding Style introduced by
As per PSR2, the static declaration should come after the visibility declaration.
Loading history...
91
    {
92 1
        $cli->addCommands(
93
            [
94
                // DBAL Commands
95 1
                new \Doctrine\DBAL\Tools\Console\Command\RunSqlCommand(),
96 1
                new \Doctrine\DBAL\Tools\Console\Command\ImportCommand(),
97
98
                // ORM Commands
99 1
                new \Doctrine\ORM\Tools\Console\Command\ClearCache\CollectionRegionCommand(),
100 1
                new \Doctrine\ORM\Tools\Console\Command\ClearCache\EntityRegionCommand(),
101 1
                new \Doctrine\ORM\Tools\Console\Command\ClearCache\MetadataCommand(),
102 1
                new \Doctrine\ORM\Tools\Console\Command\ClearCache\QueryCommand(),
103 1
                new \Doctrine\ORM\Tools\Console\Command\ClearCache\QueryRegionCommand(),
104 1
                new \Doctrine\ORM\Tools\Console\Command\ClearCache\ResultCommand(),
105 1
                new \Doctrine\ORM\Tools\Console\Command\SchemaTool\CreateCommand(),
106 1
                new \Doctrine\ORM\Tools\Console\Command\SchemaTool\UpdateCommand(),
107 1
                new \Doctrine\ORM\Tools\Console\Command\SchemaTool\DropCommand(),
108 1
                new \Doctrine\ORM\Tools\Console\Command\EnsureProductionSettingsCommand(),
109 1
                new \Doctrine\ORM\Tools\Console\Command\ConvertDoctrine1SchemaCommand(),
110 1
                new \Doctrine\ORM\Tools\Console\Command\GenerateRepositoriesCommand(),
111 1
                new \Doctrine\ORM\Tools\Console\Command\GenerateEntitiesCommand(),
112 1
                new \Doctrine\ORM\Tools\Console\Command\GenerateProxiesCommand(),
113 1
                new \Doctrine\ORM\Tools\Console\Command\ConvertMappingCommand(),
114 1
                new \Doctrine\ORM\Tools\Console\Command\RunDqlCommand(),
115 1
                new \Doctrine\ORM\Tools\Console\Command\ValidateSchemaCommand(),
116 1
                new \Doctrine\ORM\Tools\Console\Command\InfoCommand(),
117 1
                new \Doctrine\ORM\Tools\Console\Command\MappingDescribeCommand(),
118
            ]
119
        );
120 1
    }
121
122
    static public function printCliConfigTemplate()
0 ignored issues
show
Coding Style introduced by
As per PSR2, the static declaration should come after the visibility declaration.
Loading history...
123
    {
124
        echo <<<'HELP'
125
You are missing a "cli-config.php" or "config/cli-config.php" file in your
126
project, which is required to get the Doctrine Console working. You can use the
127
following sample as a template:
128
129
<?php
130
use Doctrine\ORM\Tools\Console\ConsoleRunner;
131
132
// replace with file to your own project bootstrap
133
require_once 'bootstrap.php';
134
135
// replace with mechanism to retrieve EntityManager in your app
136
$entityManager = GetEntityManager();
137
138
return ConsoleRunner::createHelperSet($entityManager);
139
140
HELP;
141
142
    }
143
}
144