Completed
Push — master ( f2c439...2a9983 )
by Neomerx
04:50
created

L10nContainerConfigurator::configureContainer()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 54
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 54
rs 9.6716
c 0
b 0
f 0
cc 1
eloc 19
nc 1
nop 1

3 Methods

Rating   Name   Duplication   Size   Complexity  
A L10nContainerConfigurator.php$0 ➔ __construct() 0 5 1
A L10nContainerConfigurator.php$0 ➔ createFormatter() 0 4 1
A L10nContainerConfigurator.php$0 ➔ createFormatterForLocale() 0 7 1

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php namespace Limoncello\Application\Packages\L10n;
2
3
/**
4
 * Copyright 2015-2017 [email protected]
5
 *
6
 * Licensed under the Apache License, Version 2.0 (the "License");
7
 * you may not use this file except in compliance with the License.
8
 * You may obtain a copy of the License at
9
 *
10
 * http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing, software
13
 * distributed under the License is distributed on an "AS IS" BASIS,
14
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
 * See the License for the specific language governing permissions and
16
 * limitations under the License.
17
 */
18
19
use Limoncello\Application\Packages\L10n\L10nSettings as S;
20
use Limoncello\Contracts\Application\ContainerConfiguratorInterface;
21
use Limoncello\Contracts\Container\ContainerInterface as LimoncelloContainerInterface;
22
use Limoncello\Contracts\L10n\FormatterFactoryInterface;
23
use Limoncello\Contracts\L10n\FormatterInterface;
24
use Limoncello\Contracts\Settings\SettingsProviderInterface;
25
use Limoncello\l10n\Format\Formatter;
26
use Limoncello\l10n\Format\Translator;
27
use Limoncello\l10n\Messages\BundleStorage;
28
use Psr\Container\ContainerInterface as PsrContainerInterface;
29
30
/**
31
 * @package Limoncello\Application
32
 */
33
class L10nContainerConfigurator implements ContainerConfiguratorInterface
34
{
35
    /**
36
     * @inheritdoc
37
     */
38
    public static function configureContainer(LimoncelloContainerInterface $container): void
39
    {
40
        $container[FormatterFactoryInterface::class] = function (PsrContainerInterface $container) {
41
            $settingsProvider = $container->get(SettingsProviderInterface::class);
42
            $settings         = $settingsProvider->get(S::class);
43
44
            $defaultLocale = $settings[S::KEY_DEFAULT_LOCALE];
45
            $storageData   = $settings[S::KEY_LOCALES_DATA];
46
47
            $factory = new class ($defaultLocale, $storageData) implements FormatterFactoryInterface
48
            {
49
                /**
50
                 * @var string
51
                 */
52
                private $defaultLocale;
53
54
                /**
55
                 * @var array
56
                 */
57
                private $storageData;
58
59
                /**
60
                 * @param string $defaultLocale
61
                 * @param array  $storageData
62
                 */
63
                public function __construct(string $defaultLocale, array $storageData)
64
                {
65
                    $this->defaultLocale = $defaultLocale;
66
                    $this->storageData   = $storageData;
67
                }
68
69
                /**
70
                 * @inheritdoc
71
                 */
72
                public function createFormatter(string $namespace): FormatterInterface
73
                {
74
                    return $this->createFormatterForLocale($namespace, $this->defaultLocale);
75
                }
76
77
                /**
78
                 * @inheritdoc
79
                 */
80
                public function createFormatterForLocale(string $namespace, string $locale): FormatterInterface
81
                {
82
                    $translator = new Translator(new BundleStorage($this->storageData));
83
                    $formatter  = new Formatter($locale, $namespace, $translator);
84
85
                    return $formatter;
86
                }
87
            };
88
89
            return $factory;
90
        };
91
    }
92
}
93