Completed
Push — ezp24624-query_controller_take... ( f346e1...647162 )
by
unknown
23:39
created

FieldDefinitionUpdate::parse()   F

Complexity

Conditions 19
Paths 4102

Size

Total Lines 82
Code Lines 38

Duplication

Lines 12
Ratio 14.63 %

Importance

Changes 0
Metric Value
cc 19
eloc 38
nc 4102
nop 2
dl 12
loc 82
rs 2.1041
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/**
4
 * File containing the FieldDefinitionUpdate parser class.
5
 *
6
 * @copyright Copyright (C) eZ Systems AS. All rights reserved.
7
 * @license For full copyright and license information view LICENSE file distributed with this source code.
8
 *
9
 * @version //autogentag//
10
 */
11
namespace eZ\Publish\Core\REST\Server\Input\Parser;
12
13
use eZ\Publish\Core\REST\Common\Input\BaseParser;
14
use eZ\Publish\Core\REST\Common\Input\ParsingDispatcher;
15
use eZ\Publish\Core\REST\Common\Input\FieldTypeParser;
16
use eZ\Publish\Core\REST\Common\Input\ParserTools;
17
use eZ\Publish\API\Repository\ContentTypeService;
18
use eZ\Publish\Core\REST\Common\Exceptions;
19
20
/**
21
 * Parser for FieldDefinitionUpdate.
22
 */
