RestCollectionTransformer   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 120
Duplicated Lines 6.67 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 12
c 1
b 0
f 0
lcom 1
cbo 6
dl 8
loc 120
rs 10
ccs 40
cts 40
cp 1

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
B transform() 0 25 4
B reverseTransform() 0 36 5
A getEntityIdentifier() 8 8 2

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 Pgs\RestfonyBundle\Form\DataTransformer;
4
5
use Doctrine\Common\Persistence\ObjectManager;
6
use Doctrine\Common\Collections\Collection;
7
use Doctrine\Common\Collections\ArrayCollection;
8
use Doctrine\Common\Persistence\Mapping\ClassMetadata;
9
use Symfony\Component\Form\DataTransformerInterface;
10
use Symfony\Component\Form\Exception\TransformationFailedException;
11
12
/**
13
 * Transforms Relationship data for entity based forms.
14
 */
15
class RestCollectionTransformer implements DataTransformerInterface
16
{
17
    /**
18
     * @var ObjectManager
19
     */
20
    private $entityManager;
21
22
    /**
23
     * The name of the entity we are working with.
24
     *
25
     * @var string
26
     */
27
    private $entityName;
28
29
    /**
30
     * @param ObjectManager $entityManager
31
     * @param string $entityName
32
     */
33 16
    public function __construct(ObjectManager $entityManager, $entityName)
34
    {
35 16
        $this->entityManager = $entityManager;
36 16
        $this->entityName = $entityName;
37 16
    }
38
39
    /**
40
     * Transforms an entity collection to an array of identifiers.
41
     *
42
     * @param Collection|null $collection
43
     *
44
     * @return string[]
45
     */
46 8
    public function transform($collection)
47
    {
48 8
        if (empty($collection)) {
49 1
            return [];
50
        }
51
52 7
        if (!$collection instanceof Collection) {
53 1
            throw new TransformationFailedException(sprintf(
54 1
                '%s is not an instance of %s',
55
                gettype($collection),
56 1
                'Doctrine\Common\Collections\Collection'
57
            ));
58
        }
59
60
        return $collection->map(function ($entity) {
61
            try {
62 5
                $entityString = (string) $entity;
63 5
            } catch (\Exception $e) {
0 ignored issues
show
Unused Code introduced by
catch (\Exception $e) { ...($metadata, $entity); } does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
64 5
                $metadata = $this->entityManager->getClassMetadata(get_class($entity));
65 5
                $entityString = $this->getEntityIdentifier($metadata, $entity);
66
            }
67
68 4
            return $entityString;
69 6
        })->toArray();
70
    }
71
72
    /**
73
     * Transforms an array of ids to an array of entities.
74
     *
75
     * @param Collection|array $collection
76
     *
77
     * @return Collection
78
     *
79
     * @throws TransformationFailedException if entity is not found.
80
     */
81 7
    public function reverseTransform($collection)
82
    {
83
        //convert plain arrays to a doctrine collection
84 7
        if (is_array($collection)) {
85 5
            $collection = new ArrayCollection($collection);
86
        }
87
88 7
        if (!$collection instanceof Collection) {
89 1
            throw new TransformationFailedException(sprintf(
90 1
                '%s is not an instance of %s',
91
                gettype($collection),
92 1
                'Doctrine\Common\Collections\Collection'
93
            ));
94
        }
95
96 6
        if ($collection->isEmpty()) {
97 2
            return $collection;
98
        }
99
100 4
        return $collection->map(function ($id) {
101 4
            $entity = $this->entityManager
102 4
                ->getRepository($this->entityName)
103 4
                ->find($id)
104
            ;
105
106 4
            if (null === $entity) {
107 1
                throw new TransformationFailedException(sprintf(
108 1
                    'A %s with id "%s" does not exist!',
109 1
                    $this->entityName,
110
                    $id
111
                ));
112
            }
113
114 3
            return $entity;
115 4
        });
116
    }
117
118
    /**
119
     * @param ClassMetadata $metadata
120
     * @param mixed         $entity   The entity.
121
     *
122
     * @throws \RuntimeException
123
     *
124
     * @return mixed
125
     */
126 5 View Code Duplication
    protected function getEntityIdentifier(ClassMetadata $metadata, $entity)
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...
127
    {
128 5
        if (count($metadata->getIdentifierFieldNames()) !== 1) {
129 1
            throw new \RuntimeException('Only one identifier allowed at this time.');
130
        }
131
132 4
        return $metadata->getIdentifierValues($entity)[$metadata->getIdentifierFieldNames()[0]];
133
    }
134
}
135