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

UserUpdate::parse()   F

Complexity

Conditions 28
Paths 10576

Size

Total Lines 110
Code Lines 57

Duplication

Lines 50
Ratio 45.45 %

Importance

Changes 0
Metric Value
cc 28
eloc 57
nc 10576
nop 2
dl 50
loc 110
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 UserUpdate 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\Core\REST\Common\Exceptions;
18
use eZ\Publish\Core\REST\Server\Values\RestUserUpdateStruct;
19
use eZ\Publish\API\Repository\UserService;
20
use eZ\Publish\API\Repository\ContentService;
21
22
/**
23
 * Parser for UserUpdate.
24
 */
25
class UserUpdate extends BaseParser
26
{
27
    /**
28
     * User service.
29
     *
30
     * @var \eZ\Publish\API\Repository\UserService
31
     */
32
    protected $userService;
33
34
    /**
35
     * Content service.
36
     *
37
     * @var \eZ\Publish\API\Repository\ContentService
38
     */
39
    protected $contentService;
40
41
    /**
42
     * FieldType parser.
43
     *
44
     * @var \eZ\Publish\Core\REST\Common\Input\FieldTypeParser
45
     */
46
    protected $fieldTypeParser;
47
48
    /**
49
     * Parser tools.
50
     *
51
     * @var \eZ\Publish\Core\REST\Common\Input\ParserTools
52
     */
53
    protected $parserTools;
54
55
    /**
56
     * Construct.
57
     *
58
     * @param \eZ\Publish\API\Repository\UserService $userService
59
     * @param \eZ\Publish\API\Repository\ContentService $contentService
60
     * @param \eZ\Publish\Core\REST\Common\Input\FieldTypeParser $fieldTypeParser
61
     * @param \eZ\Publish\Core\REST\Common\Input\ParserTools $parserTools
62
     */
63
    public function __construct(UserService $userService, ContentService $contentService, FieldTypeParser $fieldTypeParser, ParserTools $parserTools)
64
    {
65
        $this->userService = $userService;
66
        $this->contentService = $contentService;
67
        $this->fieldTypeParser = $fieldTypeParser;
68
        $this->parserTools = $parserTools;
69
    }
70
71
    /**
72
     * Parse input structure.
73
     *
74
     * @param array $data
75
     * @param \eZ\Publish\Core\REST\Common\Input\ParsingDispatcher $parsingDispatcher
76
     *
77
     * @return \eZ\Publish\Core\REST\Server\Values\RestUserUpdateStruct
78
     */
79
    public function parse(array $data, ParsingDispatcher $parsingDispatcher)
80
    {
81
        $parsedData = array();
82
83
        //@todo XSD has a login element, but it's not possible to update login
84
85
        if (array_key_exists('email', $data)) {
86
            $parsedData['email'] = $data['email'];
87
        }
88
89
        if (array_key_exists('password', $data)) {
90
            $parsedData['password'] = $data['password'];
91
        }
92
93
        if (array_key_exists('enabled', $data)) {
94
            $parsedData['enabled'] = $this->parserTools->parseBooleanValue($data['enabled']);
95
        }
96
97
        if (array_key_exists('mainLanguageCode', $data)) {
98
            $parsedData['mainLanguageCode'] = $data['mainLanguageCode'];
99
        }
100
101 View Code Duplication
        if (array_key_exists('Section', $data) && is_array($data['Section'])) {
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...
102
            if (!array_key_exists('_href', $data['Section'])) {
103
                throw new Exceptions\Parser("Missing '_href' attribute for Section element in UserUpdate.");
104
            }
105
106
            $parsedData['sectionId'] = $this->requestParser->parseHref($data['Section']['_href'], 'sectionId');
107
        }
108
109
        if (array_key_exists('remoteId', $data)) {
110
            $parsedData['remoteId'] = $data['remoteId'];
111
        }
112
113
        if (array_key_exists('fields', $data)) {
114
            $userId = $this->requestParser->parseHref($data['__url'], 'userId');
115
116
            if (!is_array($data['fields']) || !array_key_exists('field', $data['fields']) || !is_array($data['fields']['field'])) {
117
                throw new Exceptions\Parser("Invalid 'fields' element for UserUpdate.");
118
            }
119
120
            $parsedData['fields'] = array();
121 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...
122
                if (!array_key_exists('fieldDefinitionIdentifier', $fieldData)) {
123
                    throw new Exceptions\Parser("Missing 'fieldDefinitionIdentifier' element in field data for UserUpdate.");
124
                }
125
126
                if (!array_key_exists('fieldValue', $fieldData)) {
127
                    throw new Exceptions\Parser("Missing 'fieldValue' element for '{$fieldData['fieldDefinitionIdentifier']}' identifier in UserUpdate.");
128
                }
129
130
                $fieldValue = $this->fieldTypeParser->parseFieldValue($userId, $fieldData['fieldDefinitionIdentifier'], $fieldData['fieldValue']);
131
132
                $languageCode = null;
133
                if (array_key_exists('languageCode', $fieldData)) {
134
                    $languageCode = $fieldData['languageCode'];
135
                }
136
137
                $parsedData['fields'][$fieldData['fieldDefinitionIdentifier']] = array(
138
                    'fieldValue' => $fieldValue,
139
                    'languageCode' => $languageCode,
140
                );
141
            }
142
        }
143
144
        $userUpdateStruct = $this->userService->newUserUpdateStruct();
145
146
        if (!empty($parsedData)) {
147
            if (array_key_exists('email', $parsedData)) {
148
                $userUpdateStruct->email = $parsedData['email'];
149
            }
150
151
            if (array_key_exists('password', $parsedData)) {
152
                $userUpdateStruct->password = $parsedData['password'];
153
            }
154
155
            if (array_key_exists('enabled', $parsedData)) {
156
                $userUpdateStruct->enabled = $parsedData['enabled'];
157
            }
158
159 View Code Duplication
            if (array_key_exists('mainLanguageCode', $parsedData) || array_key_exists('remoteId', $parsedData)) {
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...
160
                $userUpdateStruct->contentMetadataUpdateStruct = $this->contentService->newContentMetadataUpdateStruct();
161
162
                if (array_key_exists('mainLanguageCode', $parsedData)) {
163
                    $userUpdateStruct->contentMetadataUpdateStruct->mainLanguageCode = $parsedData['mainLanguageCode'];
164
                }
165
166
                if (array_key_exists('remoteId', $parsedData)) {
167
                    $userUpdateStruct->contentMetadataUpdateStruct->remoteId = $parsedData['remoteId'];
168
                }
169
            }
170
171 View Code Duplication
            if (array_key_exists('fields', $parsedData)) {
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...
172
                $userUpdateStruct->contentUpdateStruct = $this->contentService->newContentUpdateStruct();
173
174
                foreach ($parsedData['fields'] as $fieldDefinitionIdentifier => $fieldValue) {
175
                    $userUpdateStruct->contentUpdateStruct->setField(
176
                        $fieldDefinitionIdentifier,
177
                        $fieldValue['fieldValue'],
178
                        $fieldValue['languageCode']
179
                    );
180
                }
181
            }
182
        }
183
184
        return new RestUserUpdateStruct(
0 ignored issues
show
Bug Best Practice introduced by
The return type of return new \eZ\Publish\C...a['sectionId'] : null); (eZ\Publish\Core\REST\Ser...es\RestUserUpdateStruct) is incompatible with the return type declared by the abstract method eZ\Publish\Core\REST\Common\Input\Parser::parse of type eZ\Publish\API\Repository\Values\ValueObject.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
185
            $userUpdateStruct,
186
            array_key_exists('sectionId', $parsedData) ? $parsedData['sectionId'] : null
187
        );
188
    }
189
}
190