OptionsRenderer   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 16
dl 0
loc 61
rs 10
c 1
b 0
f 0
ccs 18
cts 18
cp 1
wmc 8

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getAllows() 0 12 3
A __construct() 0 5 1
A getEntityBody() 0 8 2
A render() 0 9 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace BEAR\Resource;
6
7
use BEAR\Resource\Annotation\OptionsBody;
8
use Override;
9
use ReflectionClass;
10
use ReflectionMethod;
11
12
use function implode;
13
use function in_array;
14
use function json_encode;
15
use function strtoupper;
16
use function substr;
17
18
use const JSON_PRETTY_PRINT;
19
use const JSON_UNESCAPED_SLASHES;
20
use const PHP_EOL;
21
22 99
/**
23
 * RFC2616 OPTIONS method renderer
24 99
 *
25 99
 * Set resource request information to `headers` and `view` in ResourceObject.
26
 *
27
 * @see https://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html
28
 * @see /docs/options/README.md
29
 */
30 7
final class OptionsRenderer implements RenderInterface
31
{
32 7
    /** @SuppressWarnings(PHPMD.BooleanArgumentFlag) */
33 7
    public function __construct(
34 7
        private readonly OptionsMethods $optionsMethod,
35 7
        #[OptionsBody]
36 7
        private readonly bool $optionsBody = true,
37
    ) {
38 7
    }
39
40
    /**
41
     * {@inheritDoc}
42
     */
43
    #[Override]
44
    public function render(ResourceObject $ro)
45
    {
46
        $ro->headers['Content-Type'] = 'application/json';
47
        $allows = $this->getAllows((new ReflectionClass($ro))->getMethods());
48 7
        $ro->headers['Allow'] = implode(', ', $allows);
49
        $ro->view = $this->optionsBody ? (string) json_encode($this->getEntityBody($ro, $allows), JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES) . PHP_EOL : '';
50 7
51 7
        return $ro->view;
52 7
    }
53 7
54
    /**
55
     * Return allowed methods
56
     *
57 7
     * @param ReflectionMethod[] $methods
58
     *
59
     * @return string[]
60
     * @psalm-return list<string>
61
     */
62
    private function getAllows(array $methods): array
63 7
    {
64
        $allows = [];
65 7
        foreach ($methods as $method) {
66 7
            if (! in_array($method->name, ['onGet', 'onPost', 'onPut', 'onPatch', 'onDelete', 'onHead'], true)) {
67 7
                continue;
68
            }
69
70 7
            $allows[] = strtoupper(substr($method->name, 2));
71
        }
72
73
        return $allows;
74
    }
75
76
    /**
77
     * Return OPTIONS entity body
78
     *
79
     * @param list<string> $allows
0 ignored issues
show
Bug introduced by
The type BEAR\Resource\list was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
80
     *
81
     * @return array<string, array<array<mixed>|string>>
82
     */
83
    private function getEntityBody(ResourceObject $ro, array $allows): array
84
    {
85
        $mehtodList = [];
86
        foreach ($allows as $method) {
87
            $mehtodList[$method] = ($this->optionsMethod)($ro, $method);
88
        }
89
90
        return $mehtodList;
91
    }
92
}
93