Passed
Push — main ( aec17e...ff79ac )
by Dimitri
04:24
created

PerformanceMetrics   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 28
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 1 Features 0
Metric Value
eloc 12
c 1
b 1
f 0
dl 0
loc 28
ccs 0
cts 5
cp 0
rs 10
wmc 2

1 Method

Rating   Name   Duplication   Size   Complexity  
A process() 0 23 2
1
<?php
2
3
/**
4
 * This file is part of Blitz PHP framework.
5
 *
6
 * (c) 2022 Dimitri Sitchet Tomkeu <[email protected]>
7
 *
8
 * For the full copyright and license information, please view
9
 * the LICENSE file that was distributed with this source code.
10
 */
11
12
namespace BlitzPHP\Middlewares;
13
14
use BlitzPHP\Container\Services;
15
use Psr\Http\Message\ResponseInterface;
16
use Psr\Http\Message\ServerRequestInterface;
17
use Psr\Http\Server\MiddlewareInterface;
18
use Psr\Http\Server\RequestHandlerInterface;
19
20
class PerformanceMetrics implements MiddlewareInterface
21
{
22
    /**
23
     * Remplace les balises de mesures de performance
24
     */
25
    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
26
    {
27
        $response = $handler->handle($request);
28
29
        if (! empty($body = $response->getBody()->getContents())) {
30
            $benchmark = Services::timer();
31
32
            $output = str_replace(
33
                [
34
                    '{elapsed_time}',
35
                    '{memory_usage}',
36
                ],
37
                [
38
                    (string) $benchmark->getElapsedTime('total_execution'),
39
                    number_format(memory_get_peak_usage() / 1024 / 1024, 3),
40
                ],
41
                $body
42
            );
43
44
            $response = $response->withBody(to_stream($output));
45
        }
46
47
        return $response;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $response could return the type Psr\Http\Message\MessageInterface which includes types incompatible with the type-hinted return Psr\Http\Message\ResponseInterface. Consider adding an additional type-check to rule them out.
Loading history...
48
    }
49
}
50