Passed
Push — master ( 3ec415...9f2f47 )
by Daniel
16:25
created

UploadableLoader   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Test Coverage

Coverage 20%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 22
dl 0
loc 43
ccs 4
cts 20
cp 0.2
rs 10
c 1
b 0
f 0
wmc 7

2 Methods

Rating   Name   Duplication   Size   Complexity  
A loadClassMetadata() 0 27 6
A __construct() 0 4 1
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 7
    public function __construct(AnnotationReader $reader, UploadableAnnotationReader $annotationReader)
35
    {
36 7
        $this->reader = $reader;
37 7
        $this->annotationReader = $annotationReader;
38 7
    }
39
40
    /**
41
     * {@inheritdoc}
42
     */
43
    public function loadClassMetadata(ClassMetadataInterface $classMetadata): bool
44
    {
45
        $reflectionClass = $classMetadata->getReflectionClass();
46
        /** @var Uploadable $configuration */
47
        if (!$configuration = $this->reader->getClassAnnotation($reflectionClass, Uploadable::class)) {
0 ignored issues
show
Unused Code introduced by
The assignment to $configuration is dead and can be removed.
Loading history...
48
            return true;
49
        }
50
51
        $properties = $reflectionClass->getProperties();
52
53
        $allAttributesMetadata = $classMetadata->getAttributesMetadata();
54
        $shortClassName = $reflectionClass->getShortName();
55
        $readGroup = sprintf('%s:%s:read', $shortClassName, self::GROUP_NAME);
56
        $writeGroup = sprintf('%s:%s:write', $shortClassName, self::GROUP_NAME);
57
58
        foreach ($properties as $property) {
59
            if (
60
                $this->annotationReader->isFieldConfigured($property) &&
61
                ($attributeMetadata = ($allAttributesMetadata[$property->getName()] ?? null)) &&
62
                empty($attributeMetadata->getGroups())
63
            ) {
64
                $attributeMetadata->addGroup($readGroup);
65
                $attributeMetadata->addGroup($writeGroup);
66
            }
67
        }
68
69
        return true;
70
    }
71
}
72