Completed
Push — master ( a443c1...768364 )
by Tobias
06:26
created

TranslationAdapterLocoExtension::load()   C

Complexity

Conditions 7
Paths 3

Size

Total Lines 38
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 23
CRAP Score 7.1591

Importance

Changes 0
Metric Value
dl 0
loc 38
ccs 23
cts 27
cp 0.8519
rs 6.7272
c 0
b 0
f 0
cc 7
eloc 28
nc 3
nop 2
crap 7.1591
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
23
/**
24
 * @author Tobias Nyholm <[email protected]>
25
 */
26
class TranslationAdapterLocoExtension extends Extension
27
{
28
    /**
29
     * {@inheritdoc}
30
     */
31 1
    public function load(array $configs, ContainerBuilder $container)
32
    {
33 1
        $configuration = new Configuration();
34 1
        $config = $this->processConfiguration($configuration, $configs);
35
36 1
        $domainToProjectMap = [];
37 1
        foreach ($config['projects'] as $domain => $data) {
38
            if (empty($data['domains'])) {
39
                $domainToProjectMap[$domain] = $data['api_key'];
40
            } else {
41
                foreach ($data['domains'] as $d) {
42
                    $domainToProjectMap[$d] = $data['api_key'];
43
                }
44
            }
45
        }
46
47 1
        $requestBuilder = (new Definition(RequestBuilder::class))
48 1
            ->addArgument(empty($config['httplug_message_factory']) ? null : new Reference($config['httplug_message_factory']));
49
50 1
        $clientConfigurator = (new Definition(HttpClientConfigurator::class))
51 1
            ->addArgument(empty($config['httplug_client']) ? null : new Reference($config['httplug_client']))
52 1
            ->addArgument(empty($config['httplug_uri_factory']) ? null : new Reference($config['httplug_uri_factory']));
53
54 1
        $apiDef = $container->register('php_translation.adapter.loco.raw');
55 1
        $apiDef->setClass(LocoClient::class)
56 1
            ->setFactory([LocoClient::class, 'configure'])
57 1
            ->setPublic(true)
58 1
            ->addArgument($clientConfigurator)
59 1
            ->addArgument(null)
60 1
            ->addArgument($requestBuilder);
61
62 1
        $adapterDef = $container->register('php_translation.adapter.loco');
63
        $adapterDef
64 1
            ->setClass(Loco::class)
65 1
            ->setPublic(true)
66 1
            ->addArgument($apiDef)
67 1
            ->addArgument($domainToProjectMap);
68 1
    }
69
}
70