QuotaServiceProvider::boot()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
/**
4
 * This file is part of laravel-quota
5
 *
6
 * (c) David Faith <[email protected]>
7
 *
8
 * Full copyright and license information is available
9
 * in the LICENSE file distributed with this source code.
10
 */
11
12
namespace Projectmentor\Quota;
13
14
use bandwidthThrottle\tokenBucket\Rate;
15
use bandwidthThrottle\tokenBucket\TokenBucket;
16
use bandwidthThrottle\tokenBucket\BlockingConsumer;
17
use bandwidthThrottle\tokenBucket\storage\FileStorage;
18
19
use Illuminate\Foundation\Application as LaravelApplication;
20
use Laravel\Lumen\Application as LumenApplication;
21
22
use Illuminate\Support\ServiceProvider;
23
24
use Projectmentor\Quota\Factories\RateFactory;
25
use Projectmentor\Quota\Factories\FileStorageFactory;
26
use Projectmentor\Quota\Factories\TokenBucketFactory;
27
use Projectmentor\Quota\Factories\BlockingConsumerFactory;
28
29
use Projectmentor\Quota\Stubs\RateData;
30
use Projectmentor\Quota\Stubs\FileStorageData;
31
use Projectmentor\Quota\Stubs\TokenBucketData;
32
use Projectmentor\Quota\Stubs\BlockingConsumerData;
33
34
/**
35
 * This is the Quota service provider.
36
 *
37
 * "There is no need to bind classes into the container
38
 * if they do not depend on any interfaces"
39
 * - @See https://laravel.com/docs/5.2/container#binding
40
 *
41
 * Hence the base classes we are exposing from the library
42
 * Are not going to be registered here.
43
 *
44
 * We will register our factory classes and the Quota class
45
 * which will be used both as a `manager` and as a concrete
46
 * instance by leveraging  call_user_func_array(). In this
47
 * manner we will be able to access Quota as a singleton,
48
 * via a Facade, and it will maintain an array of rate-limiters
49
 * which we can retrieve when needed.
50
 * @See Projectmentor\Quota\Quota.php
51
 *
52
 *
53
 * @author David Faith <[email protected]>
54
 */
55
class QuotaServiceProvider extends ServiceProvider
56
{
57
    /**
58
     * Indicates if loading of the provider is deferred.
59
     *
60
     * @var bool
61
     */
62
    protected $defer = true;
63
64
    /**
65
     * Perform post-registration booting of services.
66
     *
67
     * @return void
68
     */
69 20
    public function boot()
70
    {
71 20
        $this->setupConfig();
72 20
    }
73
74
    /**
75
     * Setup the config
76
     *
77
     * @return void
78
     */
79 20
    protected function setupConfig()
80
    {
81 20
        $source = realpath(__DIR__.'/../config/quota.php');
82
83 20
        if ($this->app instanceof LaravelApplication &&
84 20
            $this->app->runningInConsole()) {
85 20
            $this->publishes([$source => config_path('quota.php')]);
86 20
        } elseif ($this->app instanceof LumenApplication) {
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...
87
            $this->app->configure('quota');
88
        }
89
90 20
        $this->mergeConfigFrom($source, 'quota');
91 20
    }
92
93
    /**
94
     * Register any package services.
95
     *
96
     * @return void
97
     */
98 20
    public function register()
99
    {
100 20
        $this->bindInterfaces();
101 20
        $this->registerFactories();
102 20
    }
103
104
    /**
105
     * Bind interfaces to implementations.
106
     * @return void
107
     */
108 20
    public function bindInterfaces()
109
    {
110 20
        $this->app->when(RateFactory::class)
111 20
          ->needs('Projectmentor\Quota\Contracts\PayloadInterface')
112 20
          ->give(RateData::class);
113
114 20
        $this->app->when(FileStorageFactory::class)
115 20
          ->needs('Projectmentor\Quota\Contracts\PayloadInterface')
116 20
          ->give(FileStorageData::class);
117
118 20
        $this->app->when(TokenBucketFactory::class)
119 20
          ->needs('Projectmentor\Quota\Contracts\PayloadInterface')
120 20
          ->give(TokenBucketData::class);
121
122 20
        $this->app->when(BlockingConsumerFactory::class)
123 20
          ->needs('Projectmentor\Quota\Contracts\PayloadInterface')
124 20
          ->give(BlockingConsumerData::class);
125 20
    }
126
127
    /**
128
     * Register the factory classes.
129
     * @return void
130
     */
131 20
    protected function registerFactories()
132
    {
133
        $this->app->singleton('quota.factory.rate', function ($app) {
0 ignored issues
show
Unused Code introduced by
The parameter $app is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
134 1
            return new RateFactory;
135 20
        });
136
137
        $this->app->singleton('quota.factory.storage.file', function ($app) {
0 ignored issues
show
Unused Code introduced by
The parameter $app is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
138 1
            return new FileStorageFactory;
139 20
        });
140
141
        $this->app->singleton('quota.factory.tokenbucket', function ($app) {
0 ignored issues
show
Unused Code introduced by
The parameter $app is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
142 2
            return new TokenBucketFactory;
143 20
        });
144
145 20
        $this->app->singleton('quota.factory.blockingconsumer', function ($app) {
0 ignored issues
show
Unused Code introduced by
The parameter $app is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
146 1
            return new BlockingConsumerFactory;
147 20
        });
148
149 20
        $this->app->alias('quota.factory.rate', RateFactory::class);
150 20
        $this->app->alias('quota.factory.rate', FactoryInterface::class);
151
152 20
        $this->app->alias('quota.factory.storage.file', FileStorageFactory::class);
153 20
        $this->app->alias('quota.factory.storage.file', FactoryInterface::class);
154
155 20
        $this->app->alias('quota.factory.tokenbucket', TokenBucketFactory::class);
156 20
        $this->app->alias('quota.factory.tokenbucket', FactoryInterface::class);
157
158 20
        $this->app->alias('quota.factory.blockingconsumer', BlockingConsumerFactory::class);
159 20
        $this->app->alias('quota.factory.blockingconsumer', FactoryInterface::class);
160 20
    }
161
162
    /**
163
     * Get the services provided by this provider.
164
     *
165
     * @return string[]
166
     */
167 2
    public function provides()
168
    {
169
        return [
170 2
            'quota.factory.rate',
171 2
            'quota.factory.storage.file',
172 2
            'quota.factory.tokenbucket',
173
            'quota.factory.blockingconsumer'
174 2
        ];
175
    }
176
}
0 ignored issues
show
Coding Style introduced by
As per coding style, files should not end with a newline character.

This check marks files that end in a newline character, i.e. an empy line.

Loading history...
177