SWPMultiTenancyExtension::loadPhpcr()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 27
rs 9.488
cc 2
nc 2
nop 3
1
<?php
2
3
/*
4
 * This file is part of the Superdesk Web Publisher MultiTenancy Bundle.
5
 *
6
 * Copyright 2015 Sourcefabric z.u. and contributors.
7
 *
8
 * For the full copyright and license information, please see the
9
 * AUTHORS and LICENSE files distributed with this source code.
10
 *
11
 * @copyright 2015 Sourcefabric z.ú
12
 * @license http://www.superdesk.org/license
13
 */
14
15
namespace SWP\Bundle\MultiTenancyBundle\DependencyInjection;
16
17
use SWP\Bundle\StorageBundle\DependencyInjection\Extension\Extension;
18
use SWP\Bundle\StorageBundle\Drivers;
19
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
20
use Symfony\Component\Config\FileLocator;
21
use Symfony\Component\DependencyInjection\ContainerBuilder;
22
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
23
24
/**
25
 * This is the class that loads and manages your bundle configuration.
26
 *
27
 * To learn more see {@link http://symfony.com/doc/current/cookbook/bundles/extension.html}
28
 */
29
class SWPMultiTenancyExtension extends Extension
30
{
31
    /**
32
     * {@inheritdoc}
33
     */
34
    public function load(array $configs, ContainerBuilder $container)
35
    {
36
        $configuration = new Configuration();
37
        $config = $this->processConfiguration($configuration, $configs);
38
39
        $loader = new YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
40
        $loader->load('services.yml');
41
        $loader->load('commands.yml');
42
43
        $backendEnabled = false;
44
45
        if ($config['persistence']['phpcr']['enabled']) {
46
            $this->loadPhpcr($config['persistence']['phpcr'], $loader, $container);
47
            $this->registerStorage(
48
                Drivers::DRIVER_DOCTRINE_PHPCR_ODM,
49
                $config['persistence']['phpcr']['classes'],
50
                $container
51
            );
52
53
            $backendEnabled = true;
54
        }
55
56
        if ($config['persistence']['orm']['enabled']) {
57
            $this->registerStorage(
58
                Drivers::DRIVER_DOCTRINE_ORM,
59
                $config['persistence']['orm']['classes'],
60
                $container
61
            );
62
63
            $backendEnabled = true;
64
        }
65
66
        if (!$backendEnabled) {
67
            throw new InvalidConfigurationException('You need to enable one of the peristence backends (phpcr or orm)');
68
        }
69
70
        if ($config['use_orm_listeners']) {
71
            $loader->load('listeners.yml');
72
        }
73
    }
74
75
    public function loadPhpcr($config, YamlFileLoader $loader, ContainerBuilder $container)
76
    {
77
        $keys = [
78
            'basepath' => 'basepath',
79
            'route_basepaths' => 'route_basepaths',
80
            'content_basepath' => 'content_basepath',
81
            'menu_basepath' => 'menu_basepath',
82
            'media_basepath' => 'media_basepath',
83
            'tenant_aware_router_class' => 'router.class',
84
        ];
85
86
        foreach ($keys as $sourceKey => $targetKey) {
87
            $container->setParameter(
88
                $this->getAlias().'.persistence.phpcr.'.$targetKey,
89
                $config[$sourceKey]
90
            );
91
        }
92
93
        array_push($config['route_basepaths'], $config['content_basepath'], $config['menu_basepath'], $config['media_basepath']);
94
95
        $container->setParameter(
96
            $this->getAlias().'.persistence.phpcr.base_paths',
97
            $config['route_basepaths']
98
        );
99
100
        $loader->load('phpcr.yml');
101
    }
102
}
103