Completed
Pull Request — master (#409)
by
unknown
01:37
created

RouteMatcher   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 16
lcom 1
cbo 4
dl 0
loc 75
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getDingoRoutesToBeDocumented() 0 4 1
A getLaravelRoutesToBeDocumented() 0 4 1
B getRoutesToBeDocumented() 0 29 6
A getAllRoutes() 0 13 2
A shouldIncludeRoute() 0 11 5
A shouldExcludeRoute() 0 6 1
1
<?php
2
3
namespace Mpociot\ApiDoc\Tools;
4
5
use Illuminate\Routing\Route;
6
use Dingo\Api\Routing\RouteCollection;
7
use Illuminate\Support\Facades\Route as RouteFacade;
8
9
class RouteMatcher
10
{
11
    public function getDingoRoutesToBeDocumented(array $routeRules)
12
    {
13
        return $this->getRoutesToBeDocumented($routeRules, true);
14
    }
15
16
    public function getLaravelRoutesToBeDocumented(array $routeRules)
17
    {
18
        return $this->getRoutesToBeDocumented($routeRules);
19
    }
20
21
    public function getRoutesToBeDocumented(array $routeRules, bool $usingDingoRouter = false)
22
    {
23
        $matchedRoutes = [];
24
25
        foreach ($routeRules as $routeRule) {
26
            $includes = $routeRule['include'] ?? [];
27
            $allRoutes = $this->getAllRoutes($usingDingoRouter, $routeRule['match']['versions'] ?? []);
28
29
            foreach ($allRoutes as $route) {
30
                if (is_array($route)) {
31
                    $route = new LumenRouteAdapter($route);
32
                }
33
34
                if ($this->shouldExcludeRoute($route, $routeRule)) {
35
                    continue;
36
                }
37
38
                if ($this->shouldIncludeRoute($route, $routeRule, $includes, $usingDingoRouter)) {
39
                    $matchedRoutes[] = [
40
                        'route' => $route,
41
                        'apply' => $routeRule['apply'] ?? [],
42
                    ];
43
                    continue;
44
                }
45
            }
46
        }
47
48
        return $matchedRoutes;
49
    }
50
51
    private function getAllRoutes(bool $usingDingoRouter, array $versions = [])
0 ignored issues
show
Unused Code introduced by
The parameter $versions is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
52
    {
53
        if (! $usingDingoRouter) {
54
            return RouteFacade::getRoutes();
55
        }
56
57
        $allRouteCollections = app(\Dingo\Api\Routing\Router::class)->getRoutes();
58
59
        return collect($allRouteCollections)
60
            ->flatMap(function (RouteCollection $collection) {
61
                return $collection->getRoutes();
62
            })->toArray();
63
    }
64
65
    private function shouldIncludeRoute(Route $route, array $routeRule, array $mustIncludes, bool $usingDingoRouter)
66
    {
67
        $matchesVersion = $usingDingoRouter
68
            ? ! empty(array_intersect($route->versions(), $routeRule['match']['versions'] ?? []))
69
            : true;
70
71
        return str_is($mustIncludes, $route->getName())
72
            || (str_is($routeRule['match']['domains'] ?? [], $route->getDomain())
73
            && str_is($routeRule['match']['prefixes'] ?? [], $route->uri())
74
            && $matchesVersion);
75
    }
76
77
    private function shouldExcludeRoute(Route $route, array $routeRule)
78
    {
79
        $excludes = $routeRule['exclude'] ?? [];
80
81
        return str_is($excludes, $route->getName());
82
    }
83
}
84