UserGroupMapper   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 111
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 15
c 2
b 0
f 0
lcom 1
cbo 7
dl 0
loc 111
ccs 54
cts 54
cp 1
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A userGroupToObject() 0 13 2
A mapObjectToCreateStruct() 0 13 1
A mapObjectToUpdateStruct() 0 13 1
A assignStructFieldValues() 0 10 3
B arrayToStruct() 0 15 5
A callStruct() 0 7 2
1
<?php
2
3
/**
4
 * This file is part of Transfer.
5
 *
6
 * For the full copyright and license information, please view the LICENSE file located
7
 * in the root directory.
8
 */
9
10
namespace Transfer\EzPlatform\Repository\Values\Mapper;
11
12
use eZ\Publish\API\Repository\Values\Content\ContentMetadataUpdateStruct;
13
use eZ\Publish\API\Repository\Values\User\UserGroup;
14
use eZ\Publish\API\Repository\Values\User\UserGroupCreateStruct;
15
use eZ\Publish\API\Repository\Values\User\UserGroupUpdateStruct;
16
use Transfer\EzPlatform\Repository\Values\UserGroupObject;
17
18
/**
19
 * Usergroup mapper.
20
 *
21
 * @author Harald Tollefsen <[email protected]>
22
 */
23
class UserGroupMapper
24
{
25
    /**
26
     * @var UserGroupObject
27
     */
28
    public $userGroupObject;
29
30
    /**
31
     * @param UserGroupObject $userGroupObject
32 20
     */
33
    public function __construct(UserGroupObject $userGroupObject)
34 20
    {
35 20
        $this->userGroupObject = $userGroupObject;
36
    }
37
38
    /**
39
     * @param UserGroup $userGroup
40 20
     */
41
    public function userGroupToObject(UserGroup $userGroup)
42 20
    {
43
        $this->userGroupObject->data['parent_id'] = $userGroup->parentId;
44 20
45 20
        $this->userGroupObject->data['fields'] = [];
46 20
        foreach ($userGroup->getFields() as $field) {
47 20
            $this->userGroupObject->data['fields'][$field->fieldDefIdentifier] = $field->value->text;
48
        }
49 20
50 20
        $this->userGroupObject->setProperty('id', $userGroup->contentInfo->id);
51 20
        $this->userGroupObject->setProperty('content_info', $userGroup->contentInfo);
52 20
        $this->userGroupObject->setProperty('version_info', $userGroup->versionInfo);
53
    }
54
55
    /**
56
     * @param UserGroupCreateStruct $createStruct
57 20
     */
58
    public function mapObjectToCreateStruct(UserGroupCreateStruct $createStruct)
59
    {
60
        // Name collection (ez => transfer)
61 20
        $keys = array(
62 20
            'remoteId' => 'remote_id',
63
        );
64 20
65
        $this->arrayToStruct($createStruct, $keys);
66 20
67
        $this->assignStructFieldValues($createStruct);
68 20
69 20
        $this->callStruct($createStruct);
70
    }
71
72
    /**
73
     * @param UserGroupUpdateStruct $updateStruct
74 3
     */
75
    public function mapObjectToUpdateStruct(UserGroupUpdateStruct $updateStruct)
76
    {
77
        // Name collection (ez => transfer)
78 3
        $keys = array(
79 3
            'remoteId' => 'remote_id',
80
        );
81 3
82
        $this->arrayToStruct($updateStruct, $keys);
83 3
84
        $this->assignStructFieldValues($updateStruct);
0 ignored issues
show
Documentation introduced by
$updateStruct is of type object<eZ\Publish\API\Re...\UserGroupUpdateStruct>, but the function expects a object<eZ\Publish\API\Re...\UserGroupCreateStruct>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
85 3
86 3
        $this->callStruct($updateStruct);
87
    }
88
89
    /**
90
     * @param UserGroupCreateStruct $struct Struct to assign values to
91 20
     */
92
    private function assignStructFieldValues($struct)
93 20
    {
94 20
        foreach ($this->userGroupObject->data['fields'] as $key => $value) {
95 3
            if ($struct instanceof UserGroupUpdateStruct) {
96 3
                $struct->contentUpdateStruct->setField($key, $value);
97 20
            } else {
98
                $struct->setField($key, $value);
99 20
            }
100 20
        }
101
    }
102
103
    /**
104
     * @param UserGroupCreateStruct|UserGroupUpdateStruct $struct
105
     * @param array                                       $keys
106 20
     */
107
    private function arrayToStruct($struct, $keys)
108 20
    {
109 20
        foreach ($keys as $ezKey => $transferKey) {
110 3
            if (isset($this->userGroupObject->data[$transferKey])) {
111 2
                if ($struct instanceof UserGroupUpdateStruct) {
112 2
                    if (!$struct->contentMetadataUpdateStruct) {
113 2
                        $struct->contentMetadataUpdateStruct = new ContentMetadataUpdateStruct();
114 2
                    }
115 2
                    $struct->contentMetadataUpdateStruct->$ezKey = $this->userGroupObject->data[$transferKey];
116 3
                } else {
117
                    $struct->$ezKey = $this->userGroupObject->data[$transferKey];
118 3
                }
119 20
            }
120 20
        }
121
    }
122
123
    /**
124
     * @param UserGroupCreateStruct|UserGroupUpdateStruct $struct
125 20
     */
126
    private function callStruct($struct)
127 20
    {
128 1
        if ($this->userGroupObject->getProperty('struct_callback')) {
129 1
            $callback = $this->userGroupObject->getProperty('struct_callback');
130 1
            $callback($struct);
131 20
        }
132
    }
133
}
134