Completed
Branch v4 (4e54dd)
by Pieter
03:26
created

PrimaryKeyPlugin::getBaseUrl()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 6
rs 10
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\OpenApiSchema\Factories\SchemaFactory;
10
use W2w\Lib\Apie\PluginInterfaces\ApieAwareInterface;
11
use W2w\Lib\Apie\PluginInterfaces\ApieAwareTrait;
12
use W2w\Lib\Apie\PluginInterfaces\NormalizerProviderInterface;
13
use W2w\Lib\Apie\PluginInterfaces\SchemaProviderInterface;
14
use W2w\Lib\Apie\Plugins\PrimaryKey\Normalizers\ApiePrimaryKeyNormalizer;
15
use W2w\Lib\Apie\Plugins\PrimaryKey\Normalizers\PrimaryKeyReferenceNormalizer;
16
use W2w\Lib\Apie\Plugins\PrimaryKey\Schema\ApiResourceLinkSchemaBuilder;
17
use W2w\Lib\Apie\Plugins\PrimaryKey\ValueObjects\PrimaryKeyReference;
18
19
/**
20
 * Core Apie plugin to map api resources to string urls for child objects.
21
 */
22
class PrimaryKeyPlugin implements NormalizerProviderInterface, ApieAwareInterface, SchemaProviderInterface
23
{
24
    use ApieAwareTrait;
25
26
    /**
27
     * {@inheritDoc}
28
     */
29
    public function getNormalizers(): array
30
    {
31
        $primaryKeyNormalizer = new ApiePrimaryKeyNormalizer(
32
            new ApiResources($this->getApie()->getResources()),
33
            $this->getApie()->getIdentifierExtractor(),
34
            $this->getApie()->getApiResourceMetadataFactory(),
35
            $this->getApie()->getClassResourceConverter(),
36
            $this->getApie()->getFrameworkConnection()
37
        );
38
        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...
39
            new PrimaryKeyReferenceNormalizer(),
40
            $primaryKeyNormalizer,
41
        ];
42
    }
43
44
    /**
45
     * {@inheritDoc}
46
     */
47
    public function getDefinedStaticData(): array
48
    {
49
        return [
50
            PrimaryKeyReference::class => SchemaFactory::createStringSchema('path'),
51
        ];
52
    }
53
54
    /**
55
     * {@inheritDoc}
56
     */
57
    public function getDynamicSchemaLogic(): array
58
    {
59
        $res = [];
60
        $identifierExtractor = $this->getApie()->getIdentifierExtractor();
61
        $builder = new ApiResourceLinkSchemaBuilder($this->getApie()->getFrameworkConnection());
62
        foreach ($this->getApie()->getResources() as $resource) {
63
            if (null !== $identifierExtractor->getIdentifierKeyOfClass($resource)) {
64
                $res[$resource] = $builder;
65
            }
66
        }
67
        return $res;
68
    }
69
}
70