Completed
Pull Request — master (#602)
by Tom
07:07
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
    /**
43 21
     * {@inheritDoc}
44 21
     */
45
    public function __construct($name)
46
    {
47
        $this->name = ucfirst(strtolower($name));
48
    }
49
50
    /**
51
     * {@inheritDoc}
52 21
     *
53
     * @return AbstractCommand
54 21
     *
55
     * @throws InvalidArgumentException
56 21
     */
57 1
    public function __invoke(ContainerInterface $container, $requestedName, ?array $options = null)
58
    {
59
        $className = 'Doctrine\Migrations\Tools\Console\Command\\' . $this->name . 'Command';
60
61
        if (! class_exists($className)) {
62 20
            throw new InvalidArgumentException();
63
        }
64 20
65
        $configuration = $container->get('doctrine.migrations_configuration.orm_default');
66 20
        // @TODO currently hardcoded: `orm_default` should be injected
67
        assert($configuration instanceof Configuration);
68 20
        $command = new $className();
69
        assert($command instanceof AbstractCommand);
70
71
        $command->setMigrationConfiguration($configuration);
72
73
        return $command;
74
    }
75
76 21
    /**
77
     * @throws InvalidArgumentException
78 21
     */
79
    public function createService(ServiceLocatorInterface $container) : AbstractCommand
80
    {
81
        return $this($container, 'Doctrine\Migrations\Tools\Console\Command\\' . $this->name . 'Command');
82
    }
83
}
84