addInvalidParameterErrorsToErrorBag()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
rs 9.4285
cc 3
eloc 4
nc 3
nop 2
1
<?php
2
/**
3
 * Author: Nil Portugués Calderó <[email protected]>
4
 * Date: 11/14/15
5
 * Time: 11:46 AM.
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\Actions\Traits;
11
12
use NilPortugues\Api\JsonApi\Http\Request\Parameters\Fields;
13
use NilPortugues\Api\JsonApi\Http\Request\Parameters\Included;
14
use NilPortugues\Api\JsonApi\JsonApiSerializer;
15
use NilPortugues\Api\JsonApi\Server\Errors\InvalidParameterError;
16
use NilPortugues\Api\JsonApi\Server\Errors\InvalidParameterMemberError;
17
use NilPortugues\Api\Mapping\Mapping;
18
19
/**
20
 * Class RequestTrait.
21
 */
22
trait RequestTrait
23
{
24
    /**
25
     * @var \NilPortugues\Api\JsonApi\Server\Errors\Error[]
26
     */
27
    protected $queryParamErrorBag = [];
28
29
    /**
30
     * @return \NilPortugues\Api\JsonApi\Server\Errors\Error[]
31
     */
32
    protected function getQueryParamsErrors()
33
    {
34
        return $this->queryParamErrorBag;
35
    }
36
37
    /**
38
     * @param JsonApiSerializer $serializer
39
     * @param Fields            $fields
40
     * @param Included          $included
41
     *
42
     * @return bool
43
     */
44
    protected function hasValidQueryParams(JsonApiSerializer $serializer, Fields $fields, Included $included)
45
    {
46
        $this->validateFieldsQueryParams($serializer, $fields, 'Fields');
47
        $this->validateIncludeQueryParamsTypes($serializer, $included, 'Include');
48
49
        return empty($this->queryParamErrorBag);
50
    }
51
52
    /**
53
     * @param JsonApiSerializer $serializer
54
     * @param Fields            $fields
55
     * @param string            $paramName
56
     */
57 View Code Duplication
    protected function validateFieldsQueryParams(JsonApiSerializer $serializer, Fields $fields, $paramName)
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 (false === $fields->isEmpty()) {
60
            $validateFields = $fields->types();
61
62
            foreach ($validateFields as $key => $field) {
63
                $mapping = $serializer->getTransformer()->getMappingByAlias($field);
64
                if (null !== $mapping) {
65
                    $properties = $this->getPropertiesFromMapping($mapping);
66
                    $invalidProperties = array_diff($fields->members($field), $properties);
67
                    $this->addInvalidParameterMemberErrorsToErrorBag($invalidProperties, $paramName, $field);
68
                    unset($validateFields[$key]);
69
                }
70
            }
71
72
            $this->addInvalidParameterErrorsToErrorBag($paramName, $validateFields);
73
        }
74
    }
75
76
    /**
77
     * @param Mapping $mapping
78
     *
79
     * @return array
80
     */
81
    protected function getPropertiesFromMapping(Mapping $mapping)
82
    {
83
        $properties = array_merge(
84
            array_combine($mapping->getProperties(), $mapping->getProperties()),
85
            $mapping->getAliasedProperties()
86
        );
87
88
        return $properties;
89
    }
90
91
    /**
92
     * @param array  $invalidProperties
93
     * @param string $paramName
94
     * @param string $field
95
     */
96
    protected function addInvalidParameterMemberErrorsToErrorBag(array $invalidProperties, $paramName, $field)
97
    {
98
        foreach ($invalidProperties as $extraField) {
99
            $this->queryParamErrorBag[] = new InvalidParameterMemberError($extraField, $field, strtolower(
100
                $paramName
101
            ));
102
        }
103
    }
104
105
    /**
106
     * @param string $paramName
107
     * @param array  $validateFields
108
     */
109
    protected function addInvalidParameterErrorsToErrorBag($paramName, array &$validateFields)
110
    {
111
        if (false === empty($validateFields)) {
112
            foreach ($validateFields as $field) {
113
                $this->queryParamErrorBag[] = new InvalidParameterError($field, strtolower($paramName));
114
            }
115
        }
116
    }
117
118
    /**
119
     * @param JsonApiSerializer $serializer
120
     * @param Included          $included
121
     * @param string            $paramName
122
     */
123 View Code Duplication
    protected function validateIncludeQueryParamsTypes(JsonApiSerializer $serializer, Included $included, $paramName)
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...
124
    {
125
        if (false === $included->isEmpty()) {
126
            $validateFields = array_keys($included->get());
127
128
            foreach ($validateFields as $key => $field) {
129
                $mapping = $serializer->getTransformer()->getMappingByAlias($field);
130
                if (null !== $mapping) {
131
                    $properties = $this->getPropertiesFromMapping($mapping);
132
                    $invalidProperties = array_diff($included->get()[$field], $properties);
133
                    $this->addInvalidParameterMemberErrorsToErrorBag($invalidProperties, $paramName, $field);
134
                    unset($validateFields[$key]);
135
                }
136
            }
137
138
            $this->addInvalidParameterErrorsToErrorBag($paramName, $validateFields);
139
        }
140
    }
141
}
142