Completed
Pull Request — master (#33)
by Tom
02:19
created

ConfigureContainer::fromFiles()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 6
rs 9.4285
cc 1
eloc 4
nc 1
nop 3
1
<?php
2
3
namespace TomPHP\ConfigServiceProvider;
4
5
final class ConfigureContainer
6
{
7
    /**
8
     * @api
9
     *
10
     * @param object $container
11
     * @param array  $patterns
12
     * @param array  $settings
13
     *
14
     * @return void
15
     */
16
    public static function fromFiles($container, array $patterns, $settings = [])
17
    {
18
        $settings = self::prepareSettings($settings);
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 2 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
19
        $appConfig = ApplicationConfig::fromFiles($patterns, $settings['config_separator']);
20
        self::configureContainer($container, $appConfig, $settings);
21
    }
22
23
    /**
24
     * @api
25
     *
26
     * @param object $container
27
     * @param array  $config
28
     * @param array  $settings
29
     *
30
     * @return void
31
     */
32
    public static function fromArray($container, array $config, $settings = [])
33
    {
34
        $settings = self::prepareSettings($settings);
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 2 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
35
        $appConfig = new ApplicationConfig($config, $settings['config_separator']);
36
        self::configureContainer($container, $appConfig, $settings);
37
    }
38
39
    /**
40
     * @param array $settings
41
     *
42
     * @return array
43
     */
44
    private static function prepareSettings(array $settings)
45
    {
46
        return array_merge(
47
            [
48
                'config_prefix'      => 'config',
49
                'config_separator'   => '.',
50
                'services_key'       => 'di.services',
51
                'inflectors_key'     => 'di.inflectors',
52
                'singleton_services' => false,
53
            ],
54
            $settings
55
        );
56
    }
57
58
    private static function configureContainer($container, ApplicationConfig $appConfig, array $settings)
0 ignored issues
show
Coding Style Best Practice introduced by
Please use __construct() instead of a PHP4-style constructor that is named after the class.
Loading history...
59
    {
60
        $factory = new ConfiguratorFactory([
61
            'League\Container\Container' => 'TomPHP\ConfigServiceProvider\League\Configurator',
62
            'Pimple\Container'           => 'TomPHP\ConfigServiceProvider\Pimple\Configurator',
63
        ]);
64
65
        $configurator = $factory->create($container);
66
67
        $configurator->addApplicationConfig($appConfig, $settings['config_prefix']);
68
69
        if (isset($appConfig[$settings['services_key']])) {
70
            $configurator->addServiceConfig(
71
                new ServiceConfig($appConfig[$settings['services_key']], $settings['singleton_services'])
72
            );
73
        }
74
75
        if (isset($appConfig[$settings['inflectors_key']])) {
76
            $configurator->addInflectorConfig(new InflectorConfig($appConfig[$settings['inflectors_key']]));
77
        }
78
    }
79
}
80