23
class FieldDefinitionUpdate extends BaseParser
24
{
25
    /**
26
     * ContentType service.
27
     *
28
     * @var \eZ\Publish\API\Repository\ContentTypeService
29
     */
30
    protected $contentTypeService;
31
32
    /**
33
     * FieldType parser.
34
     *
35
     * @var \eZ\Publish\Core\REST\Common\Input\FieldTypeParser
36
     */
37
    protected $fieldTypeParser;
38
39
    /**
40
     * Parser tools.
41
     *
42
     * @var \eZ\Publish\Core\REST\Common\Input\ParserTools
43
     */
44
    protected $parserTools;
45
46
    /**
47
     * Construct.
48
     *
49
     * @param \eZ\Publish\API\Repository\ContentTypeService $contentTypeService
50
     * @param \eZ\Publish\Core\REST\Common\Input\ParserTools $parserTools
51
     */
52
    public function __construct(ContentTypeService $contentTypeService, FieldTypeParser $fieldTypeParser, ParserTools $parserTools)
53
    {
54
        $this->contentTypeService = $contentTypeService;
55
        $this->fieldTypeParser = $fieldTypeParser;
56
        $this->parserTools = $parserTools;
57
    }
58
59
    /**
60
     * Parse input structure.
61
     *
62
     * @param array $data
63
     * @param \eZ\Publish\Core\REST\Common\Input\ParsingDispatcher $parsingDispatcher
64
     *
65
     * @return \eZ\Publish\API\Repository\Values\ContentType\FieldDefinitionUpdateStruct
66
     */
67
    public function parse(array $data, ParsingDispatcher $parsingDispatcher)
68
    {
69
        $fieldDefinitionUpdate = $this->contentTypeService->newFieldDefinitionUpdateStruct();
70
71
        if (array_key_exists('identifier', $data)) {
72
            $fieldDefinitionUpdate->identifier = $data['identifier'];
73
        }
74
75
        // @todo XSD says that descriptions is mandatory, but field definition can be updated without it
76
        if (array_key_exists('names', $data)) {
77
            if (!is_array($data['names']) || !array_key_exists('value', $data['names']) || !is_array($data['names']['value'])) {
78
                throw new Exceptions\Parser("Invalid 'names' element for FieldDefinitionUpdate.");
79
            }
80
81
            $fieldDefinitionUpdate->names = $this->parserTools->parseTranslatableList($data['names']);
82
        }
83
84
        // @todo XSD says that descriptions is mandatory, but field definition can be updated without it
85
        if (array_key_exists('descriptions', $data)) {
86
            if (!is_array($data['descriptions']) || !array_key_exists('value', $data['descriptions']) || !is_array($data['descriptions']['value'])) {
87
                throw new Exceptions\Parser("Invalid 'descriptions' element for FieldDefinitionUpdate.");
88
            }
89
90
            $fieldDefinitionUpdate->descriptions = $this->parserTools->parseTranslatableList($data['descriptions']);
91
        }
92
93
        // @todo XSD says that fieldGroup is mandatory, but field definition can be updated without it
94
        if (array_key_exists('fieldGroup', $data)) {
95
            $fieldDefinitionUpdate->fieldGroup = $data['fieldGroup'];
96
        }
97
98
        // @todo XSD says that position is mandatory, but field definition can be updated without it
99
        if (array_key_exists('position', $data)) {
100
            $fieldDefinitionUpdate->position = (int)$data['position'];
101
        }
102
103
        // @todo XSD says that isTranslatable is mandatory, but field definition can be updated without it
104
        if (array_key_exists('isTranslatable', $data)) {
105
            $fieldDefinitionUpdate->isTranslatable = $this->parserTools->parseBooleanValue($data['isTranslatable']);
106
        }
107
108
        // @todo XSD says that isRequired is mandatory, but field definition can be updated without it
109
        if (array_key_exists('isRequired', $data)) {
110
            $fieldDefinitionUpdate->isRequired = $this->parserTools->parseBooleanValue($data['isRequired']);
111
        }
112
113
        // @todo XSD says that isInfoCollector is mandatory, but field definition can be updated without it
114
        if (array_key_exists('isInfoCollector', $data)) {
115
            $fieldDefinitionUpdate->isInfoCollector = $this->parserTools->parseBooleanValue($data['isInfoCollector']);
116
        }
117
118
        // @todo XSD says that isSearchable is mandatory, but field definition can be updated without it
119
        if (array_key_exists('isSearchable', $data)) {
120
            $fieldDefinitionUpdate->isSearchable = $this->parserTools->parseBooleanValue($data['isSearchable']);
121
        }
122
123
        $fieldDefinition = $this->getFieldDefinition($data);
124
125
        // @todo XSD says that defaultValue is mandatory, but content type can be created without it
126
        if (array_key_exists('defaultValue', $data)) {
127
            $fieldDefinitionUpdate->defaultValue = $this->fieldTypeParser->parseValue(
128
                $fieldDefinition->fieldTypeIdentifier,
129
                $data['defaultValue']
130
            );
131
        }
132
133 View Code Duplication
        if (array_key_exists('validatorConfiguration', $data)) {
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...
134
            $fieldDefinitionUpdate->validatorConfiguration = $this->fieldTypeParser->parseValidatorConfiguration(
135
                $fieldDefinition->fieldTypeIdentifier,
136
                $data['validatorConfiguration']
137
            );
138
        }
139
140 View Code Duplication
        if (array_key_exists('fieldSettings', $data)) {
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...
141
            $fieldDefinitionUpdate->fieldSettings = $this->fieldTypeParser->parseFieldSettings(
142
                $fieldDefinition->fieldTypeIdentifier,
143
                $data['fieldSettings']
144
            );
145
        }
146
147
        return $fieldDefinitionUpdate;
148
    }
149
150
    /**
151
     * Returns field definition by 'typeFieldDefinitionDraft' pattern URL.
152
     *
153
     * Assumes given $data array has '__url' element set.
154
     *
155
     * @todo depends on temporary solution to give parser access to the URL
156
     *
157
     * @see \eZ\Publish\Core\REST\Server\Controller\ContentType::updateFieldDefinition
158
     *
159
     * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException
160
     *
161
     * @param array $data
162
     *
163
     * @return \eZ\Publish\API\Repository\Values\ContentType\FieldDefinition
164
     */
165
    protected function getFieldDefinition(array $data)
166
    {
167
        $contentTypeId = $this->requestParser->parseHref($data['__url'], 'contentTypeId');
168
        $fieldDefinitionId = $this->requestParser->parseHref($data['__url'], 'fieldDefinitionId');
169
170
        $contentTypeDraft = $this->contentTypeService->loadContentTypeDraft($contentTypeId);
171
        foreach ($contentTypeDraft->getFieldDefinitions() as $fieldDefinition) {
172
            if ($fieldDefinition->id == $fieldDefinitionId) {
173
                return $fieldDefinition;
174
            }
175
        }
176
        throw new Exceptions\NotFoundException("Field definition not found: '{$data['__url']}'.");
177
    }
178
}
179