Passed
Pull Request — master (#54)
by Vincent
06:15
created

MetadataNormalizer::hasCacheableSupportsMethod()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
ccs 0
cts 2
cp 0
rs 10
cc 1
nc 1
nop 0
crap 2
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\Normalizer;
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