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

UserUpdate::parse()   F

Complexity

Conditions 28
Paths 10576

Size

Total Lines 110

Duplication

Lines 50
Ratio 45.45 %

Importance

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