DataObject   A
last analyzed

Complexity

Total Complexity 24

Size/Duplication

Total Lines 179
Duplicated Lines 6.15 %

Coupling/Cohesion

Components 1
Dependencies 11

Importance

Changes 11
Bugs 1 Features 0
Metric Value
wmc 24
c 11
b 1
f 0
lcom 1
cbo 11
dl 11
loc 179
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A assertPatch() 0 4 1
B assertPost() 0 19 5
A assertPut() 0 4 1
A missingCreationAttributes() 0 20 2
A getAttributes() 0 12 1
C assertRelationshipData() 0 24 7
C relationshipDataAssert() 11 41 7

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
 * Author: Nil Portugués Calderó <[email protected]>
4
 * Date: 11/27/15
5
 * Time: 9:58 PM.
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
namespace NilPortugues\Api\JsonApi\Server\Data;
11
12
use NilPortugues\Api\JsonApi\JsonApiSerializer;
13
use NilPortugues\Api\JsonApi\JsonApiTransformer;
14
use NilPortugues\Api\JsonApi\Server\Errors\ErrorBag;
15
use NilPortugues\Api\JsonApi\Server\Errors\InvalidAttributeError;
16
use NilPortugues\Api\JsonApi\Server\Errors\InvalidTypeError;
17
use NilPortugues\Api\JsonApi\Server\Errors\MissingAttributeError;
18
use NilPortugues\Api\JsonApi\Server\Errors\MissingDataError;
19
use NilPortugues\Api\JsonApi\Server\Errors\MissingTypeError;
20
21
/**
22
 * Class DataObject.
23
 */
