Completed
Push — master ( c2b032...2a92e2 )
by Kevin
06:46
created

DoctrineMigrationsLoader::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2.032

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 4
cts 5
cp 0.8
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 0
crap 2.032
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 49
    public function __construct()
25
    {
26 49
        $this->loader = class_exists(DoctrineCommand::class)
27
            ? new V2MigrationsLoader()
28 49
            : new V3MigrationsLoader();
29 49
    }
30
31 40
    public function process(ContainerBuilder $container)
32
    {
33 40
        if (!($this->loader instanceof CompilerPassInterface)) {
34 40
            return;
35
        }
36
37
        $this->loader->process($container);
38
    }
39
40
    public function loadMigrationChecks(ContainerBuilder $container, array $migrationChecksConfig, string $groupName): array
41
    {
42
        $services = [];
43
        foreach ($migrationChecksConfig as $key => $config) {
44
            try {
45
                $serviceId = sprintf('liip_monitor.check.doctrine_migrations.configuration.%s.%s', $groupName, $key);
46
                $this->loader->createMigrationConfigurationService(
47
                    $container,
48
                    $config['connection'],
49
                    $serviceId,
50
                    $config['configuration_file'] ?? null
51
                );
52
53
                $services[$key] = $serviceId;
54
            } catch (\Exception $e) {
55
                throw new \RuntimeException(sprintf('Invalid doctrine migration check under "%s.%s": %s', $groupName, $key, $e->getMessage()), $e->getCode(), $e);
56
            }
57
        }
58
59
        return $services;
60
    }
61
}
62