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

GetFromMiddleware   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 65
Duplicated Lines 30.77 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
wmc 9
lcom 0
cbo 1
dl 20
loc 65
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __invoke() 0 7 1
A getScopesInfo() 20 34 3
A getHeaders() 0 19 5

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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