Completed
Push — dev ( ecb657...68c27d )
by Marcin
02:52 queued 02:49
created

ResponseBuilderServiceProvider::mergeConfigFrom()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2.0438

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 2
eloc 8
c 3
b 0
f 0
nc 2
nop 2
dl 0
loc 15
ccs 7
cts 9
cp 0.7778
crap 2.0438
rs 10
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 RAeturnTypeCanBeDeclaredInspection
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-2020 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
use MarcinOrlowski\ResponseBuilder\Exceptions\IncompleteConfigurationException;
26
27
class ResponseBuilderServiceProvider extends ServiceProvider
28
{
29
	protected $config_files = [
30
		'response_builder.php',
31
	];
32
33
	/**
34
	 * Register bindings in the container.
35
	 *
36
	 * @return void
37
	 */
38 103
	public function register()
39
	{
40 103
		foreach ($this->config_files as $file) {
41 103
			$this->mergeConfigFrom(__DIR__ . "/../config/{$file}", ResponseBuilder::CONF_CONFIG);
42
		}
43 103
	}
44
45
	/**
46
	 * Sets up package resources
47
	 *
48
	 * @return void
49
	 */
50 103
	public function boot()
51
	{
52 103
		$this->loadTranslationsFrom(__DIR__ . '/lang', 'response-builder');
53
54 103
		foreach ($this->config_files as $file) {
55 103
			$this->publishes([__DIR__ . "/../config/{$file}" => config_path($file)]);
56
		}
57 103
	}
58
59
	/**
60
	 * Merge the given configuration with the existing configuration.
61
	 *
62
	 * @param string $path
63
	 * @param string $key
64
	 *
65
	 * @throws \MarcinOrlowski\ResponseBuilder\Exceptions\IncompleteConfigurationException
66
	 *
67
	 * @return void
68
	 */
69 103
	protected function mergeConfigFrom($path, $key)
70
	{
71 103
		$defaults = require $path;
72 103
		$config = $this->app['config']->get($key, []);
73
74 103
		$merged_config = Util::mergeConfig($defaults, $config);
75
76 103
		if (!isset($merged_config['converter']['classes'])) {
77
			throw new IncompleteConfigurationException(
78
				sprintf('Configuration lacks "%s" array.', ResponseBuilder::CONF_KEY_CONVERTER_CLASSES));
79
		}
80
81 103
		Util::sortArrayByPri($merged_config['converter']['classes']);
82
83 103
        $this->app['config']->set($key, $merged_config);
84 103
    }
85
86
}
87