Passed
Push — v2 ( 8ecd15...5716eb )
by Alexander
02:37
created

decorateResponseFactory()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 0
loc 8
ccs 0
cts 4
cp 0
crap 6
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
        $this->app->singleton(TransformerResolverContract::class, function ($app) {
201
            return $app->make(TransformerResolver::class);
202 107
        });
203 107
    }
204
205
    /**
206
     * Register transformer bindings.
207
     *
208
     * @return void
209
     */
210
    protected function registerTransformerBindings()
211
    {
212 107
        $this->app->singleton(TransformerResolverContract::class, function ($app) {
213
            return $app->make(TransformerResolver::class);
214 107
        });
215
216 107
        BaseTransformer::containerResolver(function () {
217
            return $this->app->make(Container::class);
218 107
        });
219 107
    }
220
221
    /**
222
     * Register service bindings.
223
     *
224
     * @return void
225
     */
226
    protected function registerServiceBindings()
227
    {
228 107
        $this->app->singleton(ResponderContract::class, function ($app) {
229
            return $app->make(Responder::class);
230 107
        });
231
232 107
        $this->app->singleton(TransformerContract::class, function ($app) {
233
            return $app->make(Transformer::class);
234 107
        });
235 107
    }
236
237
    /**
238
     * Bootstrap the application events.
239
     *
240
     * @return void
241
     */
242 107
    public function boot()
243
    {
244 107
        if ($this->app instanceof Laravel) {
245 107
            $this->bootLaravelApplication();
246
        } 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...
247
            $this->bootLumenApplication();
248
        }
249
250 107
        $this->mergeConfigFrom(__DIR__ . '/../config/responder.php', 'responder');
251 107
        $this->commands(MakeTransformer::class);
252 107
    }
253
254
    /**
255
     * Bootstrap the Laravel application.
256
     *
257
     * @return void
258
     */
259 107
    protected function bootLaravelApplication()
260
    {
261 107
        if ($this->app->runningInConsole()) {
262 107
            $this->publishes([
263 107
                __DIR__ . '/../config/responder.php' => config_path('responder.php'),
264 107
            ], 'config');
265 107
            $this->publishes([
266 107
                __DIR__ . '/../resources/lang/en/errors.php' => base_path('resources/lang/en/errors.php'),
267 107
            ], 'lang');
268
        }
269 107
    }
270
271
    /**
272
     * Bootstrap the Lumen application.
273
     *
274
     * @return void
275
     */
276
    protected function bootLumenApplication()
277
    {
278
        $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...
279
    }
280
}