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

chitoku-k / HomoChecker / 24913356214

24 Apr 2026 09:46PM UTC coverage: 99.767% (-0.2%) from 100.0%
24913356214

push

github

web-flow
Bump phpunit/phpunit from 13.1.6 to 13.1.7 (#3235)

Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>

855 of 857 relevant lines covered (99.77%)

9.27 hits per line

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

94.12
/api/src/Middleware/MetricsMiddleware.php
1
<?php
2
declare(strict_types=1);
3

4
namespace HomoChecker\Middleware;
5

6
use HomoChecker\Http\ErrorResponse;
7
use Prometheus\Summary;
8
use Psr\Http\Message\ResponseInterface;
9
use Psr\Http\Message\ServerRequestInterface;
10
use Psr\Http\Server\MiddlewareInterface;
11
use Psr\Http\Server\RequestHandlerInterface;
12
use Slim\Interfaces\RouteResolverInterface;
13
use Slim\Routing\RoutingResults;
14

15
final class MetricsMiddleware implements MiddlewareInterface
16
{
17
    public const string INFORMATIONAL = 'INFORMATIONAL';
18
    public const string SUCCESS = 'SUCCESS';
19
    public const string REDIRECTION = 'REDIRECTION';
20
    public const string CLIENT_ERROR = 'CLIENT_ERROR';
21
    public const string SERVER_ERROR = 'SERVER_ERROR';
22
    public const string UNKNOWN = 'UNKNOWN';
23

24
    /**
25
     * @param string[] $skipPaths Paths from which to skip collecting metrics.
26
     */
27
    public function __construct(
9✔
28
        private Summary $httpServerRequestsSeconds,
29
        private RouteResolverInterface $routeResolver,
30
        private array $skipPaths,
31
    ) {}
9✔
32

33
    #[\Override]
34
    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
9✔
35
    {
36
        if (collect($this->skipPaths)->contains($request->getUri()->getPath())) {
9✔
37
            return $handler->handle($request);
3✔
38
        }
39

40
        $begin = microtime(true);
6✔
41
        $response = $handler->handle($request);
6✔
42
        $end = microtime(true);
6✔
43

44
        $this->httpServerRequestsSeconds->observe($end - $begin, [
6✔
45
            'method' => $request->getMethod(),
6✔
46
            'uri' => $this->getRoutePattern($request),
6✔
47
            'exception' => $this->getException($response),
6✔
48
            'status' => (string) $response->getStatusCode(),
6✔
49
            'outcome' => $this->getOutcome($response->getStatusCode()),
6✔
50
        ]);
6✔
51

52
        return $response;
6✔
53
    }
54

55
    private function getRoutePattern(ServerRequestInterface $request): string
6✔
56
    {
57
        $results = $this->routeResolver->computeRoutingResults($request->getUri()->getPath(), $request->getMethod());
6✔
58
        if ($results->getRouteStatus() !== RoutingResults::FOUND || !$id = $results->getRouteIdentifier()) {
6✔
59
            return '/**';
3✔
60
        }
61

62
        return $this->routeResolver->resolveRoute($id)->getPattern();
3✔
63
    }
64

65
    private function getOutcome(int $status): string
6✔
66
    {
67
        return match (true) {
×
68
            $status >= 100 && $status < 200 => self::INFORMATIONAL,
6✔
69
            $status >= 200 && $status < 300 => self::SUCCESS,
6✔
70
            $status >= 300 && $status < 400 => self::REDIRECTION,
6✔
71
            $status >= 400 && $status < 500 => self::CLIENT_ERROR,
6✔
72
            $status >= 500 && $status < 600 => self::SERVER_ERROR,
3✔
73
            default => self::UNKNOWN,
6✔
74
        };
×
75
    }
76

77
    private function getException(ResponseInterface $response): string
6✔
78
    {
79
        if ($response instanceof ErrorResponse && $response->getException()) {
6✔
80
            return $response->getException()::class;
3✔
81
        }
82

83
        return 'None';
3✔
84
    }
85
}
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