LaravelServiceProvider   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 129
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 15

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 15
dl 0
loc 129
rs 9.1666
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A prepareSerializer() 0 4 1
A registerProviders() 0 8 1
A registerClasses() 0 56 1
A registerCommands() 0 6 1
A boot() 0 13 2
A register() 0 6 1
1
<?php
2
3
namespace Realshadow\RequestDeserializer\Providers;
4
5
use Doctrine\Common\Annotations\AnnotationReader;
6
use Doctrine\Common\Annotations\AnnotationRegistry;
7
use Illuminate\Foundation\Application;
8
use Illuminate\Support\ServiceProvider;
9
use JMS\Serializer\EventDispatcher\EventDispatcher;
10
use JMS\Serializer\SerializerBuilder;
11
use JsonSchema\Constraints\Constraint;
12
use JsonSchema\Constraints\Factory;
13
use JsonSchema\SchemaStorage;
14
use JsonSchema\Uri\UriRetriever;
15
use JsonSchema\Validator;
16
use Realshadow\RequestDeserializer\Console\Commands\Schema\ConvertCommand;
17
use Realshadow\RequestDeserializer\JsonSchema\Uri\Retrievers\FileGetContents;
18
use Realshadow\RequestDeserializer\Http\Request\RequestDeserializationHandler;
19
use Realshadow\RequestDeserializer\Http\Request\RequestHandler;
20
use Realshadow\RequestDeserializer\Serializers\LaravelSerializer;
21
22
23
/**
24
 * Service provider for API wrapper responsible for loading all neccessary components
25
 *
26
 * @package Realshadow\RequestDeserializer\Providers
27
 * @author Lukáš Homza <[email protected]>
28
 */
29
class LaravelServiceProvider extends ServiceProvider
30
{
31
32
    const CONFIG_KEY = 'request_deserializer';
33
34
    /**
35
     * Initializes serializer with required laravel bindings
36
     *
37
     * @return SerializerBuilder
38
     */
39
    protected function prepareSerializer()
40
    {
41
        return (new LaravelSerializer())->getBuilderInstance();
42
    }
43
44
    /**
45
     * Register third party providers
46
     */
47
    protected function registerProviders()
48
    {
49
        //region Purifier
50
        $this->app->register(\Mews\Purifier\PurifierServiceProvider::class);
51
52
        $this->app->bind('purifier', \Realshadow\RequestDeserializer\Validation\Purifier::class);
53
        //endregion
54
    }
55
56
    /**
57
     * Registers/binds all third party classes to service container and/or autoloader
58
     *
59
     * @throws \InvalidArgumentException
60
     */
61
    protected function registerClasses()
62
    {
63
        //region Annotations
64
        AnnotationRegistry::registerLoader('class_exists');
65
66
        $this->app->singleton('app.annotation_reader', function (Application $app) {
0 ignored issues
show
Unused Code introduced by
The parameter $app is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
67
            return new AnnotationReader;
68
        });
69
        //endregion
70
71
        //region Serializer
72
        $this->app->singleton('api.serializer', function (Application $app) {
0 ignored issues
show
Unused Code introduced by
The parameter $app is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
73
            return $this->prepareSerializer()->build();
74
        });
75
        //endregion
76
77
        //region JSON Schema Constraint
78
        $this->app->singleton('api.schema.constraint', function (Application $app) {
0 ignored issues
show
Unused Code introduced by
The parameter $app is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
79
            $fileRetriever = new FileGetContents;
80
            $fileRetriever->setBasePath(config(self::CONFIG_KEY . '.request.schema_path') . DIRECTORY_SEPARATOR);
81
82
            $retriever = (new UriRetriever)
83
                ->setUriRetriever($fileRetriever);
84
85
            $constraintFactory = new Factory(new SchemaStorage($retriever));
86
            $constraintFactory->setConfig(Constraint::CHECK_MODE_APPLY_DEFAULTS | Constraint::CHECK_MODE_COERCE_TYPES);
87
88
            return $constraintFactory;
89
        });
90
        //endregion
91
92
        //region JSON Schema Validator
93
        $this->app->singleton('api.schema.validator', function (Application $app) {
94
            return new Validator(
95
                $app->make('api.schema.constraint')
96
            );
97
        });
98
        //endregion
99
100
        //region RequestHandler
101
        $this->app->singleton(RequestHandler::class, function (Application $app) {
102
            $serializer = $this->prepareSerializer()
103
                ->configureListeners(function (EventDispatcher $dispatcher) use ($app) {
104
                    $dispatcher->addSubscriber(
105
                        new RequestDeserializationHandler(
106
                            $app->make('api.schema.validator'),
107
                            $app->make('purifier')
108
                        )
109
                    );
110
                })
111
                ->build();
112
113
            return new RequestHandler($serializer);
114
        });
115
        //endregion
116
    }
117
118
    /**
119
     * Registers all provided console commands
120
     */
121
    protected function registerCommands()
122
    {
123
        $this->commands([
124
            ConvertCommand::class,
125
        ]);
126
    }
127
128
    /**
129
     * Perform post-registration booting of services
130
     */
131
    public function boot()
132
    {
133
        if ($this->app->runningInConsole()) {
134
            $this->registerCommands();
135
        }
136
137
        $this->publishes([__DIR__ . '/../../config/purifier.php' => config_path('purifier.php')]);
138
139
        $this->mergeConfigFrom(
140
            __DIR__ . '/../../config/request_deserializer.php',
141
            'request_deserializer'
142
        );
143
    }
144
145
    /**
146
     * Register every component
147
     *
148
     * @throws \InvalidArgumentException
149
     */
150
    public function register()
151
    {
152
        $this->registerProviders();
153
154
        $this->registerClasses();
155
    }
156
157
}
158