Passed
Push — v2 ( b247ab...8c624d )
by Alexander
02:21
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\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\Factories\LaravelResponseFactory;
17
use Flugg\Responder\Http\Responses\Factories\LumenResponseFactory;
18
use Flugg\Responder\Pagination\PaginatorFactory;
19
use Flugg\Responder\Resources\ResourceFactory;
20
use Flugg\Responder\Transformers\Transformer;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Flugg\Responder\Transformer.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
21
use Flugg\Responder\Transformers\TransformerResolver;
22
use Illuminate\Contracts\Container\Container;
23
use Illuminate\Foundation\Application as Laravel;
24
use Illuminate\Http\Request;
25
use Illuminate\Support\ServiceProvider as BaseServiceProvider;
26
use Laravel\Lumen\Application as Lumen;
27
use League\Fractal\Manager;
28
use League\Fractal\Serializer\SerializerAbstract;
29
30
/**
31
 * A service provider class responsible for bootstrapping the parts of the Laravel package.
32
 *
33
 * @package flugger/laravel-responder
34
 * @author  Alexander Tømmerås <[email protected]>
35
 * @license The MIT License
36
 */
37
class ResponderServiceProvider extends BaseServiceProvider
38
{
39
    /**
40
     * Indicates if loading of the provider is deferred.
41
     *
42
     * @var bool
43
     */
44
    protected $defer = false;
45
46
    /**
47
     * Register the service provider.
48
     *
49
     * @return void
50
     */
51 107
    public function register()
52
    {
53 107
        if ($this->app instanceof Laravel) {
54 107
            $this->registerLaravelBindings();
55
        } 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...
56
            $this->registerLumenBindings();
57
        }
58
59 107
        $this->registerSerializerBindings();
60 107
        $this->registerErrorBindings();
61 107
        $this->registerFractalBindings();
62 107
        $this->registerResourceBindings();
63 107
        $this->registerPaginationBindings();
64 107
        $this->registerTransformationBindings();
65 107
        $this->registerTransformerBindings();
66 107
        $this->registerServiceBindings();
67 107
    }
68
69
    /**
70
     * Register Laravel bindings.
71
     *
72
     * @return void
73
     */
74
    protected function registerLaravelBindings()
75
    {
76 107
        $this->app->singleton(ResponseFactoryContract::class, function ($app) {
77
            return $this->decorateResponseFactory($app->make(LaravelResponseFactory::class));
78 107
        });
79 107
    }
80
81
    /**
82
     * Register Lumen bindings.
83
     *
84
     * @return void
85
     */
86
    protected function registerLumenBindings()
87
    {
88
        $this->app->singleton(ResponseFactoryContract::class, function ($app) {
89
            return $this->decorateResponseFactory($app->make(LumenResponseFactory::class));
90
        });
91
    }
92
93
    /**
94
     * Decorate response factories.
95
     *
96
     * @param  \Flugg\Responder\Contracts\ResponseFactory $factory
97
     * @return void
0 ignored issues
show
Documentation introduced by
Should the return type not be object?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
98
     */
99
    protected function decorateResponseFactory(ResponseFactoryContract $factory)
100
    {
101
        foreach ($this->app->config['responder.decorators'] as $decorator) {
102
            $factory = new $decorator($factory);
103
        };
104
105
        return $factory;
106
    }
107
108
    /**
109
     * Register serializer bindings.
110
     *
111
     * @return void
112
     */
113
    protected function registerSerializerBindings()
114
    {
115 107
        $this->app->bind(ErrorSerializerContract::class, function ($app) {
116
            return $app->make($app->config['responder.serializers.error']);
117 107
        });
118
119 107
        $this->app->bind(SerializerAbstract::class, function ($app) {
120
            return $app->make($app->config['responder.serializers.success']);
121 107
        });
122 107
    }
123
124
    /**
125
     * Register error bindings.
126
     *
127
     * @return void
128
     */
129
    protected function registerErrorBindings()
130
    {
131 107
        $this->app->singleton(ErrorMessageResolverContract::class, function ($app) {
132
            return $app->make(ErrorMessageResolver::class);
133 107
        });
134
135 107
        $this->app->singleton(ErrorFactoryContract::class, function ($app) {
136
            return $app->make(ErrorFactory::class);
137 107
        });
138 107
    }
