Completed
Push — master ( 992188...6b183b )
by Tobias
06:43
created

TranslationAdapterLocoExtension::load()   B

Complexity

Conditions 6
Paths 3

Size

Total Lines 41
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 18
CRAP Score 8.304

Importance

Changes 0
Metric Value
dl 0
loc 41
ccs 18
cts 30
cp 0.6
rs 8.439
c 0
b 0
f 0
cc 6
eloc 31
nc 3
nop 2
crap 8.304
1
<?php
2
3
/*
4
 * This file is part of the PHP Translation package.
5
 *
6
 * (c) PHP Translation team <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Translation\PlatformAdapter\Loco\Bridge\Symfony\DependencyInjection;
13
14
use FAPI\Localise\HttpClientConfigurator;
15
use FAPI\Localise\LocoClient;
16
use FAPI\Localise\RequestBuilder;
17
use Symfony\Component\DependencyInjection\ContainerBuilder;
18
use Symfony\Component\DependencyInjection\Definition;
19
use Symfony\Component\DependencyInjection\Reference;
20
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
21
use Translation\PlatformAdapter\Loco\Loco;
22
use Translation\PlatformAdapter\Loco\Model\LocoProject;
23
24
/**
25
 * @author Tobias Nyholm <[email protected]>
26
 */
27
class TranslationAdapterLocoExtension extends Extension
28
{
29
    /**
30
     * {@inheritdoc}
31
     */
32 1
    public function load(array $configs, ContainerBuilder $container)
33
    {
34 1
        $configuration = new Configuration();
35 1
        $config = $this->processConfiguration($configuration, $configs);
36
37 1
        $projects = [];
38 1
        $globalIndexParameter = $config['index_parameter'];
39 1
        foreach ($config['projects'] as $name => $data) {
40
            if (empty($data['index_parameter'])) {
41
                $data['index_parameter'] = $globalIndexParameter;
42
            }
43
44
            $projectDefinition = new Definition(LocoProject::class);
45
            $projectDefinition
46
                ->addArgument($name)
47
                ->addArgument($data);
48
            $projects[] = $projectDefinition;
49
        }
50
51
        $requestBuilder = (new Definition(RequestBuilder::class))
52
            ->addArgument(empty($config['httplug_message_factory']) ? null : new Reference($config['httplug_message_factory']));
53
54
        $clientConfigurator = (new Definition(HttpClientConfigurator::class))
55
            ->addArgument(empty($config['httplug_client']) ? null : new Reference($config['httplug_client']))
56
            ->addArgument(empty($config['httplug_uri_factory']) ? null : new Reference($config['httplug_uri_factory']));
57
58 1
        $apiDef = $container->register('php_translation.adapter.loco.raw');
59 1
        $apiDef->setClass(LocoClient::class)
60
            ->setFactory([LocoClient::class, 'configure'])
61 1
            ->setPublic(true)
62 1
            ->addArgument($clientConfigurator)
63 1
            ->addArgument(null)
64
            ->addArgument($requestBuilder);
65 1
66 1
        $adapterDef = $container->register('php_translation.adapter.loco');
67 1
        $adapterDef
68 1
            ->setClass(Loco::class)
69 1
            ->setPublic(true)
70 1
            ->addArgument($apiDef)
71 1
            ->addArgument($projects);
72
    }
73
}
74