ServiceProvider::getResponseMacroName()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
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