Completed
Push — master ( c623e8...f78604 )
by Pavel
05:18
created

SymfonyPropertyMapper   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 101
Duplicated Lines 23.76 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 0%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 12
c 2
b 0
f 0
lcom 1
cbo 4
dl 24
loc 101
rs 10
ccs 0
cts 40
cp 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getObjectProperty() 12 12 3
A getApiProperty() 12 12 3
A getApiProperties() 0 9 2
A getObjectProperties() 0 9 2
A getMetadata() 0 4 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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
    public function __construct(ClassMetadataFactoryInterface $factory, NameConverterInterface $strategy)
25
    {
26
        $this->factory  = $factory;
27
        $this->strategy = $strategy;
28
    }
29
30
    /**
31
     * @param string $className
32
     * @param string $apiProperty
33
     *
34
     * @return null|string
35
     * @throws MapperException
36
     */
37 View Code Duplication
    public function getObjectProperty($className, $apiProperty)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
38
    {
39
        if ($this->factory->hasMetadataFor($className)) {
40
            return null;
41
        }
42
43
        if (!in_array($apiProperty, $this->getApiProperties($className), true)) {
44
            return null;
45
        }
46
47
        return $this->strategy->denormalize($apiProperty);
48
    }
49
50
    /**
51
     * @param string $className
52
     * @param string $objectProperty
53
     *
54
     * @return null|string
55
     * @throws MapperException
56
     */
57 View Code Duplication
    public function getApiProperty($className, $objectProperty)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
58
    {
59
        if ($this->factory->hasMetadataFor($className)) {
60
            return null;
61
        }
62
63
        if (!in_array($objectProperty, $this->getObjectProperties($className), true)) {
64
            return null;
65
        }
66
67
        return $this->strategy->normalize($objectProperty);
68
    }
69
70
    /**
71
     * @param string $className
72
     *
73
     * @return string[]
74
     * @throws MapperException
75
     */
76
    public function getApiProperties($className)
77
    {
78
        $properties = [];
79
        foreach ($this->getMetadata($className) as $property) {
80
            $properties[] = $this->strategy->denormalize($property->getName());
81
        }
82
83
        return $properties;
84
    }
85
86
    /**
87
     * @param string $className
88
     *
89
     * @return string[]
90
     * @throws MapperException
91
     */
92
    public function getObjectProperties($className)
93
    {
94
        $properties = [];
95
        foreach ($this->getMetadata($className) as $property) {
96
            $properties[] = $property->getName();
97
        }
98
99
        return $properties;
100
    }
101
102
    /**
103
     * @param $className
104
     *
105
     * @return AttributeMetadataInterface[]
106
     */
107
    private function getMetadata($className)
108
    {
109
        return $this->factory->getMetadataFor($className)->getAttributesMetadata();
110
    }
111
}
112