HeadersRequestFormatter   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 10
eloc 26
dl 0
loc 58
ccs 26
cts 26
cp 1
rs 10
c 1
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B getValue() 0 37 8
A getPrefix() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 *  This file is part of the Micro framework package.
7
 *
8
 *  (c) Stanislau Komar <[email protected]>
9
 *
10
 *  For the full copyright and license information, please view the LICENSE
11
 *  file that was distributed with this source code.
12
 */
13
14
namespace Micro\Plugin\Http\Business\Logger\Formatter\Format;
15
16
use Symfony\Component\HttpFoundation\Request;
17
use Symfony\Component\HttpFoundation\Response;
18
19
/**
20
 * @author Stanislau Komar <[email protected]>
21
 */
22
class HeadersRequestFormatter extends AbstractHeaderFormatter
23
{
24
    /**
25
     * @var string[]
26
     */
27
    private readonly array $securedHeadersList;
28
29
    /**
30
     * @param string[] $securedHeadersList
31
     */
32 3
    public function __construct(
33
        array $securedHeadersList
34
    ) {
35 3
        $this->securedHeadersList = array_map('mb_strtolower', $securedHeadersList);
0 ignored issues
show
Bug introduced by
The property securedHeadersList is declared read-only in Micro\Plugin\Http\Busine...HeadersRequestFormatter.
Loading history...
36
    }
37
38 3
    protected function getPrefix(): string
39
    {
40 3
        return 'request';
41
    }
42
43 3
    protected function getValue(Request $request, ?Response $response, string $matchedVar): string
44
    {
45 3
        if (!$matchedVar) {
46 1
            return '';
47
        }
48 2
        $isSingleVar = true;
49 2
        if ('*' === $matchedVar) {
50 1
            $headersArray = $request->headers->all();
51 1
            $isSingleVar = false;
52
        } else {
53 1
            $tmpHeaderValue = $request->headers->get($matchedVar);
54
55 1
            $headersArray = [$matchedVar => $tmpHeaderValue];
56
        }
57
58 2
        $headersArray = array_change_key_case($headersArray);
59 2
        foreach ($this->securedHeadersList as $securedHeader) {
60 2
            if (!\array_key_exists($securedHeader, $headersArray)) {
61 1
                continue;
62
            }
63
            /** @var string[] $securedHeaderValue */
64 1
            $securedHeaderValue = $headersArray[$securedHeader];
65
66 1
            $headersArray[$securedHeader] = array_map(fn (mixed $value) => '** Secured **', $securedHeaderValue);
0 ignored issues
show
Unused Code introduced by
The parameter $value is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

66
            $headersArray[$securedHeader] = array_map(fn (/** @scrutinizer ignore-unused */ mixed $value) => '** Secured **', $securedHeaderValue);

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
67
        }
68
69 2
        if ($isSingleVar) {
70 1
            $headersArray = array_values($headersArray);
71 1
            if (1 === \count($headersArray)) {
72 1
                $tmpValue = $headersArray[0];
73 1
                if (!\is_array($tmpValue)) {
74 1
                    return (string) $tmpValue;
75
                }
76
            }
77
        }
78
79 1
        return (string) json_encode($headersArray);
80
    }
81
}
82