ResponseBuilderServiceProvider   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Test Coverage

Coverage 76.47%

Importance

Changes 6
Bugs 0 Features 0
Metric Value
eloc 16
c 6
b 0
f 0
dl 0
loc 57
ccs 13
cts 17
cp 0.7647
rs 10
wmc 6

3 Methods

Rating   Name   Duplication   Size   Complexity  
A mergeConfigFrom() 0 15 2
A boot() 0 6 2
A register() 0 4 2
1
<?php
2
declare(strict_types=1);
3
4
namespace MarcinOrlowski\ResponseBuilder;
5
6
/**
7
 * Laravel API Response Builder
8
 *
9
 * @package   MarcinOrlowski\ResponseBuilder
10
 *
11
 * @author    Marcin Orlowski <mail (#) marcinOrlowski (.) com>
12
 * @copyright 2016-2021 Marcin Orlowski
13
 * @license   http://www.opensource.org/licenses/mit-license.php MIT
14
 * @link      https://github.com/MarcinOrlowski/laravel-api-response-builder
15
 */
16
17
/**
18
 * Disable return type hint inspection as we do not have it specified in that
19
 * class for a purpose. The base class is also not having return type hints.
20
 *
21
 * @noinspection RAeturnTypeCanBeDeclaredInspection
22
 */
23
24
use Illuminate\Support\ServiceProvider;
25
use MarcinOrlowski\ResponseBuilder\Exceptions\IncompleteConfigurationException;
26
27
/**
28
 * Laravel's service provider for ResponseBuilder
29
 *
30
 * @package MarcinOrlowski\ResponseBuilder
31
 */
32
class ResponseBuilderServiceProvider extends ServiceProvider
33
{
34
	protected $config_files = [
35
		'response_builder.php',
36
	];
37
38
	/**
39
	 * Register bindings in the container.
40
	 *
41
	 * @return void
42
	 */
43
	public function register()
44
	{
45
		foreach ($this->config_files as $file) {
46
			$this->mergeConfigFrom(__DIR__ . "/../config/{$file}", ResponseBuilder::CONF_CONFIG);
47
		}
48
	}
49 87
50
	/**
51 87
	 * Sets up package resources
52
	 *
53 87
	 * @return void
54 87
	 */
55
	public function boot()
56 87
	{
57
		$this->loadTranslationsFrom(__DIR__ . '/lang', 'response-builder');
58
59
		foreach ($this->config_files as $file) {
60
			$this->publishes([__DIR__ . "/../config/{$file}" => config_path($file)]);
61
		}
62
	}
63
64
	/**
65
	 * Merge the given configuration with the existing configuration.
66 87
	 *
67
	 * @param string $path
68 87
	 * @param string $key
69 87
	 *
70
	 * @throws \MarcinOrlowski\ResponseBuilder\Exceptions\IncompleteConfigurationException
71 87
	 *
72
	 * @return void
73 87
	 */
74 87
	protected function mergeConfigFrom($path, $key)
75
	{
76
		$defaults = require $path;
77 87
		$config = $this->app['config']->get($key, []);
78 87
79
		$merged_config = Util::mergeConfig($defaults, $config);
80
81
		if (!isset($merged_config['converter']['classes'])) {
82
			throw new IncompleteConfigurationException(
83
				sprintf('Configuration lacks "%s" array.', ResponseBuilder::CONF_KEY_CONVERTER_CLASSES));
84
		}
85
86
		Util::sortArrayByPri($merged_config['converter']['classes']);
87
88
        $this->app['config']->set($key, $merged_config);
89
    }
90
91
}
92