Completed
Pull Request — master (#56)
by Alexander
05:10
created

ResponderServiceProvider   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 244
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 82.93%

Importance

Changes 6
Bugs 0 Features 0
Metric Value
dl 0
loc 244
c 6
b 0
f 0
ccs 68
cts 82
cp 0.8293
rs 10
wmc 21
lcom 1
cbo 7

15 Methods

Rating   Name   Duplication   Size   Complexity  
A register() 0 17 3
A registerLaravelBindings() 0 6 1
A registerLumenBindings() 0 6 1
A decorateResponseFactory() 0 8 2
A registerSerializerBindings() 0 10 1
A registerErrorBindings() 0 15 1
A registerFractalBindings() 0 6 1
A registerResourceBindings() 0 10 1
A registerPaginationBindings() 0 6 1
A registerTransformationBindings() 0 14 1
A registerTransformerBindings() 0 10 1
A registerServiceBindings() 0 10 1
A boot() 0 11 3
A bootLaravelApplication() 0 11 2
A bootLumenApplication() 0 4 1
1
<?php
2
3
namespace Flugg\Responder;
4
5
use Flugg\Responder\Console\MakeTransformer;
6
use Flugg\Responder\Contracts\ErrorFactory as ErrorFactoryContract;
7
use Flugg\Responder\Contracts\ErrorMessageResolver as ErrorMessageResolverContract;
8
use Flugg\Responder\Contracts\ErrorSerializer as ErrorSerializerContract;
9
use Flugg\Responder\Contracts\Pagination\PaginatorFactory as PaginatorFactoryContract;
10
use Flugg\Responder\Contracts\Resources\ResourceFactory as ResourceFactoryContract;
11
use Flugg\Responder\Contracts\Resources\ResourceKeyResolver as ResourceKeyResolverContract;
12
use Flugg\Responder\Contracts\Responder as ResponderContract;
13
use Flugg\Responder\Contracts\ResponseFactory as ResponseFactoryContract;
14
use Flugg\Responder\Contracts\Transformer as TransformerContract;
15
use Flugg\Responder\Contracts\Transformers\TransformerResolver as TransformerResolverContract;
16
use Flugg\Responder\Contracts\TransformFactory as TransformFactoryContract;
17
use Flugg\Responder\Http\Responses\ErrorResponseBuilder;
18
use Flugg\Responder\Http\Responses\Factories\LaravelResponseFactory;
19
use Flugg\Responder\Http\Responses\Factories\LumenResponseFactory;
20
use Flugg\Responder\Pagination\PaginatorFactory;
21
use Flugg\Responder\Resources\ResourceFactory;
22
use Flugg\Responder\Resources\ResourceKeyResolver;
23
use Flugg\Responder\Transformers\Transformer as BaseTransformer;
24
use Flugg\Responder\Transformers\TransformerResolver;
25
use Illuminate\Contracts\Container\Container;
26
use Illuminate\Foundation\Application as Laravel;
27
use Illuminate\Http\Request;
28
use Illuminate\Support\ServiceProvider as BaseServiceProvider;
29
use Laravel\Lumen\Application as Lumen;
30
use League\Fractal\Manager;
31
use League\Fractal\Serializer\SerializerAbstract;
32
33
/**
34
 * A service provider class responsible for bootstrapping the parts of the Laravel package.
35
 *
36
 * @package flugger/laravel-responder
37
 * @author  Alexander Tømmerås <[email protected]>
38
 * @license The MIT License
39
 */
40
class ResponderServiceProvider extends BaseServiceProvider
41
{
42
    /**
43
     * Indicates if loading of the provider is deferred.
44
     *
45
     * @var bool
46
     */
47
    protected $defer = false;
48
49
    /**
50
     * Register the service provider.
51
     *
52
     * @return void
53
     */
54 127
    public function register()
55
    {
56 127
        if ($this->app instanceof Laravel) {
57 127
            $this->registerLaravelBindings();
58
        } elseif ($this->app instanceof Lumen) {
0 ignored issues
show
Bug introduced by
The class Laravel\Lumen\Application does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
59
            $this->registerLumenBindings();
60
        }
61
62 127
        $this->registerSerializerBindings();
63 127
        $this->registerErrorBindings();
64 127
        $this->registerFractalBindings();
65 127
        $this->registerResourceBindings();
66 127
        $this->registerPaginationBindings();
67 127
        $this->registerTransformationBindings();
68 127
        $this->registerTransformerBindings();
69 127
        $this->registerServiceBindings();
70 127
    }
71
72
    /**
73
     * Register Laravel bindings.
74
     *
75
     * @return void
76
     */
77
    protected function registerLaravelBindings()
78
    {
79 127
        $this->app->singleton(ResponseFactoryContract::class, function ($app) {
80
            return $this->decorateResponseFactory($app->make(LaravelResponseFactory::class));
81 127
        });
82 127
    }
83
84
    /**
85
     * Register Lumen bindings.
86
     *
87
     * @return void
88
     */
89
    protected function registerLumenBindings()
90
    {
91
        $this->app->singleton(ResponseFactoryContract::class, function ($app) {
92
            return $this->decorateResponseFactory($app->make(LumenResponseFactory::class));
93
        });
94
    }
95
96
    /**
97
     * Decorate response factories.
98
     *
99
     * @param  \Flugg\Responder\Contracts\ResponseFactory $factory
100
     * @return void
101
     */
102
    protected function decorateResponseFactory(ResponseFactoryContract $factory)
103
    {
104
        foreach ($this->app->config['responder.decorators'] as $decorator) {
105
            $factory = new $decorator($factory);
106
        };
107
108
        return $factory;
109
    }
110
111
    /**
112
     * Register serializer bindings.
113
     *
114
     * @return void
115
     */
116
    protected function registerSerializerBindings()
117
    {
118 127
        $this->app->bind(ErrorSerializerContract::class, function ($app) {
119
            return $app->make($app->config['responder.serializers.error']);
120 127
        });
121
122 127
        $this->app->bind(SerializerAbstract::class, function ($app) {
123
            return $app->make($app->config['responder.serializers.success']);
124 127
        });
125 127
    }
126
127
    /**
128
     * Register error bindings.
129
     *
130
     * @return void
131
     */
132
    protected function registerErrorBindings()
133
    {
134 127
        $this->app->singleton(ErrorMessageResolverContract::class, function ($app) {
135
            return $app->make(ErrorMessageResolver::class);
136 127
        });
137
138 127
        $this->app->singleton(ErrorFactoryContract::class, function ($app) {
139
            return $app->make(ErrorFactory::class);
140 127
        });
141
142 127
        $this->app->bind(ErrorResponseBuilder::class, function ($app) {
143
            return (new ErrorResponseBuilder($app->make(ResponseFactoryContract::class), $app->make(ErrorFactoryContract::class)))
144
                ->serializer($app->make(ErrorSerializerContract::class));
145 127
        });
146 127
    }
147
148
    /**
149
     * Register Fractal bindings.
150
     *
151
     * @return void
152
     */
153
    protected function registerFractalBindings()
154
    {
155 127
        $this->app->bind(Manager::class, function ($app) {
156
            return (new Manager)->setRecursionLimit($app->config['responder.recursion_limit']);
157 127
        });
158 127
    }
159
160
    /**
161
     * Register pagination bindings.
162
     *
163
     * @return void
164
     */
165
    protected function registerResourceBindings()
166
    {
167 127
        $this->app->singleton(ResourceKeyResolverContract::class, function ($app) {
168
            return $app->make(ResourceKeyResolver::class);
169 127
        });
170
171 127
        $this->app->singleton(ResourceFactoryContract::class, function ($app) {
172
            return $app->make(ResourceFactory::class);
173 127
        });
174 127
    }
175
176
    /**
177
     * Register pagination bindings.
178
     *
179
     * @return void
180
     */
181
    protected function registerPaginationBindings()
182
    {
183 127
        $this->app->singleton(PaginatorFactoryContract::class, function ($app) {
184
            return new PaginatorFactory($app->make(Request::class)->query());
185 127
        });
186 127
    }
187
188
    /**
189
     * Register transformation bindings.
190
     *
191
     * @return void
192
     */
193
    protected function registerTransformationBindings()
194
    {
195 127
        $this->app->singleton(TransformFactoryContract::class, function ($app) {
196
            return $app->make(FractalTransformFactory::class);
197 127
        });
198
199 127
        $this->app->bind(TransformBuilder::class, function ($app) {
200
            return (new TransformBuilder($app->make(ResourceFactoryContract::class), $app->make(TransformFactoryContract::class), $app->make(PaginatorFactoryContract::class)))
201
                ->serializer($app->make(SerializerAbstract::class))
202
                ->with($app->make(Request::class)->input($app->config['responder.load_relations_parameter'], []))
203
                ->only($app->make(Request::class)->input($app->config['responder.filter_fields_parameter'], []));
204 127
        });
205
206 127
    }
207
208
    /**
209
     * Register transformer bindings.
210
     *
211
     * @return void
212
     */
213
    protected function registerTransformerBindings()
214
    {
215 127
        $this->app->singleton(TransformerResolverContract::class, function ($app) {
216
            return $app->make(TransformerResolver::class);
217 127
        });
218
219 127
        BaseTransformer::containerResolver(function () {
220
            return $this->app->make(Container::class);
221 127
        });
222 127
    }
223
224
    /**
225
     * Register service bindings.
226
     *
227
     * @return void
228
     */
229
    protected function registerServiceBindings()
230
    {
231 127
        $this->app->singleton(ResponderContract::class, function ($app) {
232
            return $app->make(Responder::class);
233 127
        });
234
235 127
        $this->app->singleton(TransformerContract::class, function ($app) {
236
            return $app->make(Transformer::class);
237 127
        });
238 127
    }
239
240
    /**
241
     * Bootstrap the application events.
242
     *
243
     * @return void
244
     */
245 127
    public function boot()
246
    {
247 127
        if ($this->app instanceof Laravel) {
248 127
            $this->bootLaravelApplication();
249
        } elseif ($this->app instanceof Lumen) {
0 ignored issues
show
Bug introduced by
The class Laravel\Lumen\Application does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
250
            $this->bootLumenApplication();
251
        }
252
253 127
        $this->mergeConfigFrom(__DIR__ . '/../config/responder.php', 'responder');
254 127
        $this->commands(MakeTransformer::class);
255 127
    }
256
257
    /**
258
     * Bootstrap the Laravel application.
259
     *
260
     * @return void
261
     */
262 127
    protected function bootLaravelApplication()
263
    {
264 127
        if ($this->app->runningInConsole()) {
265 127
            $this->publishes([
266 127
                __DIR__ . '/../config/responder.php' => config_path('responder.php'),
267 127
            ], 'config');
268 127
            $this->publishes([
269 127
                __DIR__ . '/../resources/lang/en/errors.php' => base_path('resources/lang/en/errors.php'),
270 127
            ], 'lang');
271
        }
272 127
    }
273
274
    /**
275
     * Bootstrap the Lumen application.
276
     *
277
     * @return void
278
     */
279
    protected function bootLumenApplication()
280
    {
281
        $this->app->configure('responder');
0 ignored issues
show
Bug introduced by
The method configure() does not exist on Illuminate\Contracts\Foundation\Application. Did you maybe mean registerConfiguredProviders()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
282
    }
283
}