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

LocationMapper::arrayToStruct()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 8
Code Lines 4

Duplication

Lines 8
Ratio 100 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 8
loc 8
ccs 7
cts 7
cp 1
rs 9.4285
cc 3
eloc 4
nc 3
nop 2
crap 3
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\Location;
12
use eZ\Publish\API\Repository\Values\Content\LocationCreateStruct;
13
use eZ\Publish\API\Repository\Values\Content\LocationUpdateStruct;
14
use Transfer\EzPlatform\Repository\Values\LocationObject;
15
16
/**
17
 * User mapper.
18
 *
19
 * @author Harald Tollefsen <[email protected]>
20
 */
21
class LocationMapper
22
{
23
    /**
24
     * @var LocationObject
25
     */
26
    public $locationObject;
27
28
    /**
29
     * @param LocationObject $locationObject
30
     */
31 15
    public function __construct(LocationObject $locationObject)
32
    {
33 15
        $this->locationObject = $locationObject;
34 15
    }
35
36 14
    public function locationToObject(Location $location)
37
    {
38 14
        $this->locationObject->data['content_id'] = $location->contentId;
39 14
        $this->locationObject->data['remote_id'] = $location->remoteId;
40 14
        $this->locationObject->data['parent_location_id'] = $location->parentLocationId;
41 14
        $this->locationObject->data['hidden'] = $location->hidden;
42 14
        $this->locationObject->data['priority'] = $location->priority;
43 14
        $this->locationObject->data['sort_field'] = $location->sortField;
44 14
        $this->locationObject->data['sort_order'] = $location->sortOrder;
45
46 14
        $this->locationObject->setProperty('id', $location->id);
47 14
        $this->locationObject->setProperty('depth', $location->depth);
48 14
        $this->locationObject->setProperty('content_info', $location->contentInfo);
49 14
        $this->locationObject->setProperty('invisible', $location->invisible);
50 14
        $this->locationObject->setProperty('path', $location->path);
51 14
        $this->locationObject->setProperty('path_string', $location->pathString);
52 14
    }
53
54
    /**
55
     * @param LocationCreateStruct $createStruct
56
     */
57 7 View Code Duplication
    public function mapObjectToCreateStruct(LocationCreateStruct $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 7
            'remoteId' => 'remote_id',
62 7
            'hidden' => 'hidden',
63 7
            'priority' => 'priority',
64 7
            'sortField' => 'sort_field',
65 7
            'sortOrder' => 'sort_order',
66 7
        );
67
68 7
        $this->arrayToStruct($createStruct, $keys);
69
70 7
        $this->callStruct($createStruct);
71 7
    }
72
73
    /**
74
     * @param LocationUpdateStruct $updateStruct
75
     */
76 13 View Code Duplication
    public function mapObjectToUpdateStruct(LocationUpdateStruct $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...
77
    {
78
        // Name collection (ez => transfer)
79
        $keys = array(
80 13
            'remoteId' => 'remote_id',
81 13
            'priority' => 'priority',
82 13
            'sortField' => 'sort_field',
83 13
            'sortOrder' => 'sort_order',
84 13
        );
85
86 13
        $this->arrayToStruct($updateStruct, $keys);
87
88 13
        $this->callStruct($updateStruct);
89 13
    }
90
91
    /**
92
     * @param LocationCreateStruct|LocationUpdateStruct $struct
93
     * @param array                                     $keys
94
     */
95 15 View Code Duplication
    private function arrayToStruct($struct, $keys)
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...
96
    {
97 15
        foreach ($keys as $ezKey => $transferKey) {
98 15
            if (isset($this->locationObject->data[$transferKey])) {
99 12
                $struct->$ezKey = $this->locationObject->data[$transferKey];
100 12
            }
101 15
        }
102 15
    }
103
104
    /**
105
     * @param LocationCreateStruct|LocationUpdateStruct $struct
106
     */
107 15
    private function callStruct($struct)
108
    {
109 15
        if ($this->locationObject->getProperty('struct_callback')) {
110 1
            $callback = $this->locationObject->getProperty('struct_callback');
111 1
            $callback($struct);
112 1
        }
113 15
    }
114
}
115