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

DoctrineMigrationsLoader::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Liip\MonitorBundle\DependencyInjection\DoctrineMigrations;
6
7
use Doctrine\Bundle\MigrationsBundle\Command\DoctrineCommand;
8
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
9
use Symfony\Component\DependencyInjection\ContainerBuilder;
10
11
/**
12
 * Class DoctrineMigrationsLoader
13
 */
14
final class DoctrineMigrationsLoader implements CompilerPassInterface
15
{
16
    /**
17
     * @var AbstractDoctrineMigrationsLoader
18
     */
19
    private $loader;
20
21
    /**
22
     * DoctrineMigrationsLoader constructor
23
     */
24
    public function __construct()
25
    {
26
        $this->loader = class_exists(DoctrineCommand::class)
27
            ? new V2MigrationsLoader()
28
            : new V3MigrationsLoader();
29
    }
30
31
    /**
32
     * @inheritDoc
33
     */
34
    public function process(ContainerBuilder $container)
35
    {
36
        if (!($this->loader instanceof CompilerPassInterface)) {
37
            return;
38
        }
39
40
        $this->loader->process($container);
41
    }
42
43
    public function loadMigrationChecks(ContainerBuilder $container, array $migrationChecksConfig, string $groupName): array
44
    {
45
        $services = [];
46
        foreach ($migrationChecksConfig as $key => $config) {
47
            try {
48
                $serviceId = sprintf('liip_monitor.check.doctrine_migrations.configuration.%s.%s', $groupName, $key);
49
                $this->loader->createMigrationConfigurationService(
50
                    $container,
51
                    $config['connection'],
52
                    $serviceId,
53
                    $config['configuration_file'] ?? null
54
                );
55
56
                $services[$key] = $serviceId;
57
            } catch (\Exception $e) {
58
                throw new \RuntimeException(
59
                    sprintf(
60
                        'Invalid doctrine migration check under "%s.%s": %s',
61
                        $groupName,
62
                        $key,
63
                        $e->getMessage()
64
                    ), $e->getCode(), $e
65
                );
66
            }
67
        }
68
69
        return $services;
70
    }
71
}
72