Passed
Pull Request — master (#52)
by Vincent
23:37
created

FileNormalizer::__construct()   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 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
ccs 0
cts 2
cp 0
rs 10
cc 1
nc 1
nop 1
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 Silverback\ApiComponentBundle\Helper\FileHelper;
17
use Symfony\Component\Serializer\Normalizer\CacheableSupportsMethodInterface;
18
use Symfony\Component\Serializer\Normalizer\ContextAwareDenormalizerInterface;
19
use Symfony\Component\Serializer\Normalizer\ContextAwareNormalizerInterface;
20
use Symfony\Component\Serializer\Normalizer\DenormalizerAwareInterface;
21
use Symfony\Component\Serializer\Normalizer\DenormalizerAwareTrait;
22
use Symfony\Component\Serializer\Normalizer\NormalizerAwareInterface;
23
use Symfony\Component\Serializer\Normalizer\NormalizerAwareTrait;
24
25
/**
26
 * Prevent serialization and deserialzation of the internal file path.
27
 *
28
 * @author Daniel West <[email protected]>
29
 */
30
class FileNormalizer implements ContextAwareNormalizerInterface, CacheableSupportsMethodInterface, NormalizerAwareInterface, ContextAwareDenormalizerInterface, DenormalizerAwareInterface
31
{
32
    use NormalizerAwareTrait;
33
    use DenormalizerAwareTrait;
34
35
    private const ALREADY_CALLED = 'FILE_NORMALIZER_ALREADY_CALLED';
36
37
    private FileHelper $fileHelper;
38
39
    public function __construct(FileHelper $fileHelper)
40
    {
41
        $this->fileHelper = $fileHelper;
42
    }
43
44
    public function hasCacheableSupportsMethod(): bool
45
    {
46
        return false;
47
    }
48
49
    public function supportsDenormalization($data, $type, $format = null, array $context = [])
50
    {
51
        return !isset($context[self::ALREADY_CALLED]) && $this->fileHelper->isConfigured($type);
52
    }
53
54
    public function supportsNormalization($data, $format = null, array $context = [])
55
    {
56
        return !isset($context[self::ALREADY_CALLED]) &&
57
            \is_object($data) &&
58
            !$data instanceof \Traversable &&
59
            $this->fileHelper->isConfigured($data);
60
    }
61
62
    public function denormalize($data, $type, $format = null, array $context = [])
63
    {
64
        $context[self::ALREADY_CALLED] = true;
65
        $configuration = $this->fileHelper->getConfiguration($type);
66
67
        unset($data[$configuration->filePathFieldName]);
68
69
        return $this->denormalizer->denormalize($data, $type, $format, $context);
70
    }
71
72
    public function normalize($object, $format = null, array $context = [])
73
    {
74
        $context[self::ALREADY_CALLED] = true;
75
        $configuration = $this->fileHelper->getConfiguration($object);
76
        $data = $this->normalizer->normalize($object, $format, $context);
77
78
        unset($data[$configuration->filePathFieldName]);
79
80
        return $data;
81
    }
82
}
83