Passed
Push — master ( aabddd...812bf9 )
by Andrea Marco
05:43 queued 10s
created

LaravelDtoServiceProvider::boot()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3.0123

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 3
eloc 8
c 2
b 0
f 0
nc 4
nop 0
dl 0
loc 15
ccs 8
cts 9
cp 0.8889
crap 3.0123
rs 10
1
<?php
2
3
namespace Cerbero\LaravelDto\Providers;
4
5
use Cerbero\Dto\Dto;
6
use Cerbero\Dto\Manipulators\ArrayConverter;
7
use Cerbero\LaravelDto\Manipulators\Listener;
8
use Cerbero\LaravelDto\Console\Commands\MakeDtoCommand;
9
use Cerbero\LaravelDto\Console\Manifest;
10
use Cerbero\LaravelDto\Console\DtoQualifierContract;
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 42
    public function boot()
35
    {
36 42
        if ($this->app->runningInConsole()) {
37 42
            $this->commands(MakeDtoCommand::class);
38
        }
39
40 42
        $this->publishes([static::CONFIG => $this->app->configPath('dto.php')], 'dto');
41
42 42
        ArrayConverter::instance()->setConversions($this->config('conversions'));
43
44 42
        Listener::instance()->listen($this->config('listeners'));
45
46 42
        if (class_exists($cloner = '\Symfony\Component\VarDumper\Cloner\VarCloner')) {
47 42
            $cloner::$defaultCasters[Dto::class] = function (Dto $dto) {
0 ignored issues
show
Bug introduced by
The property defaultCasters does not exist on string.
Loading history...
48
                return $dto->toArray();
49
            };
50
        }
51 42
    }
52
53
    /**
54
     * Retrieve the given configuration value
55
     *
56
     * @param string $key
57
     * @return mixed
58
     */
59 42
    protected function config(string $key)
60
    {
61 42
        return $this->app['config']["dto.{$key}"];
62
    }
63
64
    /**
65
     * Register any application services.
66
     *
67
     * @return void
68
     */
69 42
    public function register()
70
    {
71 42
        $this->mergeConfigFrom(static::CONFIG, 'dto');
72
73 42
        $this->app->bind(DtoQualifierContract::class, $this->config('qualifier'));
74
75 42
        $this->app->singleton(Manifest::class, function (Application $app) {
76 24
            return new Manifest($app->storagePath() . '/cerbero_laravel_dto.php');
77 42
        });
78
79 42
        $this->app->resolving(Dto::class, function (Dto $dto, Application $app) {
80 3
            return $dto->mutate(function (Dto $dto) use ($app) {
81 3
                $dto->merge($app->make('request')->all(), IGNORE_UNKNOWN_PROPERTIES);
82 3
            });
83 42
        });
84 42
    }
85
}
86