Passed
Pull Request — master (#238)
by Alex
04:17
created

MetadataRouteProvider::isAuthEnable()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
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
    /**
45
     * Determine what auth middleware, if any, to use for OData routes
46
     *
47
     * @return string|null
48
     */
49
    private function getAuthMiddleware(): ?string
50
    {
51
        $disable = !$this->isAuthEnable();
52
        if ($disable) {
53
            return null;
54
        }
55
56
        $authMiddleware = 'auth.basic';
57
58
        if (interface_exists(\Illuminate\Contracts\Auth\Factory::class)) {
59
            /** @var \Illuminate\Contracts\Auth\Factory $manager */
60
            $manager        = App::make(\Illuminate\Contracts\Auth\Factory::class);
61
            $authMiddleware = $manager->guard('api') ? 'auth:api' : $authMiddleware;
62
        }
63
64
        return $authMiddleware;
65
    }
66
67
    /**
68
     * @return bool
69
     */
70
    protected function isAuthEnable(): bool
71
    {
72
        $env = env('APP_ENABLE_AUTH', null);
73
        return true === boolval($env);
74
    }
75
}
76