139
140
    /**
141
     * Register Fractal bindings.
142
     *
143
     * @return void
144
     */
145
    protected function registerFractalBindings()
146
    {
147 107
        $this->app->bind(Manager::class, function ($app) {
148
            return (new Manager)->setRecursionLimit($app->config['responder.recursion_limit']);
149 107
        });
150 107
    }
151
152
    /**
153
     * Register pagination bindings.
154
     *
155
     * @return void
156
     */
157
    protected function registerResourceBindings()
158
    {
159 107
        $this->app->singleton(ResourceFactoryContract::class, function ($app) {
160
            return $app->make(ResourceFactory::class);
161 107
        });
162 107
    }
163
164
    /**
165
     * Register pagination bindings.
166
     *
167
     * @return void
168
     */
169
    protected function registerPaginationBindings()
170
    {
171 107
        $this->app->singleton(PaginatorFactoryContract::class, function ($app) {
172
            return new PaginatorFactory($app->make(Request::class)->query());
173 107
        });
174 107
    }
175
176
    /**
177
     * Register transformation bindings.
178
     *
179
     * @return void
180
     */
181
    protected function registerTransformationBindings()
182
    {
183 107
        $this->app->singleton(TransformFactoryContract::class, function ($app) {
184
            return $app->make(FractalTransformFactory::class);
185 107
        });
186
187 107
        $this->app->bind(TransformBuilder::class, function ($app) {
188
            return (new TransformBuilder($app->make(ResourceFactoryContract::class), $app->make(TransformFactoryContract::class), $app->make(PaginatorFactoryContract::class)))->serializer($app->make(SerializerAbstract::class))
189
                ->with($app->make(Request::class)->input($app->config['responder.load_relations_parameter'], []))
190
                ->only($app->make(Request::class)->input($app->config['responder.filter_fields_parameter'], []));
191 107
        });
192
193 107
        $this->app->singleton(TransformerResolverContract::class, function ($app) {
194
            return $app->make(TransformerResolver::class);
195 107
        });
196 107
    }
197
198
    /**
199
     * Register transformer bindings.
200
     *
201
     * @return void
202
     */
203
    protected function registerTransformerBindings()
204
    {
205 107
        $this->app->singleton(TransformerResolverContract::class, function ($app) {
206
            return $app->make(TransformerResolver::class);
207 107
        });
208
209 107
        Transformer::containerResolver(function () {
210
            return $this->app->make(Container::class);
211 107
        });
212 107
    }
213
214
    /**
215
     * Register service bindings.
216
     *
217
     * @return void
218
     */
219
    protected function registerServiceBindings()
220
    {
221 107
        $this->app->singleton(ResponderContract::class, function ($app) {
222
            return $app->make(Responder::class);
223 107
        });
224
225 107
        $this->app->singleton(TransformerContract::class, function ($app) {
226
            return $app->make(Transformer::class);
227 107
        });
228 107
    }
229
230
    /**
231
     * Bootstrap the application events.
232
     *
233
     * @return void
234
     */
235 107
    public function boot()
236
    {
237 107
        if ($this->app instanceof Laravel) {
238 107
            $this->bootLaravelApplication();
239
        } 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...
240
            $this->bootLumenApplication();
241
        }
242
243 107
        $this->mergeConfigFrom(__DIR__ . '/../config/responder.php', 'responder');
244 107
        $this->commands(MakeTransformer::class);
245 107
    }
246
247
    /**
248
     * Bootstrap the Laravel application.
249
     *
250
     * @return void
251
     */
252 107
    protected function bootLaravelApplication()
253
    {
254 107
        if ($this->app->runningInConsole()) {
255 107
            $this->publishes([
256 107
                __DIR__ . '/../config/responder.php' => config_path('responder.php'),
257 107
            ], 'config');
258 107
            $this->publishes([
259 107
                __DIR__ . '/../resources/lang/en/errors.php' => base_path('resources/lang/en/errors.php'),
260 107
            ], 'lang');
261
        }
262 107
    }
263
264
    /**
265
     * Bootstrap the Lumen application.
266
     *
267
     * @return void
268
     */
269
    protected function bootLumenApplication()
270
    {
271
        $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...
272
    }
273
}