Completed
Pull Request — master (#540)
by
unknown
09:20 queued 07:31
created

MigrationConfigurationListener   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Test Coverage

Coverage 15%

Importance

Changes 0
Metric Value
wmc 6
lcom 0
cbo 4
dl 0
loc 48
ccs 3
cts 20
cp 0.15
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B __invoke() 0 22 4
A createMigrationConfigurationAlias() 0 4 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 DoctrineORMModule\Listener;
21
22
use Doctrine\DBAL\Migrations\Tools\Console\Command\AbstractCommand as MigrationCommand;
23
use Symfony\Component\Console\Event\ConsoleCommandEvent;
24
use Zend\ServiceManager\ServiceLocatorInterface;
25
26
/**
27
 * @license MIT
28
 * @link    www.doctrine-project.org
29
 * @author  Thomas Dutrion <[email protected]>
30
 */
31
final class MigrationConfigurationListener
32
{
33
    private $container;
34
35 16
    public function __construct(ServiceLocatorInterface $container)
36
    {
37 16
        $this->container = $container;
38 16
    }
39
40
    /**
41
     * @param ConsoleCommandEvent $consoleCommandEvent
42
     * @throws \Psr\Container\ContainerExceptionInterface
43
     * @throws \Psr\Container\NotFoundExceptionInterface
44
     */
45
    public function __invoke(ConsoleCommandEvent $consoleCommandEvent)
46
    {
47
        if (!class_exists(MigrationCommand::class)) {
48
            return;
49
        }
50
        if (!$consoleCommandEvent->getCommand() instanceof MigrationCommand) {
51
            return;
52
        }
53
        /* @var $command MigrationCommand */
54
        $command = $consoleCommandEvent->getCommand();
55
        $input = $consoleCommandEvent->getInput();
56
        $objectManagerName = 'doctrine.entitymanager.orm_default';
57
58
        if ($input->hasParameterOption(['--object-manager'])) {
59
            $objectManagerName = $input->getParameterOption(['--object-manager']);
60
        }
61
62
        $migrationsConfigurationAlias = $this->createMigrationConfigurationAlias($objectManagerName);
63
        $migrationConfiguration = $this->container->get($migrationsConfigurationAlias);
64
65
        $command->setMigrationConfiguration($migrationConfiguration);
66
    }
67
68
    /**
69
     * Create the Migration Configuration alias from the Object Manager alias
70
     *
71
     * @param string $objectManagerAlias
72
     * @return string
73
     */
74
    private function createMigrationConfigurationAlias($objectManagerAlias)
75
    {
76
        return str_replace('entitymanager', 'migrations_configuration', $objectManagerAlias);
77
    }
78
}
79