ServiceProvider   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
eloc 12
dl 0
loc 54
ccs 17
cts 17
cp 1
rs 10
c 1
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getResponseMacroName() 0 3 1
A boot() 0 5 1
A register() 0 5 1
A registerResponseMacro() 0 20 1
1
<?php
2
3
namespace Swis\Laravel\JavaScriptData;
4
5
use Illuminate\Support\Facades\Response;
6
use Illuminate\Support\ServiceProvider as BaseServiceProvider;
7
8
class ServiceProvider extends BaseServiceProvider
9
{
10
    /**
11
     * Bootstrap the application services.
12
     */
13 12
    public function boot()
14
    {
15 12
        $this->publishes([__DIR__.'/../config/' => config_path()], 'config');
16
17 12
        $this->registerResponseMacro($this->getResponseMacroName());
18
    }
19
20
    /**
21
     * Register the application services.
22
     */
23 12
    public function register()
24
    {
25 12
        $this->mergeConfigFrom(__DIR__.'/../config/javascript-data-response.php', 'javascript-data-response');
26
27 12
        $this->app->singleton(Builder::class);
28
    }
29
30
    /**
31
     * Response macro for JavaScript data.
32
     *
33
     * @param string $name
34
     */
35 12
    protected function registerResponseMacro(string $name)
36
    {
37 12
        Response::macro(
38 12
            $name,
39
            /**
40
             * Return a new JavaScript data response from the application.
41
             *
42
             * @param string $name
43
             * @param mixed  $data
44
             * @param int    $status
45
             * @param array  $headers
46
             * @param int    $options
47
             *
48
             * @return \Illuminate\Http\Response
49
             */
50 12
            function (string $name, $data = [], int $status = 200, array $headers = [], $options = 0) {
51 4
                $builder = app(Builder::class);
52 4
                $factory = new ResponseFactory(/* @scrutinizer ignore-type */ $this, $builder);
53
54 4
                return $factory->make($name, $data, $status, $headers, $options);
55 12
            }
56 12
        );
57
    }
58
59 12
    protected function getResponseMacroName(): string
60
    {
61 12
        return 'javascriptData';
62
    }
63
}
64