Passed
Push — master ( e122d6...753728 )
by Alexander
02:47
created

ResponderServiceProvider   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 249
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Test Coverage

Coverage 88.46%

Importance

Changes 9
Bugs 1 Features 0
Metric Value
dl 0
loc 249
ccs 92
cts 104
cp 0.8846
rs 10
c 9
b 1
f 0
wmc 22
lcom 1
cbo 8

15 Methods

Rating   Name   Duplication   Size   Complexity  
A boot() 0 11 3
A bootLaravelApplication() 0 11 2
A bootLumenApplication() 0 4 1
A register() 0 17 3
A registerLaravelBindings() 0 6 1
A registerLumenBindings() 0 10 1
A decorateResponseFactory() 0 8 2
A registerSerializerBindings() 0 10 1
A registerErrorBindings() 0 14 1
A registerFractalBindings() 0 6 1
A registerTransformerBindings() 0 10 1
A registerResourceBindings() 0 10 1
A registerPaginationBindings() 0 6 1
A registerTransformationBindings() 0 16 2
A registerServiceBindings() 0 10 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;
14
use Flugg\Responder\Contracts\ResponseFactory as ResponseFactoryContract;
15
use Flugg\Responder\Contracts\SimpleTransformer as SimpleTransformerContract;
16
use Flugg\Responder\Contracts\Transformers\TransformerResolver as TransformerResolverContract;
17
use Flugg\Responder\Contracts\TransformFactory as TransformFactoryContract;
18
use Flugg\Responder\Http\Responses\ErrorResponseBuilder;
19
use Flugg\Responder\Http\Responses\Factories\LaravelResponseFactory;
20
use Flugg\Responder\Http\Responses\Factories\LumenResponseFactory;
21
use Flugg\Responder\Pagination\PaginatorFactory;
22
use Flugg\Responder\Resources\ResourceFactory;
23
use Flugg\Responder\Resources\ResourceKeyResolver;
24
use Flugg\Responder\Transformers\Transformer as BaseTransformer;
25
use Flugg\Responder\Transformers\TransformerResolver;
26
use Illuminate\Contracts\Container\Container;
27
use Illuminate\Foundation\Application as Laravel;
28
use Illuminate\Http\Request;
29
use Illuminate\Support\ServiceProvider as BaseServiceProvider;
30
use Illuminate\Translation\Translator;
31
use Laravel\Lumen\Application as Lumen;
32
use League\Fractal\Manager;
33
use League\Fractal\Serializer\SerializerAbstract;
34
35
/**
36
 * A service provider class responsible for bootstrapping the parts of the Laravel package.
37
 *
38
 * @package flugger/laravel-responder
39
 * @author  Alexander Tømmerås <[email protected]>
40
 * @license The MIT License
41
 */
42
class ResponderServiceProvider extends BaseServiceProvider
43
{
44
    /**
45
     * Indicates if loading of the provider is deferred.
46
     *
47
     * @var bool
48
     */
49
    protected $defer = false;
50
51
    /**
52
     * Register the service provider.
53
     *
54
     * @return void
55
     */
56 183
    public function register()
57
    {
58 183
        if ($this->app instanceof Laravel) {
59 183
            $this->registerLaravelBindings();
60
        } 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...
61
            $this->registerLumenBindings();
62
        }
63
64 183
        $this->registerSerializerBindings();
65 183
        $this->registerErrorBindings();
66 183
        $this->registerFractalBindings();
67 183
        $this->registerTransformerBindings();
68 183
        $this->registerResourceBindings();
69 183
        $this->registerPaginationBindings();
70 183
        $this->registerTransformationBindings();
71 183
        $this->registerServiceBindings();
72 183
    }
73
74
    /**
75
     * Register Laravel bindings.
76
     *
77
     * @return void
78
     */
79
    protected function registerLaravelBindings()
80
    {
81 183
        $this->app->singleton(ResponseFactoryContract::class, function ($app) {
82 47
            return $this->decorateResponseFactory($app->make(LaravelResponseFactory::class));
83 183
        });
84 183
    }
85
86
    /**
87
     * Register Lumen bindings.
88
     *
89
     * @return void
90
     */
91
    protected function registerLumenBindings()
92
    {
93
        $this->app->singleton(ResponseFactoryContract::class, function ($app) {
94
            return $this->decorateResponseFactory($app->make(LumenResponseFactory::class));
95
        });
96
97
        $this->app->bind(Translator::class, function ($app) {
98
            return $app['translator'];
99
        });
100
    }
101
102
    /**
103
     * Decorate response factories.
104
     *
105
     * @param  \Flugg\Responder\Contracts\ResponseFactory $factory
106
     * @return \Flugg\Responder\Contracts\ResponseFactory
107
     */
108 47
    protected function decorateResponseFactory(ResponseFactoryContract $factory): ResponseFactory
109
    {
110 47
        foreach ($this->app->config['responder.decorators'] as $decorator) {
111 47
            $factory = new $decorator($factory);
112
        };
113
114 47
        return $factory;
115
    }
116
117
    /**
118
     * Register serializer bindings.
119
     *
120
     * @return void
121
     */
122
    protected function registerSerializerBindings()
123
    {
124 183
        $this->app->bind(ErrorSerializerContract::class, function ($app) {
125 47
            return $app->make($app->config['responder.serializers.error']);
126 183
        });
127
128 183
        $this->app->bind(SerializerAbstract::class, function ($app) {
129 47
            return $app->make($app->config['responder.serializers.success']);
130 183
        });
131 183
    }
