Completed
Push — master ( 2d5c6f...ea456d )
by Alexander
06:30
created

ResponderServiceProvider::bootLumenApplication()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
c 1
b 0
f 0
1
<?php
2
3
namespace Flugg\Responder;
4
5
use Flugg\Responder\Console\MakeTransformer;
6
use Flugg\Responder\Contracts\Manager as ManagerContract;
7
use Flugg\Responder\Contracts\Responder as ResponderContract;
8
use Flugg\Responder\Factories\ErrorResponseFactory;
9
use Flugg\Responder\Factories\SuccessResponseFactory;
10
use Illuminate\Foundation\Application as Laravel;
11
use Illuminate\Support\ServiceProvider as BaseServiceProvider;
12
use Laravel\Lumen\Application as Lumen;
13
use League\Fractal\Manager;
14
15
/**
16
 * The Laravel Responder service provider. This is where the package is bootstrapped.
17
 *
18
 * @package Laravel Responder
19
 * @author  Alexander Tømmerås <[email protected]>
20
 * @license The MIT License
21
 */
22
class ResponderServiceProvider extends BaseServiceProvider
23
{
24
    /**
25
     * Indicates if loading of the provider is deferred.
26
     *
27
     * @var bool
28
     */
29
    protected $defer = false;
30
31
    /**
32
     * Bootstrap the application events.
33
     *
34
     * @return void
35
     */
36
    public function boot()
37
    {
38
        if ( $this->app instanceof Laravel && $this->app->runningInConsole() ) {
39
            $this->bootLaravelApplication();
40
41
        } 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...
42
            $this->bootLumenApplication();
43
        }
44
45
        $this->mergeConfigFrom( __DIR__ . '/../resources/config/responder.php', 'responder' );
46
47
        $this->commands( [
48
            MakeTransformer::class
49
        ] );
50
51
        include __DIR__ . '/helpers.php';
52
    }
53
54
55
    /**
56
     * Bootstrap the Laravel application.
57
     *
58
     * @return void
59
     */
60
    protected function bootLaravelApplication()
61
    {
62
        $this->publishes( [
63
            __DIR__ . '/../resources/config/responder.php' => config_path( 'responder.php' )
64
        ], 'config' );
65
66
        $this->publishes( [
67
            __DIR__ . '/../resources/lang/en/errors.php' => resource_path( 'lang/en/errors.php' )
68
        ], 'lang' );
69
    }
70
71
    /**
72
     * Bootstrap the Lumen application.
73
     *
74
     * @return void
75
     */
76
    protected function bootLumenApplication()
77
    {
78
        $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...
79
    }
80
81
82
    /**
83
     * Register the service provider.
84
     *
85
     * @return void
86
     */
87
    public function register()
88
    {
89
        $this->app->singleton( ResponderContract::class, function ( $app ) {
90
            $statusCodes = $app->config->get( 'responder.status_code' );
91
            $successFactory = new SuccessResponseFactory( $statusCodes );
92
            $errorFactory = new ErrorResponseFactory( $statusCodes );
93
94
            return ( new Responder( $successFactory, $errorFactory ) );
95
        } );
96
97
        $this->app->singleton( ManagerContract::class, function ( $app ) {
98
            $serializerClass = $app->config->get( 'responder.serializer' );;
0 ignored issues
show
Coding Style introduced by
It is generally recommended to place each PHP statement on a line by itself.

Let’s take a look at an example:

// Bad
$a = 5; $b = 6; $c = 7;

// Good
$a = 5;
$b = 6;
$c = 7;
Loading history...
99
            $serializer = new $serializerClass;
100
101
            return ( new Manager() )->setSerializer( new $serializer );
102
        } );
103
104
        $this->app->alias( ResponderContract::class, 'responder' );
105
        $this->app->alias( ManagerContract::class, 'responder.manager' );
106
    }
107
108
    /**
109
     * Get the services provided by the provider.
110
     *
111
     * @return array
112
     */
113
    public function provides()
114
    {
115
        return [ 'responder', 'responder.manager' ];
116
    }
117
}