ServiceProvider   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 101
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 89.66%

Importance

Changes 7
Bugs 4 Features 2
Metric Value
wmc 11
c 7
b 4
f 2
lcom 1
cbo 2
dl 0
loc 101
ccs 26
cts 29
cp 0.8966
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A register() 0 5 1
A registerAliasMiddleware() 0 6 2
A boot() 0 8 1
A autoAppendMiddleware() 0 16 4
A pushMiddlewareToGroups() 0 10 3
1
<?php
2
3
namespace Yemenifree\LaravelArabicNumbersMiddleware;
4
5
use Illuminate\Support\ServiceProvider as BaseServiceProvider;
6
use Yemenifree\LaravelArabicNumbersMiddleware\Middleware\TransformArabicToEasternNumbers;
7
use Yemenifree\LaravelArabicNumbersMiddleware\Middleware\TransformEasternToArabicNumbers;
8
9
class ServiceProvider extends BaseServiceProvider
10
{
11
    /**
12
     * All of the short-hand keys for middlewares.
13
     *
14
     * @var array
15
     */
16
    protected $middleware = [
17
        'arabic-to-eastern' => TransformArabicToEasternNumbers::class,
18
        'eastern-to-arabic' => TransformEasternToArabicNumbers::class,
19
    ];
20
21
    /**
22
     * list of group middleware to auto append middleware to them.
23
     *
24
     *      true => all groups
25
     *      false => none
26
     *      ['web'] => for web group only
27
     *
28
     * @var bool|array
29
     */
30
    protected $groupMiddleware = false;
31
32
    /**
33
     * auto append middleware.
34
     *
35
     * @var array
36
     */
37
    protected $auto_middleware;
38
39
    /**
40
     * Perform post-registration booting of services.
41
     *
42
     * @return void
43
     */
44 12
    public function boot()
45
    {
46 12
        $this->publishes([
47 12
            __DIR__.'/../config/arabic-numbers-middleware.php' => config_path('arabic-numbers-middleware.php'),
48 12
        ], 'config');
49
50 12
        $this->autoAppendMiddleware();
51 12
    }
52
53
    /**
54
     * auto append middleware to router.
55
     */
56 12
    protected function autoAppendMiddleware()
57
    {
58 12
        $this->groupMiddleware = $this->app['config']->get('arabic-numbers-middleware.auto_register_middleware', false);
59 12
        $this->auto_middleware = $this->app['config']->get('arabic-numbers-middleware.auto_middleware', false);
60
61 12
        if ($this->groupMiddleware === false || $this->auto_middleware === false) {
62 4
            return;
63
        }
64
65 12
        if ($this->groupMiddleware === true) { // Register middleware as global Middleware
66 12
            $this->app->make('Illuminate\Contracts\Http\Kernel')->pushMiddleware($this->auto_middleware);
67
        }
68
69
        // Register Middleware for route group
70 12
        $this->pushMiddlewareToGroups($this->auto_middleware);
71 12
    }
72
73
    /**
74
     * push middleware to route groups.
75
     *
76
     * @param array $middleware
77
     */
78 12
    public function pushMiddlewareToGroups($middleware)
79
    {
80 12
        if (! is_array($this->groupMiddleware)) {
81 12
            return;
82
        }
83
84
        foreach ($this->groupMiddleware as $group) {
85
            $this->app['router']->pushMiddlewareToGroup($group, $middleware);
86
        }
87
    }
88
89
    /**
90
     * Register any package services.
91
     *
92
     * @return void
93
     */
94 12
    public function register()
95
    {
96 12
        $this->mergeConfigFrom(__DIR__.'/../config/arabic-numbers-middleware.php', 'arabic-numbers-middleware');
97 12
        $this->registerAliasMiddleware();
98 12
    }
99
100
    /**
101
     *  register alias middleware.
102
     */
103 12
    protected function registerAliasMiddleware()
104
    {
105 12
        foreach ($this->middleware as $alias => $middleware) {
106 12
            $this->app['router']->aliasMiddleware($alias, $middleware);
107
        }
108 12
    }
109
}
110