Passed
Push — feature/publishable ( ce6968...c26268 )
by Daniel
13:37
created

MetadataNormalizer   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
eloc 12
c 1
b 0
f 0
dl 0
loc 31
ccs 0
cts 12
cp 0
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A hasCacheableSupportsMethod() 0 3 1
A supportsNormalization() 0 3 2
A __construct() 0 3 1
A normalize() 0 7 1
1
<?php
2
3
/*
4
 * This file is part of the Silverback API Component Bundle Project
5
 *
6
 * (c) Daniel West <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace Silverback\ApiComponentBundle\Serializer;
15
16
use Symfony\Component\Serializer\Normalizer\CacheableSupportsMethodInterface;
17
use Symfony\Component\Serializer\Normalizer\ContextAwareNormalizerInterface;
18
use Symfony\Component\Serializer\Normalizer\NormalizerAwareInterface;
19
use Symfony\Component\Serializer\Normalizer\NormalizerAwareTrait;
20
21
/**
22
 * @author Daniel West <[email protected]>
23
 */
24
class MetadataNormalizer implements ContextAwareNormalizerInterface, CacheableSupportsMethodInterface, NormalizerAwareInterface
25
{
26
    use NormalizerAwareTrait;
27
28
    public const METADATA_CONTEXT = 'silverback_api_component_bundle_metadata';
29
    private const ALREADY_CALLED = 'METADATA_NORMALIZER_ALREADY_CALLED';
30
31
    private string $metadataKey;
32
33
    public function __construct(string $metadataKey)
34
    {
35
        $this->metadataKey = $metadataKey;
36
    }
37
38
    public function hasCacheableSupportsMethod(): bool
39
    {
40
        return false;
41
    }
42
43
    public function supportsNormalization($data, $format = null, array $context = []): bool
44
    {
45
        return !isset($context[self::ALREADY_CALLED]) && isset($context[self::METADATA_CONTEXT]);
46
    }
47
48
    public function normalize($object, $format = null, array $context = [])
49
    {
50
        $context[self::ALREADY_CALLED] = true;
51
        $data = $this->normalizer->normalize($object, $format, $context);
52
        $data[$this->metadataKey] = (array) $context[self::METADATA_CONTEXT];
53
54
        return $data;
55
    }
56
}
57