Completed
Pull Request — master (#382)
by Alexandre
30:39
created

RouteMatcher   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

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

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getDingoRoutesToBeDocumented() 0 4 1
A getLaravelRoutesToBeDocumented() 0 4 1
B getRoutesToBeDocumented() 0 31 6
A getAllRoutes() 0 13 2
A shouldIncludeRoute() 0 11 5
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
            $excludes = $routeRule['exclude'] ?? [];
27
            $includes = $routeRule['include'] ?? [];
28
            $allRoutes = $this->getAllRoutes($usingDingoRouter, $routeRule['match']['versions'] ?? []);
29
30
            foreach ($allRoutes as $route) {
31
                if (is_array($route)) {
32
                    $route = new LumenRouteAdapter($route);
33
                }
34
35
                /** @var Route $route */
36
                if (in_array($route->getName(), $excludes)) {
37
                    continue;
38
                }
39
40
                if ($this->shouldIncludeRoute($route, $routeRule, $includes, $usingDingoRouter)) {
41
                    $matchedRoutes[] = [
42
                        'route' => $route,
43
                        'apply' => $routeRule['apply'] ?? [],
44
                    ];
45
                    continue;
46
                }
47
            }
48
        }
49
50
        return $matchedRoutes;
51
    }
52
53
    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...
54
    {
55
        if (! $usingDingoRouter) {
56
            return RouteFacade::getRoutes();
57
        }
58
59
        $allRouteCollections = app(\Dingo\Api\Routing\Router::class)->getRoutes();
60
61
        return collect($allRouteCollections)
62
            ->flatMap(function (RouteCollection $collection) {
63
                return $collection->getRoutes();
64
            })->toArray();
65
    }
66
67
    private function shouldIncludeRoute(Route $route, array $routeRule, array $mustIncludes, bool $usingDingoRouter)
68
    {
69
        $matchesVersion = $usingDingoRouter
70
            ? ! empty(array_intersect($route->versions(), $routeRule['match']['versions'] ?? []))
71
            : true;
72
73
        return in_array($route->getName(), $mustIncludes)
74
            || (str_is($routeRule['match']['domains'] ?? [], $route->getDomain())
75
            && str_is($routeRule['match']['prefixes'] ?? [], $route->uri())
76
            && $matchesVersion);
77
    }
78
}
79