Completed
Push — master ( 839f22...1b3a7e )
by Pavel
04:14
created

SymfonyPropertyMapper::getEntityProperties()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 3.1852

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 2
cts 6
cp 0.3333
rs 9.6666
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 1
crap 3.1852
1
<?php
2
3
namespace ScayTrase\Api\Cruds\Adaptors\Symfony;
4
5
use ScayTrase\Api\Cruds\Exception\MapperException;
6
use ScayTrase\Api\Cruds\PropertyMapperInterface;
7
use Symfony\Component\Serializer\Mapping\AttributeMetadataInterface;
8
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactoryInterface;
9
use Symfony\Component\Serializer\NameConverter\NameConverterInterface;
10
11
final class SymfonyPropertyMapper implements PropertyMapperInterface
12
{
13
    /** @var  ClassMetadataFactoryInterface */
14
    private $factory;
15
    /** @var  NameConverterInterface */
16
    private $strategy;
17
18
    /**
19
     * SymfonyPropertyMapper constructor.
20
     *
21
     * @param ClassMetadataFactoryInterface $factory
22
     * @param NameConverterInterface        $strategy
23
     */
24 2
    public function __construct(ClassMetadataFactoryInterface $factory, NameConverterInterface $strategy)
25
    {
26 2
        $this->factory  = $factory;
27 2
        $this->strategy = $strategy;
28 2
    }
29
30
    /** {@inheritdoc} */
31 2
    public function getEntityProperty($className, $apiProperty)
32
    {
33 2
        if (!$this->factory->hasMetadataFor($className)) {
34
            return null;
35
        }
36
37 2
        if (!in_array($apiProperty, $this->getApiProperties($className), true)) {
38
            return null;
39
        }
40
41 2
        return $this->strategy->denormalize($apiProperty);
42
    }
43
44
    /** {@inheritdoc} */
45
    public function getApiProperty($className, $objectProperty)
46
    {
47
        if (!$this->factory->hasMetadataFor($className)) {
48
            return null;
49
        }
50
51
        if (!in_array($objectProperty, $this->getEntityProperties($className), true)) {
52
            return null;
53
        }
54
55
        return $this->strategy->normalize($objectProperty);
56
    }
57
58
    /** {@inheritdoc} */
59 2
    public function getApiProperties($className)
60
    {
61 2
        $properties = [];
62 2
        foreach ($this->getMetadata($className) as $property) {
63 2
            $properties[] = $this->strategy->denormalize($property->getName());
64 2
        }
65
66 2
        return $properties;
67
    }
68
69
    /** {@inheritdoc} */
70 1
    public function getEntityProperties($className)
71
    {
72 1
        $properties = [];
73
        foreach ($this->getMetadata($className) as $property) {
74
            $properties[] = $property->getName();
75
        }
76
77
        return $properties;
78
    }
79
80
    /**
81
     * @param $className
82
     *
83
     * @return AttributeMetadataInterface[]
84
     */
85 2
    private function getMetadata($className)
86
    {
87
        try {
88 2
            return $this->factory->getMetadataFor($className)->getAttributesMetadata();
89
        } catch (\InvalidArgumentException $exception) {
90
            throw MapperException::unsupportedClass($className);
91
        }
92
    }
93
}
94