Common::getUploadersNode()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 35
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 29
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 35
rs 9.456
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 DocRaptor\Doc;
25
use Mpdf\Mpdf;
26
use Parthenon\Common\Exception\MissingDependencyException;
27
use Parthenon\Common\Exception\ParameterNotSetException;
28
use Parthenon\Common\RequestHandler\RequestHandlerInterface;
29
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
30
use Symfony\Component\Config\Definition\Builder\NodeBuilder;
31
use Symfony\Component\Config\Definition\Builder\NodeDefinition;
32
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
33
use Symfony\Component\Config\FileLocator;
34
use Symfony\Component\DependencyInjection\ContainerBuilder;
35
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
36
37
final class Common implements ModuleConfigurationInterface
38
{
39
    public function addConfig(NodeBuilder $nodeBuilder): void
40
    {
41
        $nodeBuilder->arrayNode('common')
42
            ->children()
43
                ->scalarNode('site_url')->end()
44
                ->arrayNode('elasticsearch')
0 ignored issues
show
Bug introduced by
The method arrayNode() 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

44
                ->/** @scrutinizer ignore-call */ arrayNode('elasticsearch')
Loading history...
45
                    ->children()
46
                        ->arrayNode('hosts')->scalarPrototype()->end()->end()
47
                        ->scalarNode('connection_type')->end()
48
                        ->scalarNode('cloud_id')->end()
49
                        ->scalarNode('api_key')->end()
50
                        ->scalarNode('api_id')->end()
51
                        ->scalarNode('basic_username')->end()
52
                        ->scalarNode('basic_password')->end()
53
                    ->end()
54
                ->end()
55
                ->arrayNode('pdf')
56
                    ->children()
57
                        ->scalarNode('generator')->end()
58
                        ->arrayNode('mpdf')
59
                            ->children()
60
                                ->scalarNode('tmp_dir')->defaultValue('/tmp')->end()
61
                            ->end()
62
                        ->end()
63
                        ->arrayNode('docraptor')
64
                            ->children()
65
                                ->scalarNode('api_key')->end()
66
                            ->end()
67
                        ->end()
68
                        ->arrayNode('wkhtmltopdf')
69
                            ->children()
70
                                ->scalarNode('bin')->defaultValue('/usr/bin/wkhtmltopdf')->end()
71
                            ->end()
72
                        ->end()
73
                    ->end()
74
                ->end()
75
            ->end()
76
            ->fixXmlConfig('uploaders')
77
            ->append($this->getUploadersNode())
78
        ->end();
79
    }
80
81
    public function handleDefaultParameters(ContainerBuilder $container): void
82
    {
83
        $container->setParameter('parthenon_common_site_url', 'http://localhost');
84
        $container->setParameter('parthenon_common_pdf_wkhtmltopdf_bin', '');
85
        $container->setParameter('parthenon_common_pdf_docraptor_api_key', '');
86
        $container->setParameter('parthenon_common_uploaders', []);
87
    }
88
89
    public function handleConfiguration(array $config, ContainerBuilder $container): void
90
    {
91
        $container->registerForAutoconfiguration(RequestHandlerInterface::class)->addTag('parthenon.common.request_handler');
92
93
        $loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../../Resources/config'));
94
        $loader->load('services/common.xml');
95
96
        if (!isset($config['common'])) {
97
            return;
98
        }
99
100
        $container->setParameter('parthenon_common_site_url', $config['common']['site_url'] ?? '');
101
102
        $config = $this->configureUploader($config, $container);
103
        $config = $this->configurePdf($config, $container, $loader);
104
105
        $this->configureElasticsearch($config, $container);
106
    }
107
108
    private function getUploadersNode(): NodeDefinition
109
    {
110
        $treeBuilder = new TreeBuilder('uploader');
111
        $node = $treeBuilder->getRootNode();
112
113
        /** @var ArrayNodeDefinition $uploaderNode */
114
        $uploaderNode = $node
115
            ->requiresAtLeastOneElement()
0 ignored issues
show
Bug introduced by
The method requiresAtLeastOneElement() does not exist on Symfony\Component\Config...\Builder\NodeDefinition. It seems like you code against a sub-type of Symfony\Component\Config...\Builder\NodeDefinition such as Symfony\Component\Config...der\ArrayNodeDefinition. ( Ignorable by Annotation )

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

115
            ->/** @scrutinizer ignore-call */ requiresAtLeastOneElement()
Loading history...
116
            ->useAttributeAsKey('name')
117
            ->prototype('array');
118
119
        $uploaderNode
120
            ->children()
121
                ->scalarNode('provider')->end()
122
                ->scalarNode('naming_strategy')->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

122
                ->/** @scrutinizer ignore-call */ scalarNode('naming_strategy')->end()
Loading history...
123
                ->scalarNode('url')->end()
124
                ->arrayNode('local')
125
                    ->children()
126
                        ->scalarNode('path')->end()
127
                    ->end()
128
                ->end()
129
                ->arrayNode('s3')
130
                    ->children()
131
                        ->scalarNode('key')->end()
132
                        ->scalarNode('secret')->end()
133
                        ->scalarNode('region')->end()
134
                        ->scalarNode('endpoint')->end()
135
                        ->scalarNode('bucket_name')->end()
136
                        ->scalarNode('version')->end()
137
                        ->scalarNode('visibility')->end()
138
                    ->end()
139
                ->end()
140
            ->end();
141
142
        return $node;
143
    }
144
145
    private function configureElasticsearch(array $config, ContainerBuilder $container): void
146
    {
147
        if (isset($config['common']['elasticsearch'])) {
148
            $elasticsearchConfig = $config['common']['elasticsearch'];
149
150
            $definition = $container->getDefinition('parthenon.common.elasticsearch.config');
151
152
            if (isset($elasticsearchConfig['connection_type'])) {
153
                $definition->addMethodCall('setConnectionType', [$elasticsearchConfig['connection_type']]);
154
            }
155
156
            if (isset($elasticsearchConfig['hosts'])) {
157
                $definition->addMethodCall('setHosts', [$elasticsearchConfig['hosts']]);
158
            }
159
160
            if (isset($elasticsearchConfig['api_id'])) {
161
                $definition->addMethodCall('setApiId', [$elasticsearchConfig['api_id']]);
162
            }
163
164
            if (isset($elasticsearchConfig['api_key'])) {
165
                $definition->addMethodCall('setApiKey', [$elasticsearchConfig['api_key']]);
166
            }
167
168
            if (isset($elasticsearchConfig['basic_username'])) {
169
                $definition->addMethodCall('setBasicUsername', [$elasticsearchConfig['basic_username']]);
170
            }
171
172
            if (isset($elasticsearchConfig['basic_password'])) {
173
                $definition->addMethodCall('setBasicPassword', [$elasticsearchConfig['basic_password']]);
174
            }
175
            $container->setDefinition('parthenon.common.elasticsearch.config', $definition);
176
        }
177
    }
178
179
    /**
180
     * @throws ParameterNotSetException
181
     */
182
    private function configurePdf(array $config, ContainerBuilder $container, XmlFileLoader $loader): array
183
    {
184
        if (isset($config['common']['pdf']['generator']) && 'docraptor' === $config['common']['pdf']['generator']) {
185
            if (!class_exists(Doc::class)) {
186
                throw new MissingDependencyException('To use docraptor you need to have the docraptor/docraptor package installed. Do composer require docraptor/docraptor.');
187
            }
188
189
            if (!isset($config['common']['pdf']['docraptor']) || !$config['common']['pdf']['docraptor']['api_key']) {
190
                throw new ParameterNotSetException('When pdf generator is docraptor you need to set parthenon.common.pdf.docraptor.api_key');
191
            }
192
            $container->setParameter('parthenon_common_pdf_docraptor_api_key', $config['common']['pdf']['docraptor']['api_key']);
193
194
            $loader->load('services/common/pdf/docraptor.xml');
195
        } elseif (isset($config['common']['pdf']['generator']) && 'mpdf' === $config['common']['pdf']['generator']) {
196
            if (!class_exists(Mpdf::class)) {
197
                throw new MissingDependencyException('To use mpdf you need to have the mpdf/mpdf package installed. Do composer require mpdf/mpdf.');
198
            }
199
200
            if (!isset($config['common']['pdf']['mpdf'])) {
201
                $dir = '/tmp/';
202
            } else {
203
                $dir = $config['common']['pdf']['mpdf']['tmp_dir'] ?? '/tmp';
204
            }
205
            $container->setParameter('parthenon.common.pdf.mpdf.tmp_dir', $dir);
206
            $loader->load('services/common/pdf/mpdf.xml');
207
        } elseif (isset($config['common']['pdf']['generator']) && 'wkhtmltopdf' === $config['common']['pdf']['generator']) {
208
            if (!class_exists(\Knp\Snappy\Pdf::class)) {
209
                throw new MissingDependencyException('To use wkhtmltopdf you need to have the knplabs/knp-snappy. Do composer require knplabs/knp-snappy.');
210
            }
211
212
            $container->setParameter('parthenon_common_pdf_wkhtmltopdf_bin', $config['common']['pdf']['wkhtmltopdf']['bin']);
213
            $loader->load('services/common/pdf/wkhtmltopdf.xml');
214
        }
215
216
        return $config;
217
    }
218
219
    private function configureUploader(array $config, ContainerBuilder $container): array
220
    {
221
        if (isset($config['common']['uploader']) && is_array($config['common']['uploader'])) {
222
            $container->setParameter('parthenon_common_uploaders', $config['common']['uploader']);
223
        }
224
225
        return $config;
226
    }
227
}
228