Passed
Push — master ( 3e18aa...fdb701 )
by Salah
02:01
created

ServiceProvider   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 100
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 78.56%

Importance

Changes 4
Bugs 2 Features 2
Metric Value
wmc 12
c 4
b 2
f 2
lcom 1
cbo 2
dl 0
loc 100
ccs 22
cts 28
cp 0.7856
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A boot() 0 8 1
A register() 0 5 1
A registerAliasMiddleware() 0 6 2
B autoAppendMiddleware() 0 17 6
A pushMiddlewareToGroups() 0 6 2
1
<?php
2
3
namespace Yemenifree\LaravelArabicNumbersMiddleware;
4
5
use Illuminate\Support\Collection;
6
use Illuminate\Support\ServiceProvider as BaseServiceProvider;
7
use Yemenifree\LaravelArabicNumbersMiddleware\Middleware\TransformArabicToEasternNumbers;
8
use Yemenifree\LaravelArabicNumbersMiddleware\Middleware\TransformEasternToArabicNumbers;
9
10
class ServiceProvider extends BaseServiceProvider
11
{
12
    /**
13
     * All of the short-hand keys for middlewares.
14
     *
15
     * @var array
16
     */
17
    protected $middleware = [
18
        'arabic-to-eastern' => TransformArabicToEasternNumbers::class,
19
        'eastern-to-arabic' => TransformEasternToArabicNumbers::class
20
    ];
21
22
    /**
23
     * list of group middleware to auto append middleware to them.
24
     *
25
     *      true => all groups
26
     *      false => none
27
     *      ['web'] => for web group only
28
     *
29
     * @var bool|array
30
     */
31
    protected $groupMiddleware = false;
32
33
    /**
34
     * auto append middleware.
35
     *
36
     * @var Collection
37
     */
38
    protected $auto_middleware;
39
40
41
    /**
42
     * Perform post-registration booting of services.
43
     *
44
     * @return void
45
     */
46 12
    public function boot()
47
    {
48 12
        $this->publishes([
49 12
            __DIR__.'/../config/arabic-numbers-middleware.php' => config_path('arabic-numbers-middleware.php'),
50 12
        ], 'config');
51
52 12
        $this->autoAppendMiddleware();
53 12
    }
54
55
56
    /**
57
     * auto append middleware to router.
58
     */
59 12
    protected function autoAppendMiddleware()
60
    {
61 12
        $this->groupMiddleware = $this->app['config']->get('arabic-numbers-middleware.auto_register_middleware', false);
62 12
        $this->auto_middleware = $this->app['config']->get('arabic-numbers-middleware.auto_middleware', false);
63
64 12
        if ($this->groupMiddleware === false || $this->auto_middleware === false) {
65 4
            return;
66
        }
67
68 12
        if ($this->groupMiddleware === true) { // Register middleware as global Middleware
69 12
            $this->app->make('Illuminate\Contracts\Http\Kernel')->pushMiddleware($this->auto_middleware);
70
        } else if (is_array($this->groupMiddleware) && count($this->groupMiddleware) > 0) { // Register Middleware for route group
71
            $this->pushMiddlewareToGroups($this->auto_middleware);
72
        }
73
74 12
        return;
75
    }
76
77
    /**
78
     * push middleware to route groups.
79
     *
80
     * @param $middleware
81
     */
82
    function pushMiddlewareToGroups($middleware)
83
    {
84
        foreach ($this->groupMiddleware as $group) {
0 ignored issues
show
Bug introduced by
The expression $this->groupMiddleware of type boolean|array is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
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
    }
109
}