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