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

ServiceProvider::pushMiddlewareToGroups()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 6
ccs 0
cts 4
cp 0
rs 9.4285
cc 2
eloc 3
nc 2
nop 1
crap 6
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
}