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

UserGroupUpdate   A

Complexity

Total Complexity 23

Size/Duplication

Total Lines 141
Duplicated Lines 35.46 %

Coupling/Cohesion

Components 1
Dependencies 12

Importance

Changes 0
Metric Value
dl 50
loc 141
rs 10
c 0
b 0
f 0
wmc 23
lcom 1
cbo 12

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
F parse() 50 86 22

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
/**
4
 * File containing the UserGroupUpdate 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\Exceptions;
15
use eZ\Publish\Core\REST\Server\Values\RestUserGroupUpdateStruct;
16
use eZ\Publish\API\Repository\UserService;
17
use eZ\Publish\API\Repository\ContentService;
18
use eZ\Publish\API\Repository\LocationService;
19
20
/**
21
 * Parser for UserGroupUpdate.
22
 */
23
class UserGroupUpdate 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
     * Location service.
41
     *
42
     * @var \eZ\Publish\API\Repository\LocationService
43
     */
44
    protected $locationService;
45
46
    /**
47
     * FieldType parser.
48
     *
49
     * @var \eZ\Publish\Core\REST\Common\Input\FieldTypeParser
50
     */
51
    protected $fieldTypeParser;
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\API\Repository\LocationService $locationService
59
     * @param \eZ\Publish\Core\REST\Common\Input\FieldTypeParser $fieldTypeParser
60
     */
61
    public function __construct(UserService $userService, ContentService $contentService, LocationService $locationService, FieldTypeParser $fieldTypeParser)
62
    {
63
        $this->userService = $userService;
64
        $this->contentService = $contentService;
65
        $this->locationService = $locationService;
66
        $this->fieldTypeParser = $fieldTypeParser;
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\RestUserGroupUpdateStruct
76
     */
77
    public function parse(array $data, ParsingDispatcher $parsingDispatcher)
78
    {
79
        $parsedData = [];
80
81
        if (array_key_exists('mainLanguageCode', $data)) {
82
            $parsedData['mainLanguageCode'] = $data['mainLanguageCode'];
83
        }
84
85 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...
86
            if (!array_key_exists('_href', $data['Section'])) {
87
                throw new Exceptions\Parser("Missing '_href' attribute for Section element in UserGroupUpdate.");
88
            }
89
90
            $parsedData['sectionId'] = $this->requestParser->parseHref($data['Section']['_href'], 'sectionId');
91
        }
92
93
        if (array_key_exists('remoteId', $data)) {
94
            $parsedData['remoteId'] = $data['remoteId'];
95
        }
96
97
        if (array_key_exists('fields', $data)) {
98
            $groupLocationParts = explode('/', $this->requestParser->parseHref($data['__url'], 'groupPath'));
99
100
            $groupLocation = $this->locationService->loadLocation(array_pop($groupLocationParts));
101
102
            if (!is_array($data['fields']) || !array_key_exists('field', $data['fields']) || !is_array($data['fields']['field'])) {
103
                throw new Exceptions\Parser("Invalid 'fields' element for UserGroupUpdate.");
104
            }
105
106
            $parsedData['fields'] = [];
107 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...
108
                if (!array_key_exists('fieldDefinitionIdentifier', $fieldData)) {
109
                    throw new Exceptions\Parser("Missing 'fieldDefinitionIdentifier' element in field data for UserGroupUpdate.");
110
                }
111
112
                if (!array_key_exists('fieldValue', $fieldData)) {
113
                    throw new Exceptions\Parser("Missing 'fieldValue' element for '{$fieldData['fieldDefinitionIdentifier']}' identifier in UserGroupUpdate.");
114
                }
115
116
                $fieldValue = $this->fieldTypeParser->parseFieldValue($groupLocation->contentId, $fieldData['fieldDefinitionIdentifier'], $fieldData['fieldValue']);
117
118
                $languageCode = null;
119
                if (array_key_exists('languageCode', $fieldData)) {
120
                    $languageCode = $fieldData['languageCode'];
121
                }
122
123
                $parsedData['fields'][$fieldData['fieldDefinitionIdentifier']] = [
124
                    'fieldValue' => $fieldValue,
125
                    'languageCode' => $languageCode,
126
                ];
127
            }
128
        }
129
130
        $userGroupUpdateStruct = $this->userService->newUserGroupUpdateStruct();
131
132
        if (!empty($parsedData)) {
133 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...
134
                $userGroupUpdateStruct->contentMetadataUpdateStruct = $this->contentService->newContentMetadataUpdateStruct();
135
136
                if (array_key_exists('mainLanguageCode', $parsedData)) {
137
                    $userGroupUpdateStruct->contentMetadataUpdateStruct->mainLanguageCode = $parsedData['mainLanguageCode'];
138
                }
139
140
                if (array_key_exists('remoteId', $parsedData)) {
141
                    $userGroupUpdateStruct->contentMetadataUpdateStruct->remoteId = $parsedData['remoteId'];
142
                }
143
            }
144
145 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...
146
                $userGroupUpdateStruct->contentUpdateStruct = $this->contentService->newContentUpdateStruct();
147
148
                foreach ($parsedData['fields'] as $fieldDefinitionIdentifier => $fieldValue) {
149
                    $userGroupUpdateStruct->contentUpdateStruct->setField(
150
                        $fieldDefinitionIdentifier,
151
                        $fieldValue['fieldValue'],
152
                        $fieldValue['languageCode']
153
                    );
154
                }
155
            }
156
        }
157
158
        return new RestUserGroupUpdateStruct(
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...stUserGroupUpdateStruct) 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...
159
            $userGroupUpdateStruct,
160
            array_key_exists('sectionId', $parsedData) ? $parsedData['sectionId'] : null
161
        );
162
    }
163
}
164