Completed
Push — 1.0 ( fb098d...df18cb )
by Valentin
03:59
created

UserGroupMapper::populateUserGroupCreateStruct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 9
Ratio 100 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 9
loc 9
ccs 7
cts 7
cp 1
rs 9.6666
cc 2
eloc 5
nc 2
nop 1
crap 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
namespace Transfer\EzPlatform\Repository\Values\Mapper;
10
11
use eZ\Publish\API\Repository\Values\Content\ContentMetadataUpdateStruct;
12
use eZ\Publish\API\Repository\Values\User\UserGroup;
13
use eZ\Publish\API\Repository\Values\User\UserGroupCreateStruct;
14
use eZ\Publish\API\Repository\Values\User\UserGroupUpdateStruct;
15
use Transfer\EzPlatform\Repository\Values\UserGroupObject;
16
17
/**
18
 * Usergroup mapper.
19
 *
20
 * @author Harald Tollefsen <[email protected]>
21
 */
22
class UserGroupMapper
23
{
24
    /**
25
     * @var UserGroupObject
26
     */
27
    public $userGroupObject;
28
29
    /**
30
     * @param UserGroupObject $userGroupObject
31
     */
32 18
    public function __construct(UserGroupObject $userGroupObject)
33
    {
34 18
        $this->userGroupObject = $userGroupObject;
35 18
    }
36
37
    /**
38
     * @param UserGroup $userGroup
39
     */
40 18
    public function userGroupToObject(UserGroup $userGroup)
41
    {
42 18
        $this->userGroupObject->data['parent_id'] = $userGroup->parentId;
43
44 18
        $this->userGroupObject->data['fields'] = [];
45 18
        foreach ($userGroup->getFields() as $field) {
46 18
            $this->userGroupObject->data['fields'][$field->fieldDefIdentifier] = $field->value->text;
47 18
        }
48
49 18
        $this->userGroupObject->setProperty('id', $userGroup->contentInfo->id);
50 18
        $this->userGroupObject->setProperty('content_info', $userGroup->contentInfo);
51 18
        $this->userGroupObject->setProperty('version_info', $userGroup->versionInfo);
52 18
    }
53
54
    /**
55
     * @param UserGroupCreateStruct $createStruct
56
     */
57 18 View Code Duplication
    public function mapObjectToCreateStruct(UserGroupCreateStruct $createStruct)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
58
    {
59
        // Name collection (ez => transfer)
60
        $keys = array(
61 18
            'remoteId' => 'remote_id',
62 18
        );
63
64 18
        $this->arrayToStruct($createStruct, $keys);
65
66 18
        $this->assignStructFieldValues($createStruct);
67
68 18
        $this->callStruct($createStruct);
69 18
    }
70
71
    /**
72
     * @param UserGroupUpdateStruct $updateStruct
73
     */
74 3 View Code Duplication
    public function mapObjectToUpdateStruct(UserGroupUpdateStruct $updateStruct)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
75
    {
76
        // Name collection (ez => transfer)
77
        $keys = array(
78 3
            'remoteId' => 'remote_id',
79 3
        );
80
81 3
        $this->arrayToStruct($updateStruct, $keys);
82
83 3
        $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...
84
85 3
        $this->callStruct($updateStruct);
86 3
    }
87
88
    /**
89
     * @param UserGroupCreateStruct $struct Struct to assign values to
90
     */
91 18 View Code Duplication
    private function assignStructFieldValues($struct)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
92
    {
93 18
        foreach ($this->userGroupObject->data['fields'] as $key => $value) {
94 18
            if ($struct instanceof UserGroupUpdateStruct) {
95 3
                $struct->contentUpdateStruct->setField($key, $value);
96 3
            } else {
97 18
                $struct->setField($key, $value);
98
            }
99 18
        }
100 18
    }
101
102
    /**
103
     * @param UserGroupCreateStruct|UserGroupUpdateStruct $struct
104
     * @param array                                       $keys
105
     */
106 18
    private function arrayToStruct($struct, $keys)
107
    {
108 18
        foreach ($keys as $ezKey => $transferKey) {
109 18
            if (isset($this->userGroupObject->data[$transferKey])) {
110 3
                if ($struct instanceof UserGroupUpdateStruct) {
111 2
                    if (!$struct->contentMetadataUpdateStruct) {
112 2
                        $struct->contentMetadataUpdateStruct = new ContentMetadataUpdateStruct();
113 2
                    }
114 2
                    $struct->contentMetadataUpdateStruct->$ezKey = $this->userGroupObject->data[$transferKey];
115 2
                } else {
116 3
                    $struct->$ezKey = $this->userGroupObject->data[$transferKey];
117
                }
118 3
            }
119 18
        }
120 18
    }
121
122
    /**
123
     * @param UserGroupCreateStruct|UserGroupUpdateStruct $struct
124
     */
125 18
    private function callStruct($struct)
126
    {
127 18
        if ($this->userGroupObject->getProperty('struct_callback')) {
128 1
            $callback = $this->userGroupObject->getProperty('struct_callback');
129 1
            $callback($struct);
130 1
        }
131 18
    }
132
}
133