Passed
Push — master ( d69960...03caf8 )
by Alexander
55s
created

ResponderServiceProvider::boot()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3.1406

Importance

Changes 0
Metric Value
cc 3
eloc 7
nc 3
nop 0
dl 0
loc 11
ccs 6
cts 8
cp 0.75
crap 3.1406
rs 9.4285
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 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->registerTransformerBindings();
66 127
        $this->registerResourceBindings();
67 127
        $this->registerPaginationBindings();
68 127
        $this->registerTransformationBindings();
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 transformer bindings.
162
     *
163
     * @return void
164
     */
165
    protected function registerTransformerBindings()
166
    {
167 127
        $this->app->singleton(TransformerResolverContract::class, function ($app) {
168
            return $app->make(TransformerResolver::class);
169 127
        });
170
171 127
        BaseTransformer::containerResolver(function () {
172
            return $this->app->make(Container::class);
173 127
        });
174 127
    }
175
176
    /**
177
     * Register pagination bindings.
178
     *
179
     * @return void
180
     */
181
    protected function registerResourceBindings()
182
    {
183 127
        $this->app->singleton(ResourceKeyResolverContract::class, function ($app) {
184
            return $app->make(ResourceKeyResolver::class);
185 127
        });
186
187 127
        $this->app->singleton(ResourceFactoryContract::class, function ($app) {
188
            return $app->make(ResourceFactory::class);
189 127
        });
190 127
    }
191
192
    /**
193
     * Register pagination bindings.
194
     *
195
     * @return void
196
     */
197
    protected function registerPaginationBindings()
198
    {
199 127
        $this->app->singleton(PaginatorFactoryContract::class, function ($app) {
200
            return new PaginatorFactory($app->make(Request::class)->query());
201 127
        });
202 127
    }
203
204
    /**
205
     * Register transformation bindings.
206
     *
207
     * @return void
208
     */
209
    protected function registerTransformationBindings()
210
    {
211 127
        $this->app->singleton(TransformFactoryContract::class, function ($app) {
212
            return $app->make(FractalTransformFactory::class);
213 127
        });
214
215 127
        $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 127
        });
221 127
    }
222
223
    /**
224
     * Register service bindings.
225
     *
226
     * @return void
227
     */
228
    protected function registerServiceBindings()
229
    {
230 127
        $this->app->singleton(ResponderContract::class, function ($app) {
231
            return $app->make(Responder::class);
232 127
        });
233
234 127
        $this->app->singleton(TransformerContract::class, function ($app) {
235
            return $app->make(Transformer::class);
236 127
        });
237 127
    }
238
239
    /**
240
     * Bootstrap the application events.
241
     *
242
     * @return void
243
     */
244 127
    public function boot()
245
    {
246 127
        if ($this->app instanceof Laravel) {
247 127
            $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 127
        $this->mergeConfigFrom(__DIR__ . '/../config/responder.php', 'responder');
253 127
        $this->commands(MakeTransformer::class);
254 127
    }
255
256
    /**
257
     * Bootstrap the Laravel application.
258
     *
259
     * @return void
260
     */
261 127
    protected function bootLaravelApplication()
262
    {
263 127
        if ($this->app->runningInConsole()) {
264 127
            $this->publishes([
265 127
                __DIR__ . '/../config/responder.php' => config_path('responder.php'),
266 127
            ], 'config');
267 127
            $this->publishes([
268 127
                __DIR__ . '/../resources/lang/en/errors.php' => base_path('resources/lang/en/errors.php'),
269 127
            ], 'lang');
270
        }
271 127
    }
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
}