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

ConfigureContainer   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 4
Bugs 0 Features 2
Metric Value
wmc 6
c 4
b 0
f 2
lcom 1
cbo 4
dl 0
loc 75
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A fromFiles() 0 6 1
A fromArray() 0 6 1
A prepareSettings() 0 13 1
A configureContainer() 0 21 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