ApiePrimaryKeyNormalizer::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 5
c 1
b 0
f 0
nc 1
nop 5
dl 0
loc 12
rs 10
1
<?php
2
3
namespace Apie\PrimaryKeyPlugin\Normalizers;
4
5
use Apie\Core\ApiResourceMetadataFactory;
6
use Apie\Core\ClassResourceConverter;
7
use Apie\Core\IdentifierExtractor;
8
use Apie\Core\PluginInterfaces\FrameworkConnectionInterface;
9
use Apie\Core\Resources\ApiResourcesInterface;
10
use Apie\CorePlugin\ResourceSerializers\SymfonySerializerAdapter;
0 ignored issues
show
Bug introduced by
The type Apie\CorePlugin\Resource...ymfonySerializerAdapter 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...
11
use Apie\PrimaryKeyPlugin\ValueObjects\PrimaryKeyReference;
12
use Symfony\Component\Serializer\Normalizer\ContextAwareNormalizerInterface;
13
use Symfony\Component\Serializer\SerializerAwareInterface;
14
use Symfony\Component\Serializer\SerializerAwareTrait;
15
16
/**
17
 * If a resource has an other api resource as a child, this class will map it as a url and not try to hydrate it as an url.
18
 */
19
class ApiePrimaryKeyNormalizer implements ContextAwareNormalizerInterface, SerializerAwareInterface
20
{
21
    use SerializerAwareTrait;
22
23
    /**
24
     * @var ApiResourcesInterface
25
     */
26
    private $apiResources;
27
28
    /**
29
     * @var IdentifierExtractor
30
     */
31
    private $identifierExtractor;
32
33
    /**
34
     * @var ApiResourceMetadataFactory
35
     */
36
    private $metadataFactory;
37
38
    /**
39
     * @var ClassResourceConverter
40
     */
41
    private $converter;
42
43
    /**
44
     * @var FrameworkConnectionInterface
45
     */
46
    private $frameworkConnection;
47
48
    public function __construct(
49
        ApiResourcesInterface $apiResources,
50
        IdentifierExtractor $identifierExtractor,
51
        ApiResourceMetadataFactory $metadataFactory,
52
        ClassResourceConverter $converter,
53
        FrameworkConnectionInterface $frameworkConnection
54
    ) {
55
        $this->apiResources = $apiResources;
56
        $this->identifierExtractor = $identifierExtractor;
57
        $this->metadataFactory = $metadataFactory;
58
        $this->converter = $converter;
59
        $this->frameworkConnection = $frameworkConnection;
60
    }
61
    /**
62
     * {@inheritdoc}
63
     */
64
    public function supportsNormalization($data, $format = null, array $context = [])
65
    {
66
        if ($format === SymfonySerializerAdapter::INTERNAL_FOR_DATALAYER || empty($context['object_hierarchy']) || !empty($context['disable_pk_normalize'])) {
67
            return false;
68
        }
69
        foreach ($this->apiResources->getApiResources() as $apiResource) {
70
            // if someone is really stupid to add this as an API resource....
71
            if ($apiResource === PrimaryKeyReference::class) {
72
                continue;
73
            }
74
            $resourceContext = $this->metadataFactory->getMetadata($apiResource)->getContext();
75
            $identifier = $this->identifierExtractor->getIdentifierKeyOfClass($apiResource, $resourceContext);
76
            if (null !== $identifier && is_a($data, $apiResource)) {
77
                return true;
78
            }
79
        }
80
        return false;
81
    }
82
83
    /**
84
     * {@inheritdoc}
85
     */
86
    public function normalize($object, $format = null, array $context = [])
87
    {
88
        $metadata = $this->metadataFactory->getMetadata($object);
89
        $resourceContext = $this->metadataFactory->getMetadata($object)->getContext();
90
        $identifierValue = $this->identifierExtractor->getIdentifierValue($object, $resourceContext);
91
        return $this->serializer->normalize(
92
            new PrimaryKeyReference(
93
                $metadata,
94
                $this->frameworkConnection->getUrlForResource($object),
0 ignored issues
show
Bug introduced by
It seems like $this->frameworkConnecti...UrlForResource($object) can also be of type null; however, parameter $resourceUrl of Apie\PrimaryKeyPlugin\Va...eference::__construct() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

94
                /** @scrutinizer ignore-type */ $this->frameworkConnection->getUrlForResource($object),
Loading history...
95
                $identifierValue
96
            ),
97
            $format,
98
            $context
99
        );
100
    }
101
}
102