Completed
Push — master ( c5da41...e1d486 )
by Pieter
22s queued 14s
created

PrimaryKeyPlugin   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 26
c 1
b 0
f 0
dl 0
loc 63
rs 10
wmc 7

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getDefinedStaticData() 0 6 1
A getNormalizers() 0 12 1
A getBaseUrl() 0 6 2
A getDynamicSchemaLogic() 0 11 3
1
<?php
2
3
4
namespace W2w\Lib\Apie\Plugins\PrimaryKey;
5
6
use erasys\OpenApi\Spec\v3\Schema;
7
use W2w\Lib\Apie\Core\Resources\ApiResources;
8
use W2w\Lib\Apie\Exceptions\BadConfigurationException;
9
use W2w\Lib\Apie\PluginInterfaces\ApieAwareInterface;
10
use W2w\Lib\Apie\PluginInterfaces\ApieAwareTrait;
11
use W2w\Lib\Apie\PluginInterfaces\NormalizerProviderInterface;
12
use W2w\Lib\Apie\PluginInterfaces\SchemaProviderInterface;
13
use W2w\Lib\Apie\Plugins\PrimaryKey\Normalizers\ApiePrimaryKeyNormalizer;
14
use W2w\Lib\Apie\Plugins\PrimaryKey\Normalizers\PrimaryKeyReferenceNormalizer;
15
use W2w\Lib\Apie\Plugins\PrimaryKey\Schema\ApiResourceLinkSchemaBuilder;
16
use W2w\Lib\Apie\Plugins\PrimaryKey\ValueObjects\PrimaryKeyReference;
17
18
/**
19
 * Core Apie plugin to map api resources to string urls for child objects.
20
 */
21
class PrimaryKeyPlugin implements NormalizerProviderInterface, ApieAwareInterface, SchemaProviderInterface
22
{
23
    use ApieAwareTrait;
24
25
    /**
26
     * {@inheritDoc}
27
     */
28
    public function getNormalizers(): array
29
    {
30
        $primaryKeyNormalizer = new ApiePrimaryKeyNormalizer(
31
            new ApiResources($this->getApie()->getResources()),
32
            $this->getApie()->getIdentifierExtractor(),
33
            $this->getApie()->getApiResourceMetadataFactory(),
34
            $this->getApie()->getClassResourceConverter(),
35
            $this->getBaseUrl()
36
        );
37
        return [
0 ignored issues
show
introduced by
The expression return array(new W2w\Lib... $primaryKeyNormalizer) returns an array which contains values of type W2w\Lib\Apie\Plugins\Pri...piePrimaryKeyNormalizer which are incompatible with the return type Symfony\Component\Serial...r\DenormalizerInterface mandated by W2w\Lib\Apie\PluginInter...rface::getNormalizers().
Loading history...
38
            new PrimaryKeyReferenceNormalizer(),
39
            $primaryKeyNormalizer,
40
        ];
41
    }
42
43
    /**
44
     * Returns base url if one is set up.
45
     *
46
     * @return string
47
     */
48
    private function getBaseUrl(): string
49
    {
50
        try {
51
            return $this->getApie()->getBaseUrl();
52
        } catch (BadConfigurationException $exception) {
53
            return '';
54
        }
55
    }
56
57
    /**
58
     * {@inheritDoc}
59
     */
60
    public function getDefinedStaticData(): array
61
    {
62
        return [
63
            PrimaryKeyReference::class => new Schema([
64
                'type' => 'string',
65
                'format' => 'path',
66
            ]),
67
        ];
68
    }
69
70
    /**
71
     * {@inheritDoc}
72
     */
73
    public function getDynamicSchemaLogic(): array
74
    {
75
        $res = [];
76
        $identifierExtractor = $this->getApie()->getIdentifierExtractor();
77
        $builder = new ApiResourceLinkSchemaBuilder($this->getApie()->getClassResourceConverter());
78
        foreach ($this->getApie()->getResources() as $resource) {
79
            if (null !== $identifierExtractor->getIdentifierKeyOfClass($resource)) {
80
                $res[$resource] = $builder;
81
            }
82
        }
83
        return $res;
84
    }
85
}
86