Passed
Branch main (b6a268)
by Iain
04:04
created

MultiTenancyCompilerPass   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 30
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 17
c 1
b 0
f 0
dl 0
loc 30
rs 10
wmc 4

1 Method

Rating   Name   Duplication   Size   Complexity  
A process() 0 28 4
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * Copyright Humbly Arrogant Ltd 2020-2022.
7
 *
8
 * Use of this software is governed by the Business Source License included in the LICENSE file and at https://getparthenon.com/docs/next/license.
9
 *
10
 * Change Date: TBD ( 3 years after 2.0.0 release )
11
 *
12
 * On the date above, in accordance with the Business Source License, use of this software will be governed by the open source license specified in the LICENSE file.
13
 */
14
15
namespace Parthenon\MultiTenancy\Compiler;
16
17
use Parthenon\Common\Compiler\AbstractCompilerPass;
18
use Parthenon\Common\Exception\GeneralException;
19
use Parthenon\MultiTenancy\TenantProvider\TenantProviderInterface;
20
use Symfony\Component\DependencyInjection\ContainerBuilder;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\Depend...ection\ContainerBuilder was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
21
use Symfony\Component\DependencyInjection\Reference;
22
23
final class MultiTenancyCompilerPass extends AbstractCompilerPass
24
{
25
    public function process(ContainerBuilder $container)
26
    {
27
        $enabled = $container->getParameter('parthenon_multi_tenancy_enabled');
28
29
        if (!$enabled) {
30
            return;
31
        }
32
33
        $dbalConnection = $container->getParameter('parthenon_multi_tenancy_dbal_connection');
34
        $id = sprintf('doctrine.dbal.%s_connection', $dbalConnection);
35
        if (!$container->hasDefinition($id)) {
36
            throw new GeneralException(sprintf('There is no dbal connection called %s', $dbalConnection));
37
        }
38
39
        $definition = $container->getDefinition($id);
40
        $definition->addMethodCall('setCurrentTenantProvider', [new Reference(TenantProviderInterface::class)]);
41
        $container->setDefinition($id, $definition);
42
43
        $container->setAlias('parthenon.multi_tenancy.dbal.connection', $id);
44
45
        $globalDbalConnection = $container->getParameter('parthenon_multi_tenancy_global_dbal_connection');
46
47
        if (empty($globalDbalConnection)) {
48
            $globalDbalConnection = 'default';
49
        }
50
51
        $globalId = sprintf('doctrine.dbal.%s_connection', $globalDbalConnection);
52
        $container->setAlias('parthenon.multi_tenancy.dbal.global_connection', $globalId);
53
    }
54
}
55