Completed
Push — master ( 565813...d70e1f )
by Vaibhav
01:44
created

ConfigServiceProvider   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 29
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
wmc 6
lcom 0
cbo 1
dl 0
loc 29
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
B register() 0 23 6
1
<?php
2
3
/*
4
 * This file is part of vaibhavpandeyvpz/pimple-config package.
5
 *
6
 * (c) Vaibhav Pandey <[email protected]>
7
 *
8
 * This source file is subject to the MIT license that is bundled
9
 * with this source code in the file LICENSE.md.
10
 */
11
12
namespace Pimple\Config;
13
14
use InvalidArgumentException;
15
use RuntimeException;
16
use Pimple\Container;
17
use Pimple\ServiceProviderInterface;
18
19
/**
20
 * Class ConfigServiceProvider
21
 * @package Pimple\Config
22
 */
23
class ConfigServiceProvider implements ServiceProviderInterface
24
{
25
    /**
26
     * @inheritdoc
27
     */
28
    public function register(Container $pimple)
29
    {
30
        $pimple['config.files'] = function ($pimple) {
31
            return [$pimple['config.file']];
32
        };
33
        $pimple['config.hydrate'] = $pimple->protect(function ($pimple) {
34
            foreach ($pimple['config.files'] as $file) {
35
                if (is_file($file) && is_readable($file)) {
36
                    /** @noinspection PhpIncludeInspection */
37
                    $values = require $file;
38
                    if (is_array($contents)) {
0 ignored issues
show
Bug introduced by
The variable $contents does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
39
                        foreach ($values as $key => $value) {
40
                            $pimple[$key] = $value;
41
                        }
42
                    } else {
43
                        throw new InvalidArgumentException("Configuration file must return an array");
44
                    }
45
                } else {
46
                    throw new RuntimeException("Could not find or read from {$file}");
47
                }
48
            }
49
        });
50
    }
51
}
52