ThrottleServiceProvider   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 102
Duplicated Lines 0 %

Test Coverage

Coverage 95.12%

Importance

Changes 14
Bugs 2 Features 0
Metric Value
eloc 29
c 14
b 2
f 0
dl 0
loc 102
ccs 39
cts 41
cp 0.9512
rs 10
wmc 11

7 Methods

Rating   Name   Duplication   Size   Complexity  
A registerTransformer() 0 8 1
A register() 0 5 1
A boot() 0 3 1
A setupConfig() 0 11 5
A provides() 0 6 1
A registerThrottle() 0 10 1
A registerFactory() 0 10 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of Laravel Throttle.
7
 *
8
 * (c) Graham Campbell <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace GrahamCampbell\Throttle;
15
16
use GrahamCampbell\Throttle\Factories\CacheFactory;
17
use GrahamCampbell\Throttle\Factories\FactoryInterface;
18
use GrahamCampbell\Throttle\Transformers\TransformerFactory;
19
use GrahamCampbell\Throttle\Transformers\TransformerFactoryInterface;
20
use Illuminate\Contracts\Container\Container;
21
use Illuminate\Foundation\Application as LaravelApplication;
22
use Illuminate\Support\ServiceProvider;
23
use Laravel\Lumen\Application as LumenApplication;
24
25
/**
26
 * This is the throttle service provider class.
27
 *
28
 * @author Graham Campbell <[email protected]>
29
 */
30
class ThrottleServiceProvider extends ServiceProvider
31
{
32
    /**
33
     * Boot the service provider.
34
     *
35
     * @return void
36
     */
37 192
    public function boot()
38
    {
39 192
        $this->setupConfig();
40 192
    }
41
42
    /**
43
     * Setup the config.
44
     *
45
     * @return void
46
     */
47 192
    protected function setupConfig()
48
    {
49 192
        $source = realpath($raw = __DIR__.'/../config/throttle.php') ?: $raw;
50
51 192
        if ($this->app instanceof LaravelApplication && $this->app->runningInConsole()) {
52 192
            $this->publishes([$source => config_path('throttle.php')]);
53
        } elseif ($this->app instanceof LumenApplication) {
54
            $this->app->configure('throttle');
55
        }
56
57 192
        $this->mergeConfigFrom($source, 'throttle');
58 192
    }
59
60
    /**
61
     * Register the service provider.
62
     *
63
     * @return void
64
     */
65 192
    public function register()
66
    {
67 192
        $this->registerFactory();
68 192
        $this->registerTransformer();
69 192
        $this->registerThrottle();
70 192
    }
71
72
    /**
73
     * Register the factory class.
74
     *
75
     * @return void
76
     */
77 160
    protected function registerFactory()
78
    {
79 32
        $this->app->singleton('throttle.factory', function (Container $app) {
80 120
            $cache = $app->cache->driver($app->config->get('throttle.driver'));
81
82 120
            return new CacheFactory($cache);
83 192
        });
84
85 192
        $this->app->alias('throttle.factory', CacheFactory::class);
86 192
        $this->app->alias('throttle.factory', FactoryInterface::class);
87 192
    }
88
89
    /**
90
     * Register the transformer class.
91
     *
92
     * @return void
93
     */
94 160
    protected function registerTransformer()
95
    {
96 32
        $this->app->singleton('throttle.transformer', function () {
97 120
            return new TransformerFactory();
98 192
        });
99
100 192
        $this->app->alias('throttle.transformer', TransformerFactory::class);
101 192
        $this->app->alias('throttle.transformer', TransformerFactoryInterface::class);
102 192
    }
103
104
    /**
105
     * Register the throttle class.
106
     *
107
     * @return void
108
     */
109 160
    protected function registerThrottle()
110
    {
111 32
        $this->app->singleton('throttle', function (Container $app) {
112 108
            $factory = $app['throttle.factory'];
113 108
            $transformer = $app['throttle.transformer'];
114
115 108
            return new Throttle($factory, $transformer);
116 192
        });
117
118 192
        $this->app->alias('throttle', Throttle::class);
119 192
    }
120
121
    /**
122
     * Get the services provided by the provider.
123
     *
124
     * @return string[]
125
     */
126 24
    public function provides()
127
    {
128
        return [
129 24
            'throttle',
130
            'throttle.factory',
131
            'throttle.transformer',
132
        ];
133
    }
134
}
135