Passed
Push — master ( a1071b...9766e2 )
by Alexander
52s
created

ResponderServiceProvider::bootLaravelApplication()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 2

Importance

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