24
class DataObject
25
{
26
    /**
27
     * @param array             $data
28
     * @param JsonApiSerializer $serializer
29
     * @param string            $className
30
     * @param ErrorBag          $errorBag
31
     */
32
    public static function assertPatch($data, JsonApiSerializer $serializer, $className, ErrorBag $errorBag)
33
    {
34
        DataAssertions::assert($data, $serializer, $className, $errorBag);
35
    }
36
37
    /**
38
     * @param array             $data
39
     * @param JsonApiSerializer $serializer
40
     * @param string            $className
41
     * @param ErrorBag          $errorBag
42
     *
43
     * @throws DataException
44
     */
45
    public static function assertPost($data, JsonApiSerializer $serializer, $className, ErrorBag $errorBag)
46
    {
47
        try {
48
            DataAssertions::assert($data, $serializer, $className, $errorBag);
49
            self::assertRelationshipData($data, $serializer, $errorBag);
50
        } catch (DataException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
51
        }
52
53
        $missing = self::missingCreationAttributes($data, $serializer);
54
        if (false === empty($missing)) {
55
            foreach ($missing as $attribute) {
56
                $errorBag->offsetSet(null, new MissingAttributeError($attribute));
57
            }
58
        }
59
60
        if ($errorBag->count() > 0) {
61
            throw new DataException();
62
        }
63
    }
64
65
    /**
66
     * @param array             $data
67
     * @param JsonApiSerializer $serializer
68
     * @param string            $className
69
     * @param ErrorBag          $errorBag
70
     *
71
     * @throws DataException
72
     */
73
    public static function assertPut($data, JsonApiSerializer $serializer, $className, ErrorBag $errorBag)
74
    {
75
        self::assertPost($data, $serializer, $className, $errorBag);
76
    }
77
78
    /**
79
     * @param array             $data
80
     * @param JsonApiSerializer $serializer
81
     *
82
     * @return array
83
     */
84
    protected static function missingCreationAttributes(array $data, JsonApiSerializer $serializer)
85
    {
86
        $inputAttributes = array_keys($data[JsonApiTransformer::ATTRIBUTES_KEY]);
87
88
        $mapping = $serializer->getTransformer()->getMappingByAlias($data[JsonApiTransformer::TYPE_KEY]);
89
90
        $diff = [];
91
        if (null !== $mapping) {
92
            $properties = str_replace(
93
                array_keys($mapping->getAliasedProperties()),
94
                array_values($mapping->getAliasedProperties()),
95
                $mapping->getProperties()
96
            );
97
            $properties = array_diff($properties, $mapping->getIdProperties());
98
99
            $diff = (array) array_diff($properties, $inputAttributes);
100
        }
101
102
        return $diff;
103
    }
104
105
    /**
106
     * @param array             $data
107
     * @param JsonApiSerializer $serializer
108
     *
109
     * @return array
110
     */
111
    public static function getAttributes(array $data, JsonApiSerializer $serializer)
112
    {
113
        $mapping = $serializer->getTransformer()->getMappingByAlias($data[JsonApiTransformer::TYPE_KEY]);
114
        $aliases = $mapping->getAliasedProperties();
115
        $keys = str_replace(
116
            array_values($aliases),
117
            array_keys($aliases),
118
            array_keys($data[JsonApiTransformer::ATTRIBUTES_KEY])
119
        );
120
121
        return array_combine($keys, array_values($data[JsonApiTransformer::ATTRIBUTES_KEY]));
122
    }
123
124
    /**
125
     * @param array             $data
126
     * @param JsonApiSerializer $serializer
127
     * @param ErrorBag          $errorBag
128
     *
129
     * @throws DataException
130
     */
131
    protected static function assertRelationshipData(array $data, JsonApiSerializer $serializer, ErrorBag $errorBag)
132
    {
133
        if (!empty($data[JsonApiTransformer::RELATIONSHIPS_KEY])) {
134
            foreach ($data[JsonApiTransformer::RELATIONSHIPS_KEY] as $relationshipData) {
135
                if (empty($relationshipData[JsonApiTransformer::DATA_KEY]) || !is_array(
136
                        $relationshipData[JsonApiTransformer::DATA_KEY]
137
                    )
138
                ) {
139
                    $errorBag->offsetSet(null, new MissingDataError());
140
                    break;
141
                }
142
143
                $firstKey = key($relationshipData[JsonApiTransformer::DATA_KEY]);
144
                if (is_numeric($firstKey)) {
145
                    foreach ($relationshipData[JsonApiTransformer::DATA_KEY] as $inArrayRelationshipData) {
146
                        self::relationshipDataAssert($inArrayRelationshipData, $serializer, $errorBag);
147
                    }
148
                    break;
149
                }
150
151
                self::relationshipDataAssert($relationshipData[JsonApiTransformer::DATA_KEY], $serializer, $errorBag);
152
            }
153
        }
154
    }
155
156
    /**
157
     * @param array             $relationshipData
158
     * @param JsonApiSerializer $serializer
159
     * @param ErrorBag          $errorBag
160
     */
161
    protected static function relationshipDataAssert($relationshipData, JsonApiSerializer $serializer, ErrorBag $errorBag)
162
    {
163
        //Has type member.
164
        if (empty($relationshipData[JsonApiTransformer::TYPE_KEY]) || !is_string(
165
                $relationshipData[JsonApiTransformer::TYPE_KEY]
166
            )
167
        ) {
168
            $errorBag->offsetSet(null, new MissingTypeError());
169
170
            return;
171
        }
172
173
        //Provided type value is supported.
174 View Code Duplication
        if (null === $serializer->getTransformer()->getMappingByAlias(
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...
175
                $relationshipData[JsonApiTransformer::TYPE_KEY]
176
            )
177
        ) {
178
            $errorBag->offsetSet(null, new InvalidTypeError($relationshipData[JsonApiTransformer::TYPE_KEY]));
179
180
            return;
181
        }
182
183
        //Validate if attributes passed in make sense.
184
        if (!empty($relationshipData[JsonApiTransformer::ATTRIBUTES_KEY])) {
185
            $mapping = $serializer->getTransformer()->getMappingByAlias(
186
                $relationshipData[JsonApiTransformer::TYPE_KEY]
187
            );
188
189
            $properties = str_replace(
190
                array_keys($mapping->getAliasedProperties()),
191
                array_values($mapping->getAliasedProperties()),
192
                $mapping->getProperties()
193
            );
194
195
            foreach (array_keys($relationshipData[JsonApiTransformer::ATTRIBUTES_KEY]) as $property) {
196 View Code Duplication
                if (false === in_array($property, $properties, true)) {
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...
197
                    $errorBag->offsetSet(null, new InvalidAttributeError($property, $relationshipData[JsonApiTransformer::TYPE_KEY]));
198
                }
199
            }
200
        }
201
    }
202
}
203