Passed
Push — master ( d98237...ada1ad )
by Gerrit
13:03
created

RDMCompilerPass::findDoctrineConnectionName()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 5
c 1
b 0
f 0
nc 3
nop 1
dl 0
loc 12
rs 10
1
<?php
2
/**
3
 * Copyright (C) 2019 Gerrit Addiks.
4
 * This package (including this file) was released under the terms of the GPL-3.0.
5
 * You should have received a copy of the GNU General Public License along with this program.
6
 * If not, see <http://www.gnu.org/licenses/> or send me a mail so i can send you a copy.
7
 *
8
 * @license GPL-3.0
9
 *
10
 * @author Gerrit Addiks <[email protected]>
11
 */
12
13
namespace Addiks\RDMBundle\DependencyInjection;
14
15
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
16
use Symfony\Component\DependencyInjection\ContainerBuilder;
17
use Symfony\Component\DependencyInjection\Alias;
18
19
final class RDMCompilerPass implements CompilerPassInterface
20
{
21
    public function process(ContainerBuilder $container)
22
    {
23
        $container->setAlias(
24
            'addiks_rdm.doctrine.orm.configuration', 
25
            new Alias(sprintf(
26
                'doctrine.orm.%s_configuration',
27
                $this->findDoctrineConnectionName($container) ?? 'default'
28
            ))
29
        );
30
    }
31
    
32
    private function findDoctrineConnectionName(ContainerBuilder $container): ?string
33
    {
34
        /** @var array<int, array<string, mixed>> $doctrineConfigs */
35
        $doctrineConfigs = $container->getExtensionConfig('doctrine');
36
        
37
        foreach ($doctrineConfigs as $doctrineConfig) {
38
            foreach ($doctrineConfig['dbal']['connections'] ?? [] as $name => $connection) {
39
                return $name;
40
            }
41
        }
42
        
43
        return null;
44
    }
45
    
46
}
47