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

klinge / sl-webapp / 18131712951

30 Sep 2025 01:33PM UTC coverage: 63.01% (+6.4%) from 56.619%
18131712951

push

github

web-flow
Merge pull request #103 from klinge/klinge/issue93

Refactor middleware to follow PSR-15

78 of 88 new or added lines in 9 files covered. (88.64%)

39 existing lines in 1 file now uncovered.

1298 of 2060 relevant lines covered (63.01%)

3.38 hits per line

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

68.18
/App/Middleware/ApplicationHandler.php
1
<?php
2

3
declare(strict_types=1);
4

5
namespace App\Middleware;
6

7
use App\Application;
8
use App\Utils\ResponseEmitter;
9
use App\Middleware\Contracts\RequestHandlerInterface;
10
use Psr\Http\Message\ResponseInterface;
11
use Psr\Http\Message\ServerRequestInterface;
12
use Laminas\Diactoros\Response\HtmlResponse;
13
use AltoRouter;
14
use Exception;
15

16
class ApplicationHandler implements RequestHandlerInterface
17
{
18
    public function __construct(
19
        private Application $app,
20
        private AltoRouter $router
21
    ) {
22
    }
19✔
23

24
    public function handle(ServerRequestInterface $request): ResponseInterface
25
    {
26
        $match = $this->router->match();
4✔
27

28
        if ($match === false) {
4✔
29
            return new HtmlResponse("404 - Ingen mappning för denna url. Och dessutom borde detta aldrig kunna hända!!", 404);
1✔
30
        }
31

32
        return $this->dispatch($match, $request);
3✔
33
    }
34

35
    private function dispatch(array $match, ServerRequestInterface $request): ResponseInterface
36
    {
37
        if (is_string($match['target']) && strpos($match['target'], "#") !== false) {
3✔
38
            list($controller, $action) = explode('#', $match['target']);
1✔
39
            $params = $match['params'];
1✔
40

41
            $controllerClass = "App\\Controllers\\{$controller}";
1✔
42
            if (!class_exists($controllerClass)) {
1✔
43
                throw new Exception("Controller class {$controllerClass} not found");
1✔
44
            }
45

NEW
46
            if (method_exists($controllerClass, $action)) {
×
NEW
47
                $controllerInstance = $this->app->getContainer()->get($controllerClass);
×
NEW
48
                $response = $controllerInstance->{$action}($params);
×
49

50
                // Ensure we always return a ResponseInterface
NEW
51
                if ($response instanceof ResponseInterface) {
×
NEW
52
                    return $response;
×
53
                } else {
54
                    // Handle legacy controllers that might not return ResponseInterface
NEW
55
                    return new HtmlResponse($response ?? '', 200);
×
56
                }
57
            } else {
NEW
58
                throw new Exception("Method {$action} not found in {$controllerClass}");
×
59
            }
60
        } elseif (is_callable($match['target'])) {
2✔
61
            $result = call_user_func_array($match['target'], [$request, ...$match['params']]);
1✔
62
            return $result instanceof ResponseInterface ? $result : new HtmlResponse($result ?? '', 200);
1✔
63
        } else {
64
            throw new Exception('Invalid route target');
1✔
65
        }
66
    }
67
}
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