Passed
Pull Request — master (#52)
by Daniel
06:09
created

UploadableNormalizer   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 10
eloc 23
c 1
b 0
f 0
dl 0
loc 60
ccs 0
cts 22
cp 0
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A hasCacheableSupportsMethod() 0 3 1
A supportsDenormalization() 0 3 2
A __construct() 0 3 1
A denormalize() 0 31 6
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\AnnotationReader\UploadableAnnotationReader;
17
use Silverback\ApiComponentBundle\Model\Uploadable\Base64EncodedFile;
18
use Silverback\ApiComponentBundle\Model\Uploadable\UploadedBase64EncodedFile;
19
use Symfony\Component\HttpFoundation\File\Exception\FileException;
20
use Symfony\Component\Serializer\Exception\NotNormalizableValueException;
21
use Symfony\Component\Serializer\Normalizer\CacheableSupportsMethodInterface;
22
use Symfony\Component\Serializer\Normalizer\ContextAwareDenormalizerInterface;
23
use Symfony\Component\Serializer\Normalizer\DenormalizerAwareInterface;
24
use Symfony\Component\Serializer\Normalizer\DenormalizerAwareTrait;
25
26
/**
27
 * @author Vincent Chalamon <[email protected]>
28
 */
29
final class UploadableNormalizer implements CacheableSupportsMethodInterface, ContextAwareDenormalizerInterface, DenormalizerAwareInterface
30
{
31
    use DenormalizerAwareTrait;
32
33
    private const ALREADY_CALLED = 'UPLOADABLE_NORMALIZER_ALREADY_CALLED';
34
35
    private UploadableAnnotationReader $uploadableHelper;
36
37
    public function __construct(UploadableAnnotationReader $uploadableHelper)
38
    {
39
        $this->uploadableHelper = $uploadableHelper;
40
    }
41
42
    /**
43
     * {@inheritdoc}
44
     */
45
    public function denormalize($data, $type, $format = null, array $context = [])
46
    {
47
        $context[self::ALREADY_CALLED] = true;
48
49
        foreach ($data as $fieldName => $value) {
50
            try {
51
                $reflectionProperty = new \ReflectionProperty($type, $fieldName);
52
            } catch (\ReflectionException $exception) {
53
                // Property does not exist on class: just ignore it.
54
                continue;
55
            }
56
57
            // Property is not an UploadableField: just ignore it.
58
            if (!$this->uploadableHelper->isFieldConfigured($reflectionProperty)) {
59
                continue;
60
            }
61
62
            // Value is empty: set it to null.
63
            if (empty($value)) {
64
                $data[$fieldName] = null;
65
                continue;
66
            }
67
68
            try {
69
                $data[$fieldName] = new UploadedBase64EncodedFile(new Base64EncodedFile($value), 'uploaded_name');
70
            } catch (FileException $exception) {
71
                throw new NotNormalizableValueException($exception->getMessage());
72
            }
73
        }
74
75
        return $this->denormalizer->denormalize($data, $type, $format, $context);
76
    }
77
78
    /**
79
     * {@inheritdoc}
80
     */
81
    public function supportsDenormalization($data, $type, $format = null, array $context = []): bool
82
    {
83
        return !isset($context[self::ALREADY_CALLED]) && $this->uploadableHelper->isConfigured($type);
84
    }
85
86
    public function hasCacheableSupportsMethod(): bool
87
    {
88
        return false;
89
    }
90
}
91