Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
AddHeaders
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
2 / 2
4
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 process
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
3
1<?php
2
3declare(strict_types=1);
4
5namespace Engelsystem\Middleware;
6
7use Engelsystem\Config\Config;
8use Psr\Http\Message\ResponseInterface;
9use Psr\Http\Message\ServerRequestInterface;
10use Psr\Http\Server\MiddlewareInterface;
11use Psr\Http\Server\RequestHandlerInterface;
12
13class AddHeaders implements MiddlewareInterface
14{
15    public function __construct(protected Config $config)
16    {
17    }
18
19    /**
20     * Process an incoming server request and setting the locale if required
21     */
22    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
23    {
24        $response = $handler->handle($request);
25        if (!$this->config->get('add_headers', true)) {
26            return $response;
27        }
28
29        $headers = $this->config->get('headers', []);
30
31        foreach ($headers as $name => $value) {
32            $response = $response->withAddedHeader($name, $value);
33        }
34
35        return $response;
36    }
37}