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

LocationMapper   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 94
Duplicated Lines 39.36 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 3
Bugs 0 Features 1
Metric Value
wmc 9
c 3
b 0
f 1
lcom 1
cbo 2
dl 37
loc 94
ccs 50
cts 50
cp 1
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A locationToObject() 0 17 1
A mapObjectToCreateStruct() 15 15 1
A mapObjectToUpdateStruct() 14 14 1
A arrayToStruct() 8 8 3
A callStruct() 0 7 2

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
 * 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