Completed
Branch master (e1d486)
by Pieter
02:44
created

ApiResourceLinkSchemaBuilder   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 10
c 1
b 0
f 0
dl 0
loc 34
rs 10
wmc 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __invoke() 0 16 4
A __construct() 0 3 1
1
<?php
2
3
4
namespace W2w\Lib\Apie\Plugins\PrimaryKey\Schema;
5
6
7
use erasys\OpenApi\Spec\v3\Schema;
8
use W2w\Lib\Apie\Core\ClassResourceConverter;
9
use W2w\Lib\Apie\OpenApiSchema\SchemaGenerator;
10
use W2w\Lib\Apie\PluginInterfaces\DynamicSchemaInterface;
11
use W2w\Lib\Apie\Plugins\Core\Normalizers\ApieObjectNormalizer;
12
use W2w\Lib\Apie\Plugins\Core\Normalizers\ContextualNormalizer;
13
14
class ApiResourceLinkSchemaBuilder implements DynamicSchemaInterface
15
{
16
    /**
17
     * @var ClassResourceConverter
18
     */
19
    private $classResourceConverter;
20
21
    public function __construct(ClassResourceConverter $classResourceConverter)
22
    {
23
        $this->classResourceConverter = $classResourceConverter;
24
    }
25
    /**
26
     * @param string $resourceClass
27
     * @param string $operation
28
     * @param array $groups
29
     * @param int $recursion
30
     * @param SchemaGenerator $generator
31
     */
32
    public function __invoke(
33
        string $resourceClass,
34
        string $operation,
35
        array $groups,
36
        int $recursion,
37
        SchemaGenerator $generator
38
    ) {
39
        if (!ContextualNormalizer::isNormalizerEnabled(ApieObjectNormalizer::class) && $recursion > 0 && $operation === 'get') {
40
            return new Schema([
41
                'type' => 'string',
42
                'format' => 'path',
43
                'nullable' => true,
44
                'example' => '/' . $this->classResourceConverter->normalize($resourceClass) . '/12345',
45
            ]);
46
        }
47
        return $generator->createSchema($resourceClass, $operation, $groups);
48
    }
49
}
50