JmsPropertyMapper::getEntityProperties()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 15
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 15
ccs 0
cts 11
cp 0
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 7
nc 3
nop 1
crap 12
1
<?php
2
3
namespace ScayTrase\Api\Cruds\Adaptors\JmsSerializer;
4
5
use JMS\Serializer\Metadata\PropertyMetadata;
6
use JMS\Serializer\Naming\PropertyNamingStrategyInterface;
7
use Metadata\ClassMetadata;
8
use Metadata\MetadataFactoryInterface;
9
use ScayTrase\Api\Cruds\Exception\MapperException;
10
use ScayTrase\Api\Cruds\PropertyMapperInterface;
11
12
final class JmsPropertyMapper implements PropertyMapperInterface
13
{
14
    /** @var MetadataFactoryInterface */
15
    private $factory;
16
    /** @var  PropertyNamingStrategyInterface */
17
    private $strategy;
18
19
    /**
20
     * JmsPropertyMapper constructor.
21
     *
22
     * @param MetadataFactoryInterface        $factory
23
     * @param PropertyNamingStrategyInterface $strategy
24
     */
25 5
    public function __construct(MetadataFactoryInterface $factory, PropertyNamingStrategyInterface $strategy)
26
    {
27 5
        $this->factory  = $factory;
28 5
        $this->strategy = $strategy;
29 5
    }
30
31
    /** {@inheritdoc} */
32 4
    public function getEntityProperty($className, $apiProperty)
33
    {
34 4
        $metadata = $this->getMetadata($className);
35
36 4
        foreach ($metadata->propertyMetadata as $propertyMetadata) {
37
            /** @var PropertyMetadata $propertyMetadata */
38 4
            if ($this->getPropertyName($propertyMetadata) === $apiProperty) {
39 4
                return $propertyMetadata->reflection ? $propertyMetadata->reflection->getName() : null;
40
            }
41
        }
42
43
        return null;
44
    }
45
46
    /** {@inheritdoc} */
47
    public function getApiProperty($className, $objectProperty)
48
    {
49
        $metadata = $this->getMetadata($className);
50
51
        if (!array_key_exists($objectProperty, $metadata->propertyMetadata)) {
52
            return null;
53
        }
54
55
        return $this->getPropertyName($metadata->propertyMetadata[$objectProperty]);
56
    }
57
58
    /** {@inheritdoc} */
59
    public function getApiProperties($className): array
60
    {
61
        $metadata = $this->getMetadata($className);
62
63
        $apiProperties = [];
64
65
        foreach ($metadata->propertyMetadata as $propertyMetadata) {
66
            /** @var PropertyMetadata $propertyMetadata */
67
            $apiProperties[] = $this->getPropertyName($propertyMetadata);
68
        }
69
70
        return $apiProperties;
71
    }
72
73
    /** {@inheritdoc} */
74
    public function getEntityProperties($className): array
75
    {
76
        $metadata = $this->getMetadata($className);
77
78
        $objectProperties = [];
79
80
        foreach ($metadata->propertyMetadata as $propertyMetadata) {
81
            /** @var PropertyMetadata $propertyMetadata */
82
            if (null !== $propertyMetadata->reflection) {
83
                $objectProperties[] = $propertyMetadata->reflection->getName();
84
            }
85
        }
86
87
        return $objectProperties;
88
    }
89
90
    /**
91
     * @param $className
92
     *
93
     * @return ClassMetadata
94
     * @throws MapperException
95
     */
96 4
    private function getMetadata($className): ClassMetadata
97
    {
98 4
        $metadata = $this->factory->getMetadataForClass($className);
99
100 4
        if (null === $metadata) {
101
            throw MapperException::unsupportedClass($className);
102
        }
103
104 4
        return $metadata;
105
    }
106
107 4
    private function getPropertyName(PropertyMetadata $propertyMetadata): string
108
    {
109 4
        return $this->strategy->translateName($propertyMetadata);
110
    }
111
}
112