Test Failed
Push — master ( fc5cfa...18d5e6 )
by Alex
03:42
created

MetadataRouteProvider::getAuthMiddleware()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

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