Export::configureDoctrine()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 2
nc 2
nop 2
dl 0
loc 4
rs 10
c 1
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * Copyright (C) 2020-2025 Iain Cambridge
7
 *
8
 * This program is free software: you can redistribute it and/or modify
9
 * it under the terms of the GNU LESSER GENERAL PUBLIC LICENSE as published by
10
 * the Free Software Foundation, either version 2.1 of the License, or
11
 * (at your option) any later version.
12
 *
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 * GNU Lesser General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU General Public License
19
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
20
 */
21
22
namespace Parthenon\DependencyInjection\Modules;
23
24
use Parthenon\Common\Exception\MissingDependencyException;
25
use Parthenon\Export\DataProvider\DataProviderInterface;
26
use Parthenon\Export\Engine\BackgroundDownloadEngine;
27
use Parthenon\Export\Engine\BackgroundEmailEngine;
28
use Parthenon\Export\Engine\DirectDownloadEngine;
29
use Parthenon\Export\Engine\EngineInterface;
30
use Parthenon\Export\Exporter\ExporterInterface;
31
use Parthenon\Export\Normaliser\NormaliserInterface;
32
use Symfony\Component\Config\Definition\Builder\NodeBuilder;
33
use Symfony\Component\Config\FileLocator;
34
use Symfony\Component\DependencyInjection\ContainerBuilder;
35
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
36
37
class Export implements ModuleConfigurationInterface
38
{
39
    public function addConfig(NodeBuilder $nodeBuilder): void
40
    {
41
        $nodeBuilder
42
            ->arrayNode('export')
43
                ->children()
44
                    ->booleanNode('enabled')->defaultFalse()->end()
45
                    ->scalarNode('default_engine')->defaultValue('direct_download')->end()
0 ignored issues
show
Bug introduced by
The method scalarNode() does not exist on Symfony\Component\Config...der\NodeParentInterface. It seems like you code against a sub-type of Symfony\Component\Config...der\NodeParentInterface such as Symfony\Component\Config...ion\Builder\NodeBuilder. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

45
                    ->/** @scrutinizer ignore-call */ scalarNode('default_engine')->defaultValue('direct_download')->end()
Loading history...
46
                    ->scalarNode('user_provider')->end()
47
                ->end()
48
            ->end();
49
    }
50
51
    public function handleDefaultParameters(ContainerBuilder $container): void
52
    {
53
    }
54
55
    public function handleConfiguration(array $config, ContainerBuilder $container): void
56
    {
57
        if (!isset($config['export']) || !isset($config['export']['enabled']) || false == $config['export']['enabled']) {
58
            return;
59
        }
60
61
        $container->setParameter('parthenon_export_enabled', true);
62
63
        $loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../../Resources/config'));
64
        $loader->load('services/export.xml');
65
66
        $container->registerForAutoconfiguration(NormaliserInterface::class)->addTag('parthenon.export.normaliser');
67
        $container->registerForAutoconfiguration(ExporterInterface::class)->addTag('parthenon.export.exporter');
68
        $container->registerForAutoconfiguration(DataProviderInterface::class)->addTag('parthenon.export.data_provider');
69
70
        $bundles = $container->getParameter('kernel.bundles');
71
72
        $this->configureMongoDb($bundles, $loader);
73
        $this->configureDoctrine($bundles, $loader);
74
75
        if (isset($config['export']['default_engine'])) {
76
            $defaultEngine = $config['export']['default_engine'];
77
78
            if (DirectDownloadEngine::NAME === $defaultEngine) {
79
                $container->setAlias(EngineInterface::class, DirectDownloadEngine::class);
80
            } elseif (BackgroundEmailEngine::class === $defaultEngine) {
81
                $container->setAlias(EngineInterface::class, BackgroundEmailEngine::class);
82
            } elseif (BackgroundDownloadEngine::NAME === $defaultEngine) {
83
                $container->setAlias(EngineInterface::class, BackgroundDownloadEngine::class);
84
            }
85
        }
86
87
        if (isset($config['export']['user_provider'])) {
88
            if (!$container->hasDefinition($config['export']['user_provider'])) {
89
                throw new MissingDependencyException(sprintf("The service '%s' for user provider for the export system", $config['export']['user_provider']));
90
            }
91
            $container->setAlias('parthenon.export.user_provider', $config['export']['user_provider']);
92
        }
93
    }
94
95
    /**
96
     * @throws \Exception
97
     */
98
    private function configureDoctrine(float|array|bool|int|string|null $bundles, XmlFileLoader $loader): void
99
    {
100
        if (isset($bundles['DoctrineBundle'])) {
101
            $loader->load('services/orm/export.xml');
102
        }
103
    }
104
105
    /**
106
     * @throws \Exception
107
     */
108
    private function configureMongoDb(float|int|bool|array|string|null $bundles, XmlFileLoader $loader): void
109
    {
110
        if (isset($bundles['DoctrineMongoDBBundle'])) {
111
            $loader->load('services/odm/export.xml');
112
        }
113
    }
114
}
115