Passed
Push — master ( 034e39...aabddd )
by Andrea Marco
04:28 queued 11s
created

LaravelDtoServiceProvider::boot()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 5
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 11
ccs 6
cts 6
cp 1
crap 2
rs 10
1
<?php
2
3
namespace Cerbero\LaravelDto\Providers;
4
5
use Cerbero\Dto\Manipulators\ArrayConverter;
6
use Cerbero\LaravelDto\Manipulators\Listener;
7
use Cerbero\LaravelDto\Console\Commands\MakeDtoCommand;
8
use Cerbero\LaravelDto\Console\Manifest;
9
use Cerbero\LaravelDto\Console\DtoQualifierContract;
10
use Cerbero\LaravelDto\Dto;
11
use Illuminate\Contracts\Foundation\Application;
12
use Illuminate\Support\ServiceProvider;
13
14
use const Cerbero\Dto\IGNORE_UNKNOWN_PROPERTIES;
0 ignored issues
show
Bug introduced by
The constant Cerbero\Dto\IGNORE_UNKNOWN_PROPERTIES was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
15
16
/**
17
 * The package service provider.
18
 *
19
 */
20
class LaravelDtoServiceProvider extends ServiceProvider
21
{
22
    /**
23
     * The package configuration path.
24
     *
25
     * @var string
26
     */
27
    protected const CONFIG = __DIR__ . '/../../config/dto.php';
28
29
    /**
30
     * Bootstrap the application services.
31
     *
32
     * @return void
33
     */
34 39
    public function boot()
35
    {
36 39
        if ($this->app->runningInConsole()) {
37 39
            $this->commands(MakeDtoCommand::class);
38
        }
39
40 39
        $this->publishes([static::CONFIG => $this->app->configPath('dto.php')], 'cerbero-dto');
41
42 39
        ArrayConverter::instance()->setConversions($this->config('conversions'));
43
44 39
        Listener::instance()->listen($this->config('listeners'));
45 39
    }
46
47
    /**
48
     * Retrieve the given configuration value
49
     *
50
     * @param string $key
51
     * @return mixed
52
     */
53 39
    protected function config(string $key)
54
    {
55 39
        return $this->app['config']["dto.{$key}"];
56
    }
57
58
    /**
59
     * Register any application services.
60
     *
61
     * @return void
62
     */
63 39
    public function register()
64
    {
65 39
        $this->mergeConfigFrom(static::CONFIG, 'dto');
66
67 39
        $this->app->bind(DtoQualifierContract::class, $this->config('qualifier'));
68
69
        $this->app->singleton(Manifest::class, function (Application $app) {
70 24
            return new Manifest($app->storagePath() . '/cerbero_laravel_dto.php');
71 39
        });
72
73
        $this->app->resolving(Dto::class, function (Dto $dto, Application $app) {
74
            return $dto->mutate(function (Dto $dto) use ($app) {
75 3
                $dto->merge($app->make('request')->all(), IGNORE_UNKNOWN_PROPERTIES);
76 3
            });
77 39
        });
78 39
    }
79
}
80