Passed
Pull Request — dev (#118)
by Marcin
02:26
created

ResponseBuilderServiceProvider   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Test Coverage

Coverage 78.95%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 15
c 3
b 0
f 0
dl 0
loc 53
ccs 15
cts 19
cp 0.7895
rs 10
wmc 3

3 Methods

Rating   Name   Duplication   Size   Complexity  
A boot() 0 7 1
A register() 0 4 1
A mergeConfigFrom() 0 16 1
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
    /**
29
     * Register bindings in the container.
30
     *
31
     * @return void
32
     */
33
    public function register()
34
    {
35
        $this->mergeConfigFrom(
36
            __DIR__ . '/../config/response_builder.php', 'response_builder'
37
        );
38
    }
39
40
    /**
41
     * Sets up package resources
42
     *
43
     * @return void
44
     */
45 82
    public function boot()
46
    {
47 82
        $this->loadTranslationsFrom(__DIR__ . '/lang', 'response-builder');
48
49 82
        $source = __DIR__ . '/../config/response_builder.php';
50 82
        $this->publishes([
51 82
            $source => config_path('response_builder.php'),
52
        ]);
53 82
    }
54
55
    /**
56
     * Merge the given configuration with the existing configuration.
57
     *
58
     * @param string $path
59
     * @param string $key
60
     *
61
     * @return void
62
     */
63 82
    protected function mergeConfigFrom($path, $key)
64
    {
65 82
        $defaults = require $path;
66 82
        $config = $this->app['config']->get($key, []);
67
68 82
        $merged_config = Util::mergeConfig($defaults, $config);
69
70
        // we now need to sort 'classes' node by priority
71
        uasort($merged_config['classes'], function($array_a, $array_b) {
72 82
            $pri_a = $array_a['pri'] ?? 0;
73 82
            $pri_b = $array_b['pri'] ?? 0;
74
75 82
            return $pri_b <=> $pri_a;
76 82
        });
77
78 82
        $this->app['config']->set($key, $merged_config);
79 82
    }
80
81
}
82