BlogApiServiceProvider::resourceOverride()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
eloc 4
c 2
b 1
f 0
dl 0
loc 6
rs 10
cc 2
nc 2
nop 0
1
<?php
2
3
namespace CSlant\Blog\Api\Providers;
4
5
use CSlant\Blog\Api\Http\Middlewares\ApiActionRateLimiter;
6
use Illuminate\Routing\Router;
7
use Illuminate\Support\ServiceProvider;
8
9
class BlogApiServiceProvider extends ServiceProvider
10
{
11
    /**
12
     * Bootstrap services.
13
     *
14
     * @return void
15
     */
16
    public function boot(): void
17
    {
18
        $routePath = __DIR__.'/../../routes/blog-api.php';
19
        if (file_exists($routePath)) {
20
            $this->loadRoutesFrom($routePath);
21
        }
22
23
        $this->loadTranslationsFrom(__DIR__.'/../../lang', 'blog-api');
24
25
        $this->loadMigrationsFrom(__DIR__.'/../../database/migrations');
26
27
        $this->registerCommands();
28
29
        $this->registerAssetPublishing();
30
31
        $this->resourceOverride();
32
33
        $this->registerMiddlewares();
34
    }
35
36
    /**
37
     * Register services.
38
     *
39
     * @return void
40
     */
41
    public function register(): void
42
    {
43
        $this->registerConfigs();
44
    }
45
46
    /**
47
     * Register configs.
48
     *
49
     * @return void
50
     */
51
    protected function registerConfigs(): void
52
    {
53
        $configDir = __DIR__.'/../../config';
54
        $files = scandir($configDir);
55
56
        if ($files === false) {
57
            return;
58
        }
59
60
        foreach ($files as $file) {
61
            if (pathinfo($file, PATHINFO_EXTENSION) === 'php') {
62
                $configName = pathinfo($file, PATHINFO_FILENAME);
63
                $configPath = $configDir.'/'.$file;
64
65
                if (file_exists(config_path($configName.'.php'))) {
0 ignored issues
show
Bug introduced by
Are you sure $configName of type array|string can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

65
                if (file_exists(config_path(/** @scrutinizer ignore-type */ $configName.'.php'))) {
Loading history...
66
                    config()->set($configName, array_merge(
0 ignored issues
show
Bug introduced by
The call to config() has too few arguments starting with string. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

66
                    /** @scrutinizer ignore-call */ 
67
                    config()->set($configName, array_merge(

This check compares calls to functions or methods with their respective definitions. If the call has less arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
67
                        is_array(config($configName)) ? config($configName) : [],
0 ignored issues
show
Bug introduced by
It seems like $configName can also be of type array; however, parameter $string of config() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

67
                        is_array(config(/** @scrutinizer ignore-type */ $configName)) ? config($configName) : [],
Loading history...
68
                        require $configPath
69
                    ));
70
                } else {
71
                    $this->mergeConfigFrom($configPath, $configName);
0 ignored issues
show
Bug introduced by
It seems like $configName can also be of type array; however, parameter $key of Illuminate\Support\Servi...ider::mergeConfigFrom() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

71
                    $this->mergeConfigFrom($configPath, /** @scrutinizer ignore-type */ $configName);
Loading history...
72
                }
73
            }
74
        }
75
    }
76
77
    /**
78
     * Get the services provided by the provider.
79
     *
80
     * @return null|array<string>
81
     */
82
    public function provides(): ?array
83
    {
84
        return ['blog-api'];
85
    }
86
87
    /**
88
     * @return void
89
     */
90
    protected function registerCommands(): void
91
    {
92
        $this->commands([
93
            //
94
        ]);
95
    }
96
97
    /**
98
     * @return void
99
     */
100
    protected function registerAssetPublishing(): void
101
    {
102
        $configPath = __DIR__.'/../../config/blog-api.php';
103
        $this->publishes([
104
            $configPath => config_path('blog-api.php'),
105
        ], 'config');
106
107
        $this->publishes([
108
            __DIR__.'/../../lang' => resource_path('lang/packages/blog-api'),
109
        ], 'lang');
110
    }
111
112
    /**
113
     * Override resource of the package.
114
     */
115
    public function resourceOverride(): void
116
    {
117
        if (!class_exists(\Botble\Blog\Http\Resources\TagResource::class, false)) {
118
            class_alias(
119
                \CSlant\Blog\Api\Http\Resources\Tag\TagResource::class,
120
                \Botble\Blog\Http\Resources\TagResource::class
121
            );
122
        }
123
    }
124
125
    /**
126
     * Register middlewares for the package.
127
     */
128
    protected function registerMiddlewares(): void
129
    {
130
        /** @var Router $router */
131
        $router = $this->app->make('router');
132
133
        // Register route middleware
134
        $router->aliasMiddleware('api-action-rate-limiter', ApiActionRateLimiter::class);
135
    }
136
}
137