EnumSchemaGenerator::__invoke()   A
last analyzed

Complexity

Conditions 4
Paths 2

Size

Total Lines 18
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 4
eloc 9
c 2
b 0
f 0
nc 2
nop 5
dl 0
loc 18
rs 9.9666
1
<?php
2
3
namespace W2w\Lib\ApieBenSampoEnumPlugin\Schema;
4
5
use erasys\OpenApi\Spec\v3\Schema;
6
use W2w\Lib\Apie\OpenApiSchema\OpenApiSchemaGenerator;
7
use W2w\Lib\Apie\OpenApiSchema\SchemaGenerator;
0 ignored issues
show
Bug introduced by
The type W2w\Lib\Apie\OpenApiSchema\SchemaGenerator 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...
8
use W2w\Lib\Apie\PluginInterfaces\DynamicSchemaInterface;
9
10
/**
11
 * Creates Schema for OpenAPI. For POST and PUT key and values can be used. For GET only keys are returned.
12
 */
13
class EnumSchemaGenerator implements DynamicSchemaInterface
14
{
15
    public function __invoke(
16
        string $resourceClass,
17
        string $operation,
18
        array $groups,
19
        int $recursion,
20
        OpenApiSchemaGenerator $generator
21
    ): ?Schema {
22
        $enumValues = array_values(array_unique($resourceClass::getValues()));
23
        if ($operation !== 'get') {
24
            $enumKeys = $resourceClass::getKeys();
25
            $enumValues =  array_values(array_unique(array_merge($enumValues, $enumKeys)));
26
        }
27
        return new Schema(
28
            [
29
                'type'    => 'string',
30
                'enum'    => $enumValues,
31
                'example' => reset($enumValues) ? : null,
32
                'default' => reset($enumValues) ? : null
33
            ]
34
        );
35
    }
36
}
37