Completed
Pull Request — master (#3)
by Pavel
06:46
created

JsonApiHydrator   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 74
Duplicated Lines 9.46 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 16
lcom 1
cbo 3
dl 7
loc 74
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
B hydrate() 7 20 8
C hydrateRelationships() 0 34 8

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
namespace pmill\Doctrine\Hydrator;
3
4
/**
5
 * Json API Request Doctrine Hydrator
6
 */
7
class JsonApiHydrator extends ArrayHydrator
8
{
9
    /**
10
     * @link http://jsonapi.org/format/#document-resource-objects
11
     * @param object|string $entity     Doctrine entity or class name
12
     * @param array         $data       Data to hydrate
13
     *
14
     * @return mixed|object
15
     */
16
    public function hydrate($entity, array $data)
17
    {
18 View Code Duplication
        if (is_string($entity) && class_exists($entity)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
19
            $entity = new $entity;
20
        } elseif (!is_object($entity)) {
21
            throw new \InvalidArgumentException(
22
                'Entity passed to JsonApiHydrator::hydrate() must be a class name or entity object'
23
            );
24
        }
25
26
        if (isset($data['attributes']) && is_array($data['attributes'])) {
27
            $entity = $this->hydrateProperties($entity, $data['attributes']);
28
        }
29
30
        if (isset($data['relationships']) && is_array($data['relationships'])) {
31
            $entity = $this->hydrateRelationships($entity, $data['relationships']);
32
        }
33
34
        return $entity;
35
    }
36
37
    /**
38
     * Map JSON API resource relations to doctrine entity.
39
     *
40
     * @param       $entity
41
     * @param array $relationships
42
     *
43
     * @return array
44
     * @throws \Exception
45
     */
46
    protected function hydrateRelationships($entity, array $relationships)
47
    {
48
        $metadata = $this->entityManager->getClassMetadata(get_class($entity));
49
        foreach ($relationships as $name => $data) {
50
            if (!isset($metadata->associationMappings[$name])) {
51
                throw new \Exception(sprintf('Relation `%s` association not found', $name));
52
            }
53
54
            if (is_array($data['data'])) {
55
                if (isset($data['data']['id']) && isset($data['data']['type'])) {
56
                    $this->hydrateToOneAssociation($entity, $name,
57
                        $metadata->associationMappings[$name],
58
                        $data['data']['id']
59
                    );
60
                } else {
61
                    $this->hydrateToManyAssociation($entity, $name,
62
                        $metadata->associationMappings[$name],
63
                        array_map(
64
                            function($relation) {
65
                                if (isset($relation['id']) && isset($relation['type'])) {
66
                                    return $relation['id'];
67
                                }
68
69
                                return ['attributes' => $relation];
70
                            },
71
                            $data['data']
72
                        )
73
                    );
74
                }
75
            }
76
        }
77
78
        return $entity;
79
    }
80
}
81