CwaResourceLoader::isCoreClassName()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 2
c 0
b 0
f 0
nc 2
nop 1
dl 0
loc 4
ccs 0
cts 3
cp 0
crap 6
rs 10
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 Silverback\ApiComponentsBundle\Entity\Core\AbstractComponent;
17
use Silverback\ApiComponentsBundle\Entity\Core\AbstractPageData;
18
use Symfony\Component\Serializer\Mapping\ClassMetadataInterface;
19
use Symfony\Component\Serializer\Mapping\Loader\LoaderInterface;
20
21
/**
22
 * Adds {CLASS}:component serialization groups on component entities. This will allow cascaded persists by default and every property accessible by read/write unless a serialization group has been specifically defined on a property.
23
 *
24
 * @author Daniel West <[email protected]>
25
 */
26
final class CwaResourceLoader implements LoaderInterface
27
{
28
    public const GROUP_NAME = 'cwa_resource';
29
30
    /**
31
     * {@inheritdoc}
32
     */
33
    public function loadClassMetadata(ClassMetadataInterface $classMetadata): bool
34
    {
35
        $reflectionClass = $classMetadata->getReflectionClass();
36
        $reflectionClassName = $reflectionClass->getName();
37
        if (
38
            !$this->isCoreClassName($reflectionClassName)
39
            && !$reflectionClass->isSubclassOf(AbstractComponent::class)
40
            && !$reflectionClass->isSubclassOf(AbstractPageData::class)) {
41
            return true;
42
        }
43
44
        $allAttributesMetadata = $classMetadata->getAttributesMetadata();
45
        $shortClassName = $reflectionClass->getShortName();
46
        $readGroup = sprintf('%s:%s:read', $shortClassName, self::GROUP_NAME);
47
        $writeGroup = sprintf('%s:%s:write', $shortClassName, self::GROUP_NAME);
48
49
        foreach ($allAttributesMetadata as $attributeMetadatum) {
50
            $name = $attributeMetadatum->getName();
51
            if ('id' === $name) {
52
                continue;
53
            }
54
55
            try {
56
                $reflectionProperty = $reflectionClass->getProperty($name);
57
                $className = $reflectionProperty->getDeclaringClass()->getName();
58
                $isCoreClassName = $this->isCoreClassName($className);
59
            } catch (\ReflectionException $e) {
60
                // may not be a property - could be a method
61
                $isCoreClassName = false;
62
            }
63
            if ($isCoreClassName || empty($attributeMetadatum->getGroups())) {
64
                $attributeMetadatum->addGroup($readGroup);
65
                $attributeMetadatum->addGroup($writeGroup);
66
            }
67
        }
68
69
        return true;
70
    }
71
72
    private function isCoreClassName(string $className): bool
73
    {
74
        return AbstractComponent::class === $className
75
            || AbstractPageData::class === $className;
76
    }
77
}
78