Completed
Pull Request — master (#602)
by Tom
09:02
created

MigrationsCommandFactory   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 1
dl 0
loc 50
ccs 13
cts 13
cp 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
7
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
8
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
9
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
10
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
11
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
12
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
13
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
14
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
15
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
16
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
17
 *
18
 * This software consists of voluntary contributions made by many individuals
19
 * and is licensed under the MIT license.
20
 */
21
22
namespace DoctrineORMModule\Service;
23
24
use Doctrine\Migrations\Configuration\Configuration;
25
use Doctrine\Migrations\Tools\Console\Command\AbstractCommand;
26
use Interop\Container\ContainerInterface;
27
use InvalidArgumentException;
28
use Laminas\ServiceManager\FactoryInterface;
29
use Laminas\ServiceManager\ServiceLocatorInterface;
30
use function assert;
31
use function class_exists;
32
use function strtolower;
33
use function ucfirst;
34
35
/**
36
 * Service factory for migrations command
37
 */
38
class MigrationsCommandFactory implements FactoryInterface
39
{
40
    private string $name;
0 ignored issues
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected T_STRING, expecting T_FUNCTION or T_CONST
Loading history...
41 21
42
    public function __construct($name)
43 21
    {
44 21
        $this->name = ucfirst(strtolower($name));
45
    }
46
47
    /**
48
     * {@inheritDoc}
49
     *
50
     * @return AbstractCommand
51
     *
52 21
     * @throws InvalidArgumentException
53
     */
54 21
    public function __invoke(ContainerInterface $container, $requestedName, ?array $options = null)
55
    {
56 21
        $className = 'Doctrine\Migrations\Tools\Console\Command\\' . $this->name . 'Command';
57 1
58
        if (! class_exists($className)) {
59
            throw new InvalidArgumentException();
60
        }
61
62 20
        $configuration = $container->get('doctrine.migrations_configuration.orm_default');
63
        // @TODO currently hardcoded: `orm_default` should be injected
64 20
        assert($configuration instanceof Configuration);
65
        $command = new $className();
66 20
        assert($command instanceof AbstractCommand);
67
68 20
        $command->setMigrationConfiguration($configuration);
69
70
        return $command;
71
    }
72
73
    /**
74
     * @throws InvalidArgumentException
75
     */
76 21
    public function createService(ServiceLocatorInterface $container) : AbstractCommand
77
    {
78 21
        return $this($container, 'Doctrine\Migrations\Tools\Console\Command\\' . $this->name . 'Command');
79
    }
80
}
81