132
133
    /**
134
     * Register error bindings.
135
     *
136
     * @return void
137
     */
138
    protected function registerErrorBindings()
139
    {
140 183
        $this->app->singleton(ErrorMessageResolverContract::class, function ($app) {
141 47
            return $app->make(ErrorMessageResolver::class);
142 183
        });
143
144 183
        $this->app->singleton(ErrorFactoryContract::class, function ($app) {
145 47
            return $app->make(ErrorFactory::class);
146 183
        });
147
148 183
        $this->app->bind(ErrorResponseBuilder::class, function ($app) {
149 47
            return (new ErrorResponseBuilder($app->make(ResponseFactoryContract::class), $app->make(ErrorFactoryContract::class)))->serializer($app->make(ErrorSerializerContract::class));
150 183
        });
151 183
    }
152
153
    /**
154
     * Register Fractal bindings.
155
     *
156
     * @return void
157
     */
158
    protected function registerFractalBindings()
159
    {
160 183
        $this->app->bind(Manager::class, function ($app) {
161 47
            return (new Manager)->setRecursionLimit($app->config['responder.recursion_limit']);
162 183
        });
163 183
    }
164
165
    /**
166
     * Register transformer bindings.
167
     *
168
     * @return void
169
     */
170
    protected function registerTransformerBindings()
171
    {
172 183
        $this->app->singleton(TransformerResolverContract::class, function ($app) {
173 49
            return new TransformerResolver($app, $app->config['responder.fallback_transformer']);
174 183
        });
175
176 183
        BaseTransformer::containerResolver(function () {
177 14
            return $this->app->make(Container::class);
178 183
        });
179 183
    }
180
181
    /**
182
     * Register pagination bindings.
183
     *
184
     * @return void
185
     */
186
    protected function registerResourceBindings()
187
    {
188 183
        $this->app->singleton(ResourceKeyResolverContract::class, function ($app) {
189 49
            return $app->make(ResourceKeyResolver::class);
190 183
        });
191
192 183
        $this->app->singleton(ResourceFactoryContract::class, function ($app) {
193 49
            return $app->make(ResourceFactory::class);
194 183
        });
195 183
    }
196
197
    /**
198
     * Register pagination bindings.
199
     *
200
     * @return void
201
     */
202
    protected function registerPaginationBindings()
203
    {
204 183
        $this->app->bind(PaginatorFactoryContract::class, function ($app) {
205 47
            return new PaginatorFactory($app->make(Request::class)->query());
206 183
        });
207 183
    }
208
209
    /**
210
     * Register transformation bindings.
211
     *
212
     * @return void
213
     */
214
    protected function registerTransformationBindings()
215
    {
216 183
        $this->app->bind(TransformFactoryContract::class, function ($app) {
217 47
            return $app->make(FractalTransformFactory::class);
218 183
        });
219
220 183
        $this->app->bind(TransformBuilder::class, function ($app) {
221 47
            $request = $this->app->make(Request::class);
222 47
            $relations = $request->input($this->app->config['responder.load_relations_parameter'], []);
223 47
            $fieldsets = $request->input($app->config['responder.filter_fields_parameter'], []);
224
225 47
            return (new TransformBuilder($app->make(ResourceFactoryContract::class), $app->make(TransformFactoryContract::class), $app->make(PaginatorFactoryContract::class)))->serializer($app->make(SerializerAbstract::class))
226 47
                ->with(is_string($relations) ? explode(',', $relations) : $relations)
227 47
                ->only($fieldsets);
228 183
        });
229 183
    }
230
231
    /**
232
     * Register service bindings.
233
     *
234
     * @return void
235
     */
236
    protected function registerServiceBindings()
237
    {
238 183
        $this->app->bind(ResponderContract::class, function ($app) {
239 46
            return $app->make(Responder::class);
240 183
        });
241
242 183
        $this->app->bind(SimpleTransformerContract::class, function ($app) {
243
            return $app->make(SimpleTransformer::class);
244 183
        });
245 183
    }
246
247
    /**
248
     * Bootstrap the application events.
249
     *
250
     * @return void
251
     */
252 183
    public function boot()
253
    {
254 183
        if ($this->app instanceof Laravel) {
255 183
            $this->bootLaravelApplication();
256
        } 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...
257
            $this->bootLumenApplication();
258
        }
259
260 183
        $this->mergeConfigFrom(__DIR__ . '/../config/responder.php', 'responder');
261 183
        $this->commands(MakeTransformer::class);
262 183
    }
263
264
    /**
265
     * Bootstrap the Laravel application.
266
     *
267
     * @return void
268
     */
269 183
    protected function bootLaravelApplication()
270
    {
271 183
        if ($this->app->runningInConsole()) {
272 183
            $this->publishes([
273 183
                __DIR__ . '/../config/responder.php' => config_path('responder.php'),
274 183
            ], 'config');
275 183
            $this->publishes([
276 183
                __DIR__ . '/../resources/lang/en/errors.php' => base_path('resources/lang/en/errors.php'),
277 183
            ], 'lang');
278
        }
279 183
    }
280
281
    /**
282
     * Bootstrap the Lumen application.
283
     *
284
     * @return void
285
     */
286
    protected function bootLumenApplication()
287
    {
288
        $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...
289
    }
290
}