Passed
Push — main ( 9ed449...256357 )
by Daniel
16:48
created

UploadableLoader   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Test Coverage

Coverage 15.79%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 22
dl 0
loc 41
ccs 3
cts 19
cp 0.1579
rs 10
c 2
b 0
f 0
wmc 7

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A loadClassMetadata() 0 25 6
1
<?php
2
3
/*
4
 * This file is part of the Silverback API Components 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\ApiComponentsBundle\Serializer\MappingLoader;
15
16
use Doctrine\Common\Annotations\AnnotationReader;
17
use Silverback\ApiComponentsBundle\Annotation\Uploadable;
18
use Silverback\ApiComponentsBundle\AnnotationReader\UploadableAnnotationReader;
19
use Symfony\Component\Serializer\Mapping\ClassMetadataInterface;
20
use Symfony\Component\Serializer\Mapping\Loader\LoaderInterface;
21
22
/**
23
 * Adds {CLASS}:timestamped serialization group on {CLASS}.createdAt and {CLASS}.updatedAt for Timestamped entities.
24
 *
25
 * @author Daniel West <[email protected]>
26
 */
27
final class UploadableLoader implements LoaderInterface
28
{
29
    public const GROUP_NAME = 'uploadable';
30
31
    private AnnotationReader $reader;
32
    private UploadableAnnotationReader $annotationReader;
33
34 3
    public function __construct(AnnotationReader $reader, UploadableAnnotationReader $annotationReader)
35
    {
36 3
        $this->reader = $reader;
37 3
        $this->annotationReader = $annotationReader;
38
    }
39
40
    /**
41
     * {@inheritdoc}
42
     */
43
    public function loadClassMetadata(ClassMetadataInterface $classMetadata): bool
44
    {
45
        $reflectionClass = $classMetadata->getReflectionClass();
46
        if (!$this->reader->getClassAnnotation($reflectionClass, Uploadable::class)) {
47
            return true;
48
        }
49
50
        $properties = $reflectionClass->getProperties();
51
        $allAttributesMetadata = $classMetadata->getAttributesMetadata();
52
        $shortClassName = $reflectionClass->getShortName();
53
        $readGroup = sprintf('%s:%s:read', $shortClassName, self::GROUP_NAME);
54
        $writeGroup = sprintf('%s:%s:write', $shortClassName, self::GROUP_NAME);
55
56
        foreach ($properties as $property) {
57
            if (
58
                $this->annotationReader->isFieldConfigured($property) &&
59
                ($attributeMetadata = ($allAttributesMetadata[$property->getName()] ?? null)) &&
60
                empty($attributeMetadata->getGroups())
61
            ) {
62
                $attributeMetadata->addGroup($readGroup);
63
                $attributeMetadata->addGroup($writeGroup);
64
            }
65
        }
66
67
        return true;
68
    }
69
}
70