MetadataRouteProvider   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 8
eloc 17
c 0
b 0
f 0
dl 0
loc 57
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A setupRoute() 0 10 1
A isAuthDisable() 0 3 1
A register() 0 2 1
A boot() 0 3 1
A getAuthMiddleware() 0 16 4
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AlgoWeb\PODataLaravel\Providers;
6
7
use Illuminate\Support\Facades\App;
8
use Illuminate\Support\Facades\Route;
9
use Illuminate\Support\ServiceProvider;
10
11
class MetadataRouteProvider extends ServiceProvider
12
{
13
    /**
14
     * Bootstrap the application services.
15
     *
16
     * @return void
17
     */
18
    public function boot(): void
19
    {
20
        $this->setupRoute();
21
    }
22
23
    private function setupRoute(): void
24
    {
25
        $authMiddleware   = $this->getAuthMiddleware();
26
        $controllerMethod = 'AlgoWeb\PODataLaravel\Controllers\ODataController@index';
27
28
        Route::get('odata.svc/$metadata', ['uses' => $controllerMethod, 'middleware' => null]);
29
30
        Route::any('odata.svc/{section}', ['uses' => $controllerMethod, 'middleware' => $authMiddleware])
31
            ->where(['section' => '.*']);
32
        Route::any('odata.svc', ['uses' => $controllerMethod, 'middleware' => null]);
33
    }
34
35
    /**
36
     * Register the application services.
37
     *
38
     * @return void
39
     */
40
    public function register(): void
41
    {
42
    }
43
44
    private function getAuthMiddleware(): ?string
45
    {
46
        $disable = $this->isAuthDisable();
47
        if ($disable) {
48
            return null;
49
        }
50
51
        $authMiddleware = 'auth.basic';
52
53
        if (interface_exists(\Illuminate\Contracts\Auth\Factory::class)) {
54
            /** @var \Illuminate\Contracts\Auth\Factory $manager */
55
            $manager        = App::make(\Illuminate\Contracts\Auth\Factory::class);
56
            $authMiddleware = $manager->guard('api') ? 'auth:api' : $authMiddleware;
57
        }
58
59
        return $authMiddleware;
60
    }
61
62
    /**
63
     * @return bool
64
     */
65
    protected function isAuthDisable(): bool
66
    {
67
        return true === boolval(env('APP_DISABLE_AUTH', null));
68
    }
69
}
70