Completed
Pull Request — master (#351)
by
unknown
06:32
created

RouteMatcher::getAllRoutes()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.8666
c 0
b 0
f 0
cc 2
nc 2
nop 2
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
                /** @var Route $route */
32
                if (in_array($route->getName(), $excludes)) {
33
                    continue;
34
                }
35
36
                if ($this->shouldIncludeRoute($route, $routeRule, $includes, $usingDingoRouter)) {
37
                    $matchedRoutes[] = [
38
                        'route' => $route,
39
                        'apply' => $routeRule['apply'] ?? [],
40
                    ];
41
                    continue;
42
                }
43
            }
44
        }
45
46
        return $matchedRoutes;
47
    }
48
49
    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...
50
    {
51
        if (!$usingDingoRouter) {
52
            return RouteFacade::getRoutes();
53
        }
54
55
        $allRouteCollections = app(\Dingo\Api\Routing\Router::class)->getRoutes();
56
        return collect($allRouteCollections)
57
            ->flatMap(function (RouteCollection $collection) {
58
                return $collection->getRoutes();
59
            })->toArray();
60
    }
61
62
    private function shouldIncludeRoute(Route $route, array $routeRule, array $mustIncludes, bool $usingDingoRouter)
63
    {
64
        $matchesVersion = $usingDingoRouter
65
            ? !empty(array_intersect($route->versions(), $routeRule['match']['versions'] ?? []))
66
            : true;
67
68
        return in_array($route->getName(), $mustIncludes)
69
            || (str_is($routeRule['match']['domains'] ?? [], $route->getDomain())
70
            && str_is($routeRule['match']['prefixes'] ?? [], $route->uri())
71
            && $matchesVersion);
72
    }
73
74
}
75