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

FieldDefinitionCreate::parse()   F

Complexity

Conditions 21
Paths 2309

Size

Total Lines 91
Code Lines 44

Duplication

Lines 22
Ratio 24.18 %

Importance

Changes 0
Metric Value
cc 21
eloc 44
nc 2309
nop 2
dl 22
loc 91
rs 2
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 FieldDefinitionCreate 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
use Exception;
20
21
/**
22
 * Parser for FieldDefinitionCreate.
23
 */
24
class FieldDefinitionCreate extends BaseParser
25
{
26
    /**
27
     * ContentType service.
28
     *
29
     * @var \eZ\Publish\API\Repository\ContentTypeService
30
     */
31
    protected $contentTypeService;
32
33
    /**
34
     * FieldType parser.
35
     *
36
     * @var \eZ\Publish\Core\REST\Common\Input\FieldTypeParser
37
     */
38
    protected $fieldTypeParser;
39
40
    /**
41
     * Parser tools.
42
     *
43
     * @var \eZ\Publish\Core\REST\Common\Input\ParserTools
44
     */
45
    protected $parserTools;
46
47
    /**
48
     * Construct.
49
     *
50
     * @param \eZ\Publish\API\Repository\ContentTypeService $contentTypeService
51
     * @param \eZ\Publish\Core\REST\Common\Input\FieldTypeParser $fieldTypeParser
52
     * @param \eZ\Publish\Core\REST\Common\Input\ParserTools $parserTools
53
     */
54
    public function __construct(ContentTypeService $contentTypeService, FieldTypeParser $fieldTypeParser, ParserTools $parserTools)
55
    {
56
        $this->contentTypeService = $contentTypeService;
57
        $this->fieldTypeParser = $fieldTypeParser;
58
        $this->parserTools = $parserTools;
59
    }
60
61
    /**
62
     * Parse input structure.
63
     *
64
     * @param array $data
65
     * @param \eZ\Publish\Core\REST\Common\Input\ParsingDispatcher $parsingDispatcher
66
     *
67
     * @throws \eZ\Publish\Core\REST\Common\Exceptions\Parser If an error is found while parsing
68
     *
69
     * @return \eZ\Publish\API\Repository\Values\ContentType\FieldDefinitionCreateStruct
70
     */
71
    public function parse(array $data, ParsingDispatcher $parsingDispatcher)
72
    {
73
        if (!array_key_exists('identifier', $data)) {
74
            throw new Exceptions\Parser("Missing 'identifier' element for FieldDefinitionCreate.");
75
        }
76
77
        if (!array_key_exists('fieldType', $data)) {
78
            throw new Exceptions\Parser("Missing 'fieldType' element for FieldDefinitionCreate.");
79
        }
80
81
        $fieldDefinitionCreate = $this->contentTypeService->newFieldDefinitionCreateStruct(
82
            $data['identifier'],
83
            $data['fieldType']
84
        );
85
86
        // @todo XSD says that descriptions is mandatory, but content type can be created without it
87
        if (array_key_exists('names', $data)) {
88
            if (!is_array($data['names']) || !array_key_exists('value', $data['names']) || !is_array($data['names']['value'])) {
89
                throw new Exceptions\Parser("Invalid 'names' element for FieldDefinitionCreate.");
90
            }
91
92
            $fieldDefinitionCreate->names = $this->parserTools->parseTranslatableList($data['names']);
93
        }
94
95
        // @todo XSD says that descriptions is mandatory, but content type can be created without it
96
        if (array_key_exists('descriptions', $data)) {
97
            if (!is_array($data['descriptions']) || !array_key_exists('value', $data['descriptions']) || !is_array($data['descriptions']['value'])) {
98
                throw new Exceptions\Parser("Invalid 'descriptions' element for FieldDefinitionCreate.");
99
            }
100
101
            $fieldDefinitionCreate->descriptions = $this->parserTools->parseTranslatableList($data['descriptions']);
102
        }
103
104
        // @todo XSD says that fieldGroup is mandatory, but content type can be created without it
105
        if (array_key_exists('fieldGroup', $data)) {
106
            $fieldDefinitionCreate->fieldGroup = $data['fieldGroup'];
107
        }
108
109
        // @todo XSD says that position is mandatory, but content type can be created without it
110
        if (array_key_exists('position', $data)) {
111
            $fieldDefinitionCreate->position = (int)$data['position'];
112
        }
113
114
        // @todo XSD says that isTranslatable is mandatory, but content type can be created without it
115
        if (array_key_exists('isTranslatable', $data)) {
116
            $fieldDefinitionCreate->isTranslatable = $this->parserTools->parseBooleanValue($data['isTranslatable']);
117
        }
118
119
        // @todo XSD says that isRequired is mandatory, but content type can be created without it
120
        if (array_key_exists('isRequired', $data)) {
121
            $fieldDefinitionCreate->isRequired = $this->parserTools->parseBooleanValue($data['isRequired']);
122
        }
123
124
        // @todo XSD says that isInfoCollector is mandatory, but content type can be created without it
125
        if (array_key_exists('isInfoCollector', $data)) {
126
            $fieldDefinitionCreate->isInfoCollector = $this->parserTools->parseBooleanValue($data['isInfoCollector']);
127
        }
128
129
        // @todo XSD says that isSearchable is mandatory, but content type can be created without it
130
        if (array_key_exists('isSearchable', $data)) {
131
            $fieldDefinitionCreate->isSearchable = $this->parserTools->parseBooleanValue($data['isSearchable']);
132
        }
133
134
        // @todo XSD says that defaultValue is mandatory, but content type can be created without it
135 View Code Duplication
        if (array_key_exists('defaultValue', $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...
136
            try {
137
                $fieldDefinitionCreate->defaultValue = $this->fieldTypeParser->parseValue(
138
                    $data['fieldType'],
139
                    $data['defaultValue']
140
                );
141
            } catch (Exception $e) {
142
                throw new Exceptions\Parser("Invalid 'defaultValue' element for FieldDefinitionCreate.", 0, $e);
143
            }
144
        }
145
146 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...
147
            $fieldDefinitionCreate->validatorConfiguration = $this->fieldTypeParser->parseValidatorConfiguration(
148
                $data['fieldType'],
149
                $data['validatorConfiguration']
150
            );
151
        }
152
153 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...
154
            $fieldDefinitionCreate->fieldSettings = $this->fieldTypeParser->parseFieldSettings(
155
                $data['fieldType'],
156
                $data['fieldSettings']
157
            );
158
        }
159
160
        return $fieldDefinitionCreate;
161
    }
162
}
163