Completed
Pull Request — master (#589)
by
unknown
12:05 queued 02:55
created

GetFromMiddleware::getScopesInfo()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 34

Duplication

Lines 20
Ratio 58.82 %

Importance

Changes 0
Metric Value
dl 20
loc 34
rs 9.376
c 0
b 0
f 0
cc 3
nc 3
nop 1
1
<?php
2
3
namespace Mpociot\ApiDoc\Strategies\Metadata;
4
5
use ReflectionClass;
6
use ReflectionMethod;
7
use Illuminate\Routing\Route;
8
use Laravel\Passport\Passport;
9
use Mpociot\ApiDoc\Strategies\Strategy;
10
11
class GetFromMiddleware extends Strategy
12
{
13
    public function __invoke(Route $route, ReflectionClass $controller, ReflectionMethod $method, array $routeRules, array $context = [])
14
    {
15
        return [
16
            'scopes' => $this->getScopesInfo($route),
17
            'headers' => $this->getHeaders($route),
18
        ];
19
    }
20
21
    protected function getScopesInfo($route)
22
    {
23
        $middlewares = $route->gatherMiddleware();
24
        $anyScopeMiddleware = collect($middlewares)->first(function ($item) {
25
            return preg_match('/^scope:/', $item);
26
        });
27 View Code Duplication
        if ($anyScopeMiddleware) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
28
            $scopes = explode(',', preg_replace('/^scope:/', '', $anyScopeMiddleware));
29
            $scopesInfo = Passport::scopes()->filter(function ($item) use ($scopes) {
30
                return in_array($item->id, $scopes);
31
            })->values();
32
33
            return [
34
                'type' => 'any',
35
                'scopes' => $scopesInfo,
36
            ];
37
        }
38
        $allScopeMiddleware = collect($middlewares)->first(function ($item) {
39
            return preg_match('/^scopes:/', $item);
40
        });
41 View Code Duplication
        if ($allScopeMiddleware) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
42
            $scopes = explode(',', preg_replace('/^scopes:/', '', $allScopeMiddleware));
43
            $scopesInfo = Passport::scopes()->filter(function ($item) use ($scopes) {
44
                return in_array($item->id, $scopes);
45
            })->values();
46
47
            return [
48
                'type' => 'all',
49
                'scopes' => $scopesInfo,
50
            ];
51
        }
52
53
        return null;
54
    }
55
56
    protected function getHeaders($route)
57
    {
58
        $middlewares = $route->gatherMiddleware();
59
        $authMiddleware = collect($middlewares)->first(function ($item) {
60
            return preg_match('/^auth:/', $item);
61
        });
62
        $headers = [];
63
        if ($authMiddleware) {
64
            $guard = preg_replace('/^auth:/', '', $authMiddleware);
65
            $driver = config('auth.guards.'.$guard.'.driver');
66
            if ($driver === 'token') {
67
                $headers['Authorization'] = 'Token {token}';
68
            } elseif ($driver === 'passport' || $driver === 'jwt') {
69
                $headers['Authorization'] = 'Bearer {access_token}';
70
            }
71
        }
72
73
        return $headers;
74
    }
75
}
76