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

ResponderServiceProvider::bootLumenApplication()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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