Completed
Push — master ( 1008a1...9cabe1 )
by Tobias
01:30
created

TranslationAdapterLocoExtension   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 5

Test Coverage

Coverage 66.67%

Importance

Changes 0
Metric Value
wmc 8
lcom 0
cbo 5
dl 0
loc 54
ccs 24
cts 36
cp 0.6667
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
C load() 0 48 8
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
        $domainToProjectMap = [];
38 1
        $globalIndexParameter = $config['index_parameter'];
39 1
        foreach ($config['projects'] as $domain => $data) {
40
            if (empty($data['index_parameter'])) {
41
                $data['index_parameter'] = $globalIndexParameter;
42
            }
43
            if (empty($data['domains'])) {
44
                $projectDefinition = (new Definition(LocoProject::class))
45
                    ->addArgument($domain)
46
                    ->addArgument($data);
47
                $domainToProjectMap[$domain] = $projectDefinition;
48
            } else {
49
                foreach ($data['domains'] as $d) {
50
                    $projectDefinition = (new Definition(LocoProject::class))
51
                        ->addArgument($d)
52
                        ->addArgument($data);
53
                    $domainToProjectMap[$d] = $projectDefinition;
54
                }
55
            }
56
        }
57
58 1
        $requestBuilder = (new Definition(RequestBuilder::class))
59 1
            ->addArgument(empty($config['httplug_message_factory']) ? null : new Reference($config['httplug_message_factory']));
60
61 1
        $clientConfigurator = (new Definition(HttpClientConfigurator::class))
62 1
            ->addArgument(empty($config['httplug_client']) ? null : new Reference($config['httplug_client']))
63 1
            ->addArgument(empty($config['httplug_uri_factory']) ? null : new Reference($config['httplug_uri_factory']));
64
65 1
        $apiDef = $container->register('php_translation.adapter.loco.raw');
66 1
        $apiDef->setClass(LocoClient::class)
67 1
            ->setFactory([LocoClient::class, 'configure'])
68 1
            ->setPublic(true)
69 1
            ->addArgument($clientConfigurator)
70 1
            ->addArgument(null)
71 1
            ->addArgument($requestBuilder);
72
73 1
        $adapterDef = $container->register('php_translation.adapter.loco');
74
        $adapterDef
75 1
            ->setClass(Loco::class)
76 1
            ->setPublic(true)
77 1
            ->addArgument($apiDef)
78 1
            ->addArgument($domainToProjectMap);
79 1
    }
80
}
81