Test Failed
Push — master ( 0fcd0f...4e8aa2 )
by Pavel
04:49 queued 16s
created

JmsPropertyMapper::getApiProperty()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 10
rs 9.4285
cc 2
eloc 5
nc 2
nop 2
1
<?php
2
3
namespace ScayTrase\Api\Cruds\Adaptors\Jms;
4
5
use JMS\Serializer\Metadata\PropertyMetadata;
6
use Metadata\MetadataFactoryInterface;
7
use ScayTrase\Api\Cruds\Exception\MapperException;
8
use ScayTrase\Api\Cruds\PropertyMapperInterface;
9
10
final class JmsPropertyMapper implements PropertyMapperInterface
11
{
12
    /** @var MetadataFactoryInterface */
13
    private $factory;
14
15
    /** {@inheritdoc} */
16
    public function getObjectProperty($className, $apiProperty)
17
    {
18
        $metadata = $this->getMetadata($className);
19
20
        foreach ($metadata->propertyMetadata as $propertyMetadata) {
21
            /** @var PropertyMetadata $propertyMetadata */
22
            if ($propertyMetadata->serializedName === $apiProperty) {
23
                return $propertyMetadata->reflection ? $propertyMetadata->reflection->getName() : null;
24
            }
25
        }
26
27
        return null;
28
    }
29
30
    /** {@inheritdoc} */
31
    public function getApiProperty($className, $objectProperty)
32
    {
33
        $metadata = $this->getMetadata($className);
34
35
        if (!array_key_exists($objectProperty, $metadata->propertyMetadata)) {
36
            return null;
37
        }
38
39
        return $metadata->propertyMetadata[$objectProperty]->serializedName;
40
    }
41
42
    /** {@inheritdoc} */
43
    public function getApiProperties($className)
44
    {
45
        $metadata = $this->getMetadata($className);
46
47
        $apiProperties = [];
48
49
        foreach ($metadata->propertyMetadata as $propertyMetadata) {
50
            /** @var PropertyMetadata $propertyMetadata */
51
            $apiProperties[] = $propertyMetadata->serializedName;
52
        }
53
54
        return $apiProperties;
55
    }
56
57
    /** {@inheritdoc} */
58
    public function getObjectProperties($className)
59
    {
60
        $metadata = $this->getMetadata($className);
61
62
        $objectProperties = [];
63
64
        foreach ($metadata->propertyMetadata as $propertyMetadata) {
65
            /** @var PropertyMetadata $propertyMetadata */
66
            if (null !== $propertyMetadata->reflection) {
67
                $objectProperties[] = $propertyMetadata->reflection->getName();
68
            }
69
        }
70
71
        return $objectProperties;
72
    }
73
74
    /**
75
     * @param $className
76
     *
77
     * @return \Metadata\ClassHierarchyMetadata|\Metadata\MergeableClassMetadata|null
78
     * @throws MapperException
79
     */
80
    private function getMetadata($className)
81
    {
82
        $metadata = $this->factory->getMetadataForClass($className);
83
84
        if (null === $metadata) {
85
            throw MapperException::unsupportedClass($className);
86
        }
87
88
        return $metadata;
89
    }
90
}
91