Completed
Pull Request — master (#255)
by Evgenij
17:18
created

createMigrationConfigurationService()   B

Complexity

Conditions 5
Paths 9

Size

Total Lines 42

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 42
rs 8.9368
c 0
b 0
f 0
cc 5
nc 9
nop 4
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Liip\MonitorBundle\DependencyInjection\DoctrineMigrations;
6
7
use Doctrine\Migrations\Configuration\Connection\ExistingConnection;
8
use Doctrine\Migrations\Configuration\Migration\JsonFile;
9
use Doctrine\Migrations\Configuration\Migration\PhpFile;
10
use Doctrine\Migrations\Configuration\Migration\XmlFile;
11
use Doctrine\Migrations\Configuration\Migration\YamlFile;
12
use Doctrine\Migrations\DependencyFactory;
13
use Symfony\Component\DependencyInjection\Alias;
14
use Symfony\Component\DependencyInjection\ContainerBuilder;
15
use Symfony\Component\DependencyInjection\Reference;
16
17
/**
18
 * Class V3MigrationsLoader
19
 */
20
final class V3MigrationsLoader extends AbstractDoctrineMigrationsLoader
21
{
22
    /**
23
     * @inheritDoc
24
     */
25
    public function createMigrationConfigurationService(
26
        ContainerBuilder $container,
27
        string $connectionName,
28
        string $serviceId,
29
        string $filename = null
30
    ): void {
31
        if ($filename !== null) {
32
            $configurationClass = $this->getConfigurationLoaderClass($container, $filename);
33
            $filenameHash = md5($filename);
34
            $configurationServiceId = 'liip_monitor.check.doctrine_migrations.configuration.' . $filenameHash;
35
            if (!$container->has($configurationServiceId)) {
36
                $container->register($configurationServiceId, $configurationClass)
37
                          ->setPublic(false)
38
                          ->setArguments([$filename]);
39
            }
40
41
            $connectionLoaderId = 'liip_monitor.check.doctrine_migrations.connection_loader.' . $connectionName;
42
            if (!$container->has($connectionLoaderId)) {
43
                $container->register($connectionLoaderId, ExistingConnection::class)
44
                          ->setPublic(false)
45
                          ->setArguments([new Reference(sprintf('doctrine.dbal.%s_connection', $connectionName))]);
46
            }
47
48
            $dependencyFactoryId = sprintf(
49
                'liip_monitor.check.doctrine_migrations.dependency_factory.%s.%s',
50
                $connectionName,
51
                $filenameHash
52
            );
53
            if (!$container->has($dependencyFactoryId)) {
54
                $container->register($dependencyFactoryId, DependencyFactory::class)
55
                          ->setFactory([DependencyFactory::class, 'fromConnection'])
56
                          ->setPublic(false)
57
                          ->setArguments([new Reference($configurationServiceId), new Reference($connectionLoaderId)]);
58
            }
59
60
            $container->setAlias($serviceId, new Alias($dependencyFactoryId, true));
61
62
            return;
63
        }
64
65
        $container->setAlias($serviceId, new Alias('doctrine.migrations.dependency_factory', true));
66
    }
67
68
    /**
69
     * Creates in-memory migration configuration for setting up container service.
70
     *
71
     * @param ContainerBuilder $container The container
72
     * @param string           $filename  Migrations configuration file
73
     *
74
     * @return string FQCN of configuration class loader
75
     */
76
    private function getConfigurationLoaderClass(ContainerBuilder $container, string $filename): string
77
    {
78
        // Available options are located under Doctrine\Migrations\Configuration\Migration namespace
79
        $map = [
80
            'xml' => XmlFile::class,
81
            'yaml' => YamlFile::class,
82
            'yml' => YamlFile::class,
83
            'php' => PhpFile::class,
84
            'json' => JsonFile::class,
85
        ];
86
87
        $filename = $container->getParameterBag()->resolveValue($filename);
88
        $info = pathinfo($filename);
89
90
        $extension = $info['extension'] ?? '';
91
        if (empty($map[$extension])) {
92
            throw new \InvalidArgumentException(sprintf('Config file type "%s" is not supported', $extension));
93
        }
94
95
        return $map[$info['extension']];
96
    }
97
}
98