Passed
Push — master ( ac98a1...77c90a )
by Alexander
02:44
created

ResponderServiceProvider   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 243
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 83.72%

Importance

Changes 7
Bugs 1 Features 0
Metric Value
c 7
b 1
f 0
dl 0
loc 243
ccs 72
cts 86
cp 0.8372
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 registerPaginationBindings() 0 6 1
A registerTransformationBindings() 0 13 1
A registerServiceBindings() 0 10 1
A boot() 0 11 3
A bootLaravelApplication() 0 11 2
A bootLumenApplication() 0 4 1
A registerTransformerBindings() 0 10 1
A registerResourceBindings() 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 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 133
    public function register()
55
    {
56 133
        if ($this->app instanceof Laravel) {
57 133
            $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 133
        $this->registerSerializerBindings();
63 133
        $this->registerErrorBindings();
64 133
        $this->registerFractalBindings();
65 133
        $this->registerTransformerBindings();
66 133
        $this->registerResourceBindings();
67 133
        $this->registerPaginationBindings();
68 133
        $this->registerTransformationBindings();
69 133
        $this->registerServiceBindings();
70 133
    }
71
72
    /**
73
     * Register Laravel bindings.
74
     *
75
     * @return void
76
     */
77
    protected function registerLaravelBindings()
78
    {
79 133
        $this->app->singleton(ResponseFactoryContract::class, function ($app) {
80
            return $this->decorateResponseFactory($app->make(LaravelResponseFactory::class));
81 133
        });
82 133
    }
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 133
        $this->app->bind(ErrorSerializerContract::class, function ($app) {
119
            return $app->make($app->config['responder.serializers.error']);
120 133
        });
121
122 133
        $this->app->bind(SerializerAbstract::class, function ($app) {
123
            return $app->make($app->config['responder.serializers.success']);
124 133
        });
125 133
    }
126
127
    /**
128
     * Register error bindings.
129
     *
130
     * @return void
131
     */
132
    protected function registerErrorBindings()
133
    {
134 133
        $this->app->singleton(ErrorMessageResolverContract::class, function ($app) {
135
            return $app->make(ErrorMessageResolver::class);
136 133
        });
137
138 133
        $this->app->singleton(ErrorFactoryContract::class, function ($app) {
139
            return $app->make(ErrorFactory::class);
140 133
        });
141
142 133
        $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 133
        });
146 133
    }
147
148
    /**
149
     * Register Fractal bindings.
150
     *
151
     * @return void
152
     */
153
    protected function registerFractalBindings()
154
    {
155 133
        $this->app->bind(Manager::class, function ($app) {
156
            return (new Manager)->setRecursionLimit($app->config['responder.recursion_limit']);
157 133
        });
158 133
    }
159
160
    /**
161
     * Register transformer bindings.
162
     *
163
     * @return void
164
     */
165
    protected function registerTransformerBindings()
166
    {
167 133
        $this->app->singleton(TransformerResolverContract::class, function ($app) {
168 2
            return $app->make(TransformerResolver::class);
169 133
        });
170
171 133
        BaseTransformer::containerResolver(function () {
172 2
            return $this->app->make(Container::class);
173 133
        });
174 133
    }
175
176
    /**
177
     * Register pagination bindings.
178
     *
179
     * @return void
180
     */
181
    protected function registerResourceBindings()
182
    {
183 133
        $this->app->singleton(ResourceKeyResolverContract::class, function ($app) {
184 2
            return $app->make(ResourceKeyResolver::class);
185 133
        });
186
187 133
        $this->app->singleton(ResourceFactoryContract::class, function ($app) {
188 2
            return $app->make(ResourceFactory::class);
189 133
        });
190 133
    }
191
192
    /**
193
     * Register pagination bindings.
194
     *
195
     * @return void
196
     */
197
    protected function registerPaginationBindings()
198
    {
199 133
        $this->app->singleton(PaginatorFactoryContract::class, function ($app) {
200
            return new PaginatorFactory($app->make(Request::class)->query());
201 133
        });
202 133
    }
203
204
    /**
205
     * Register transformation bindings.
206
     *
207
     * @return void
208
     */
209
    protected function registerTransformationBindings()
210
    {
211 133
        $this->app->singleton(TransformFactoryContract::class, function ($app) {
212
            return $app->make(FractalTransformFactory::class);
213 133
        });
214
215 133
        $this->app->bind(TransformBuilder::class, function ($app) {
216
            return (new TransformBuilder($app->make(ResourceFactoryContract::class), $app->make(TransformFactoryContract::class), $app->make(PaginatorFactoryContract::class)))
217
                ->serializer($app->make(SerializerAbstract::class))
218
                ->with($app->make(Request::class)->input($app->config['responder.load_relations_parameter'], []))
219
                ->only($app->make(Request::class)->input($app->config['responder.filter_fields_parameter'], []));
220 133
        });
221 133
    }
222
223
    /**
224
     * Register service bindings.
225
     *
226
     * @return void
227
     */
228
    protected function registerServiceBindings()
229
    {
230 133
        $this->app->singleton(ResponderContract::class, function ($app) {
231
            return $app->make(Responder::class);
232 133
        });
233
234 133
        $this->app->singleton(TransformerContract::class, function ($app) {
235
            return $app->make(Transformer::class);
236 133
        });
237 133
    }
238
239
    /**
240
     * Bootstrap the application events.
241
     *
242
     * @return void
243
     */
244 133
    public function boot()
245
    {
246 133
        if ($this->app instanceof Laravel) {
247 133
            $this->bootLaravelApplication();
248
        } 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...
249
            $this->bootLumenApplication();
250
        }
251
252 133
        $this->mergeConfigFrom(__DIR__ . '/../config/responder.php', 'responder');
253 133
        $this->commands(MakeTransformer::class);
254 133
    }
255
256
    /**
257
     * Bootstrap the Laravel application.
258
     *
259
     * @return void
260
     */
261 133
    protected function bootLaravelApplication()
262
    {
263 133
        if ($this->app->runningInConsole()) {
264 133
            $this->publishes([
265 133
                __DIR__ . '/../config/responder.php' => config_path('responder.php'),
266 133
            ], 'config');
267 133
            $this->publishes([
268 133
                __DIR__ . '/../resources/lang/en/errors.php' => base_path('resources/lang/en/errors.php'),
269 133
            ], 'lang');
270
        }
271 133
    }
272
273
    /**
274
     * Bootstrap the Lumen application.
275
     *
276
     * @return void
277
     */
278
    protected function bootLumenApplication()
279
    {
280
        $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...
281
    }
282
}