• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

IlyasDeckers / ody-core / 13532154862

25 Feb 2025 10:24PM UTC coverage: 30.374% (+1.7%) from 28.706%
13532154862

push

github

web-flow
Update php.yml

544 of 1791 relevant lines covered (30.37%)

9.13 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

86.67
/src/Middleware/RoutingMiddleware.php
1
<?php
2
declare(strict_types=1);
3

4
namespace Ody\Core\Middleware;
5

6
use Psr\Http\Message\ResponseInterface;
7
use Psr\Http\Message\ServerRequestInterface;
8
use Psr\Http\Server\MiddlewareInterface;
9
use Psr\Http\Server\RequestHandlerInterface;
10
use RuntimeException;
11
use Ody\Core\Exception\HttpMethodNotAllowedException;
12
use Ody\Core\Exception\HttpNotFoundException;
13
use Ody\Core\Interfaces\RouteParserInterface;
14
use Ody\Core\Interfaces\RouteResolverInterface;
15
use Ody\Core\Routing\RouteContext;
16
use Ody\Core\Routing\RoutingResults;
17

18
class RoutingMiddleware implements MiddlewareInterface
19
{
20
    protected RouteResolverInterface $routeResolver;
21

22
    protected RouteParserInterface $routeParser;
23

24
    public function __construct(RouteResolverInterface $routeResolver, RouteParserInterface $routeParser)
44✔
25
    {
26
        $this->routeResolver = $routeResolver;
44✔
27
        $this->routeParser = $routeParser;
44✔
28
    }
29

30
    /**
31
     * @throws HttpNotFoundException
32
     * @throws HttpMethodNotAllowedException
33
     * @throws RuntimeException
34
     */
35
    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
×
36
    {
37
        $request = $this->performRouting($request);
×
38
        return $handler->handle($request);
×
39
    }
40

41
    /**
42
     * Perform routing
43
     *
44
     * @param  ServerRequestInterface $request PSR7 Server Request
45
     *
46
     * @throws HttpNotFoundException
47
     * @throws HttpMethodNotAllowedException
48
     * @throws RuntimeException
49
     */
50
    public function performRouting(ServerRequestInterface $request): ServerRequestInterface
43✔
51
    {
52
        $request = $request->withAttribute(RouteContext::ROUTE_PARSER, $this->routeParser);
43✔
53

54
        $routingResults = $this->resolveRoutingResultsFromRequest($request);
43✔
55
        $routeStatus = $routingResults->getRouteStatus();
43✔
56

57
        $request = $request->withAttribute(RouteContext::ROUTING_RESULTS, $routingResults);
43✔
58

59
        switch ($routeStatus) {
60
            case RoutingResults::FOUND:
43✔
61
                $routeArguments = $routingResults->getRouteArguments();
41✔
62
                $routeIdentifier = $routingResults->getRouteIdentifier() ?? '';
41✔
63
                $route = $this->routeResolver
41✔
64
                    ->resolveRoute($routeIdentifier)
41✔
65
                    ->prepare($routeArguments);
41✔
66
                return $request->withAttribute(RouteContext::ROUTE, $route);
41✔
67

68
            case RoutingResults::NOT_FOUND:
2✔
69
                throw new HttpNotFoundException($request);
1✔
70

71
            case RoutingResults::METHOD_NOT_ALLOWED:
1✔
72
                $exception = new HttpMethodNotAllowedException($request);
1✔
73
                $exception->setAllowedMethods($routingResults->getAllowedMethods());
1✔
74
                throw $exception;
1✔
75

76
            default:
77
                throw new RuntimeException('An unexpected error occurred while performing routing.');
×
78
        }
79
    }
80

81
    /**
82
     * Resolves the route from the given request
83
     */
84
    protected function resolveRoutingResultsFromRequest(ServerRequestInterface $request): RoutingResults
43✔
85
    {
86
        return $this->routeResolver->computeRoutingResults(
43✔
87
            $request->getUri()->getPath(),
43✔
88
            $request->getMethod()
43✔
89
        );
43✔
90
    }
91
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc