Completed
Push — master ( 724ebd...bb6c14 )
by Marcin
18s queued 11s
created

ResponseBuilderServiceProvider::mergeConfig()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 5

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 10
c 1
b 0
f 0
nc 5
nop 2
dl 0
loc 17
ccs 11
cts 11
cp 1
crap 5
rs 9.6111
1
<?php
2
/**
3
 * Disable return type hint inspection as we do not have it specified in that
4
 * class for a purpose. The base class is also not having return type hints.
5
 *
6
 * @noinspection ReturnTypeCanBeDeclaredInspection
7
 */
8
9
declare(strict_types=1);
10
11
namespace MarcinOrlowski\ResponseBuilder;
12
13
/**
14
 * Laravel API Response Builder
15
 *
16
 * @package   MarcinOrlowski\ResponseBuilder
17
 *
18
 * @author    Marcin Orlowski <mail (#) marcinOrlowski (.) com>
19
 * @copyright 2016-2019 Marcin Orlowski
20
 * @license   http://www.opensource.org/licenses/mit-license.php MIT
21
 * @link      https://github.com/MarcinOrlowski/laravel-api-response-builder
22
 */
23
24
use Illuminate\Support\ServiceProvider;
25
26
class ResponseBuilderServiceProvider extends ServiceProvider
27
{
28
    protected $config_files = [
29
        'response_builder.php',
30
    ];
31
32
    /**
33
     * Register bindings in the container.
34
     *
35
     * @return void
36
     */
37
    public function register()
38
    {
39
        foreach ($this->config_files as $file) {
40
            $this->mergeConfigFrom(__DIR__ . "/../config/{$file}", ResponseBuilder::CONF_CONFIG);
41
        }
42
    }
43
44
    /**
45 77
     * Sets up package resources
46
     *
47 77
     * @return void
48
     */
49 77
    public function boot()
50 77
    {
51 77
        $this->loadTranslationsFrom(__DIR__ . '/lang', 'response-builder');
52
53 77
        foreach ($this->config_files as $file) {
54
            $this->publishes([__DIR__ . "/../config/{$file}" => config_path($file)]);
55
        }
56
    }
57
58
    /**
59
     * Merge the given configuration with the existing configuration.
60
     *
61
     * @param string $path
62
     * @param string $key
63
     *
64
     * @return void
65
     */
66
    protected function mergeConfigFrom($path, $key)
67
    {
68 77
        $defaults = require $path;
69
        $config = $this->app['config']->get($key, []);
70 77
71 77
        $merged_config = Util::mergeConfig($defaults, $config);
72 77
73
        if (array_key_exists('converter', $merged_config)) {
74
            Util::sortArrayByPri($merged_config['converter']);
75
        }
76
77
        $this->app['config']->set($key, $merged_config);
78
    }
79
80
}
81