TranslatorFactory::getTranslationConfig()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 8
dl 0
loc 10
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
/**
3
 * This File is Part of the Validus Translation package.
4
 *
5
 * @copyright (c) 2018 Validus <https://github.com/ValidusPHP/>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
declare(strict_types=1);
12
13
namespace Validus\Translation;
14
15
use function array_merge;
16
use InvalidArgumentException;
17
use Psr\Container\ContainerInterface;
18
use Symfony\Component\Translation\Formatter\MessageFormatter;
19
use Symfony\Component\Translation\Formatter\MessageFormatterInterface;
20
use Symfony\Component\Translation\Translator;
21
22
class TranslatorFactory
23
{
24
    /**
25
     * @param ContainerInterface $container
26
     *
27
     * @return Translator
28
     */
29
    public function __invoke(ContainerInterface $container): Translator
30
    {
31
        $config = $container->has('config') ? $container->get('config') : [];
32
        $debug = $config['debug'] ?? true;
33
34
        $config = $this->getTranslationConfig($config);
35
36
        /** @var MessageFormatterInterface::class $formatter */
37
        $formatter = $config['formatter'];
38
        /** @var MessageFormatterInterface $formatter */
39
        $formatter = $container->has($formatter) ? $container->get($formatter) : new $formatter();
0 ignored issues
show
Bug introduced by
$formatter of type Symfony\Component\Transl...ssageFormatterInterface is incompatible with the type string expected by parameter $id of Psr\Container\ContainerInterface::has(). ( Ignorable by Annotation )

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

39
        $formatter = $container->has(/** @scrutinizer ignore-type */ $formatter) ? $container->get($formatter) : new $formatter();
Loading history...
Bug introduced by
$formatter of type Symfony\Component\Transl...ssageFormatterInterface is incompatible with the type string expected by parameter $id of Psr\Container\ContainerInterface::get(). ( Ignorable by Annotation )

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

39
        $formatter = $container->has($formatter) ? $container->get(/** @scrutinizer ignore-type */ $formatter) : new $formatter();
Loading history...
40
41
        $cache = $debug ? null : $config['cache_dir'];
42
        $translator = new Translator($config['locale'], $formatter, $cache, $debug);
43
44
        $this->addLoaders($container, $translator, $config['loaders']);
45
        $this->addResources($translator, $config['resources']);
46
47
        $translator->setFallbackLocales($config['fallback']);
48
        $translator->setLocale($config['locale']);
49
50
        return $translator;
51
    }
52
53
    /**
54
     * Add resources to the given translator instance.
55
     *
56
     * @param Translator $translator
57
     * @param array      $resources
58
     */
59
    protected function addResources(Translator $translator, array $resources = []): void
60
    {
61
        foreach ($resources as $id => $resource) {
62
            if (!isset($resource['format'])) {
63
                throw new InvalidArgumentException("resource format is missing from resources configuration#{$id}.");
64
            }
65
66
            if (!isset($resource['resource'])) {
67
                throw new InvalidArgumentException("resource is missing from resources configuration#{$id}.");
68
            }
69
70
            $resource['locale'] = $resource['locale'] ?? $config['locale'] ?? 'en';
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $config seems to never exist and therefore isset should always be false.
Loading history...
71
            $resource['domain'] = $resource['domain'] ?? 'messages';
72
73
            $translator->addResource($resource['format'], $resource['resource'], $resource['locale'], $resource['domain']);
74
        }
75
    }
76
77
    /**
78
     * Add loaders to the given translator instance.
79
     *
80
     * @param ContainerInterface $container
81
     * @param Translator         $translator
82
     * @param array              $loaders
83
     */
84
    protected function addLoaders(ContainerInterface $container, Translator $translator, array $loaders = []): void
85
    {
86
        foreach ($loaders as $format => $loader) {
87
            if (\is_string($loader)) {
88
                $loader = $container->has($loader) ? $container->get($loader) : new $loader();
89
            }
90
91
            $translator->addLoader($format, $loader);
92
        }
93
    }
94
95
    /**
96
     * @param array $config
97
     *
98
     * @return array
99
     */
100
    protected function getTranslationConfig(array $config): array
101
    {
102
        return array_merge([
103
            'locale' => 'en',
104
            'cache_dir' => null,
105
            'fallback' => [],
106
            'formatter' => MessageFormatter::class,
107
            'loaders' => [],
108
            'resources' => [],
109
        ], $config['translation'] ?? []);
110
    }
111
}
112