Completed
Push — ezp-30696 ( 9bb3ad...3bd812 )
by
unknown
49:02 queued 18:35
created

ContentCreate::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 5
dl 0
loc 13
rs 9.8333
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * File containing the ContentCreate 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
namespace eZ\Publish\Core\REST\Server\Input\Parser;
10
11
use eZ\Publish\Core\REST\Common\Input\BaseParser;
12
use eZ\Publish\Core\REST\Common\Input\ParsingDispatcher;
13
use eZ\Publish\Core\REST\Common\Input\ParserTools;
14
use eZ\Publish\Core\REST\Common\Input\FieldTypeParser;
15
use eZ\Publish\Core\REST\Common\Exceptions;
16
use eZ\Publish\API\Repository\ContentService;
17
use eZ\Publish\API\Repository\ContentTypeService;
18
use eZ\Publish\Core\REST\Server\Values\RestContentCreateStruct;
19
use DateTime;
20
21
/**
22
 * Parser for ContentCreate.
23
 */
24
class ContentCreate extends BaseParser
25
{
26
    /**
27
     * Content service.
28
     *
29
     * @var \eZ\Publish\API\Repository\ContentService
30
     */
31
    protected $contentService;
32
33
    /**
34
     * ContentType service.
35
     *
36
     * @var \eZ\Publish\API\Repository\ContentTypeService
37
     */
38
    protected $contentTypeService;
39
40
    /**
41
     * FieldType parser.
42
     *
43
     * @var \eZ\Publish\Core\REST\Common\Input\FieldTypeParser
44
     */
45
    protected $fieldTypeParser;
46
47
    /**
48
     * LocationCreate parser.
49
     *
50
     * @var \eZ\Publish\Core\REST\Server\Input\Parser\LocationCreate
51
     */
52
    protected $locationCreateParser;
53
54
    /**
55
     * Parser tools.
56
     *
57
     * @var \eZ\Publish\Core\REST\Common\Input\ParserTools
58
     */
59
    protected $parserTools;
60
61
    /**
62
     * Construct.
63
     *
64
     * @param \eZ\Publish\API\Repository\ContentService $contentService
65
     * @param \eZ\Publish\API\Repository\ContentTypeService $contentTypeService
66
     * @param \eZ\Publish\Core\REST\Common\Input\FieldTypeParser $fieldTypeParser
67
     * @param \eZ\Publish\Core\REST\Server\Input\Parser\LocationCreate $locationCreateParser
68
     * @param \eZ\Publish\Core\REST\Common\Input\ParserTools $parserTools
69
     */
70
    public function __construct(
71
        ContentService $contentService,
72
        ContentTypeService $contentTypeService,
73
        FieldTypeParser $fieldTypeParser,
74
        LocationCreate $locationCreateParser,
75
        ParserTools $parserTools
76
    ) {
77
        $this->contentService = $contentService;
78
        $this->contentTypeService = $contentTypeService;
79
        $this->fieldTypeParser = $fieldTypeParser;
80
        $this->locationCreateParser = $locationCreateParser;
81
        $this->parserTools = $parserTools;
82
    }
83
84
    /**
85
     * Parse input structure.
86
     *
87
     * @param array $data
88
     * @param \eZ\Publish\Core\REST\Common\Input\ParsingDispatcher $parsingDispatcher
89
     *
90
     * @return \eZ\Publish\Core\REST\Server\Values\RestContentCreateStruct
91
     */
92
    public function parse(array $data, ParsingDispatcher $parsingDispatcher)
93
    {
94
        if (!array_key_exists('LocationCreate', $data) || !is_array($data['LocationCreate'])) {
95
            throw new Exceptions\Parser("Missing or invalid 'LocationCreate' element for ContentCreate.");
96
        }
97
98
        $locationCreateStruct = $this->locationCreateParser->parse($data['LocationCreate'], $parsingDispatcher);
99
100
        if (!array_key_exists('ContentType', $data) || !is_array($data['ContentType'])) {
101
            throw new Exceptions\Parser("Missing or invalid 'ContentType' element for ContentCreate.");
102
        }
103
104
        if (!array_key_exists('_href', $data['ContentType'])) {
105
            throw new Exceptions\Parser("Missing '_href' attribute for ContentType element in ContentCreate.");
106
        }
107
108
        if (!array_key_exists('mainLanguageCode', $data)) {
109
            throw new Exceptions\Parser("Missing 'mainLanguageCode' element for ContentCreate.");
110
        }
111
112
        $contentType = $this->contentTypeService->loadContentType(
113
            $this->requestParser->parseHref($data['ContentType']['_href'], 'contentTypeId')
114
        );
115
116
        $contentCreateStruct = $this->contentService->newContentCreateStruct($contentType, $data['mainLanguageCode']);
117
118
        if (array_key_exists('Section', $data) && is_array($data['Section'])) {
119
            if (!array_key_exists('_href', $data['Section'])) {
120
                throw new Exceptions\Parser("Missing '_href' attribute for Section element in ContentCreate.");
121
            }
122
123
            $contentCreateStruct->sectionId = $this->requestParser->parseHref($data['Section']['_href'], 'sectionId');
124
        }
125
126
        if (array_key_exists('alwaysAvailable', $data)) {
127
            $contentCreateStruct->alwaysAvailable = $this->parserTools->parseBooleanValue($data['alwaysAvailable']);
128
        }
129
130
        if (array_key_exists('remoteId', $data)) {
131
            $contentCreateStruct->remoteId = $data['remoteId'];
132
        }
133
134
        if (array_key_exists('modificationDate', $data)) {
135
            $contentCreateStruct->modificationDate = new DateTime($data['modificationDate']);
136
        }
137
138
        if (array_key_exists('User', $data) && is_array($data['User'])) {
139
            if (!array_key_exists('_href', $data['User'])) {
140
                throw new Exceptions\Parser("Missing '_href' attribute for User element in ContentCreate.");
141
            }
142
143
            $contentCreateStruct->ownerId = $this->requestParser->parseHref($data['User']['_href'], 'userId');
144
        }
145
146
        if (!array_key_exists('fields', $data) || !is_array($data['fields']) || !is_array($data['fields']['field'])) {
147
            throw new Exceptions\Parser("Missing or invalid 'fields' element for ContentCreate.");
148
        }
149
150 View Code Duplication
        foreach ($data['fields']['field'] as $fieldData) {
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...
151
            if (!array_key_exists('fieldDefinitionIdentifier', $fieldData)) {
152
                throw new Exceptions\Parser("Missing 'fieldDefinitionIdentifier' element in field data for ContentCreate.");
153
            }
154
155
            $fieldDefinition = $contentType->getFieldDefinition($fieldData['fieldDefinitionIdentifier']);
156
            if (!$fieldDefinition) {
157
                throw new Exceptions\Parser(
158
                    "'{$fieldData['fieldDefinitionIdentifier']}' is invalid field definition identifier for '{$contentType->identifier}' content type in ContentCreate."
159
                );
160
            }
161
162
            if (!array_key_exists('fieldValue', $fieldData)) {
163
                throw new Exceptions\Parser("Missing 'fieldValue' element for '{$fieldData['fieldDefinitionIdentifier']}' identifier in ContentCreate.");
164
            }
165
166
            $fieldValue = $this->fieldTypeParser->parseValue($fieldDefinition->fieldTypeIdentifier,
167
                $fieldData['fieldValue']);
168
169
            $languageCode = null;
170
            if (array_key_exists('languageCode', $fieldData)) {
171
                $languageCode = $fieldData['languageCode'];
172
            }
173
174
            $contentCreateStruct->setField($fieldData['fieldDefinitionIdentifier'], $fieldValue, $languageCode);
175
        }
176
177
        return new RestContentCreateStruct($contentCreateStruct, $locationCreateStruct);
178
    }
179
}
180