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

FieldDefinitionMapper   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 77
Duplicated Lines 63.64 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 6
c 2
b 0
f 0
lcom 1
cbo 1
dl 49
loc 77
ccs 43
cts 43
cp 1
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A mapObjectToCreateStruct() 21 21 1
A mapObjectToUpdateStruct() 20 20 1
A arrayToStruct() 8 8 3

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\ContentType\FieldDefinitionCreateStruct;
12
use eZ\Publish\API\Repository\Values\ContentType\FieldDefinitionUpdateStruct;
13
use Transfer\EzPlatform\Repository\Values\FieldDefinitionObject;
14
15
/**
16
 * Field definition mapper.
17
 *
18
 * @internal
19
 *
20
 * @author Harald Tollefsen <[email protected]>
21
 */
22
class FieldDefinitionMapper
23
{
24
    /**
25
     * @var FieldDefinitionObject
26
     */
27
    public $fieldDefinitionObject;
28
29
    /**
30
     * @param FieldDefinitionObject $fieldDefinitionObject
31
     */
32 36
    public function __construct(FieldDefinitionObject $fieldDefinitionObject)
33
    {
34 36
        $this->fieldDefinitionObject = $fieldDefinitionObject;
35 36
    }
36
37
    /**
38
     * @param FieldDefinitionCreateStruct $createStruct
39
     */
40 4 View Code Duplication
    public function mapObjectToCreateStruct(FieldDefinitionCreateStruct $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...
41
    {
42
        // Name collection (ez => transfer)
43
        $keys = array(
44 4
            'names' => 'names',
45 4
            'descriptions' => 'descriptions',
46 4
            'fieldGroup' => 'field_group',
47 4
            'position' => 'position',
48 4
            'isTranslatable' => 'is_translatable',
49 4
            'isRequired' => 'is_required',
50 4
            'isInfoCollector' => 'is_info_collector',
51 4
            'isSearchable' => 'is_searchable',
52 4
            'fieldSettings' => 'field_settings',
53 4
            'defaultValue' => 'default_value',
54 4
            'identifier' => 'identifier',
55 4
            'validatorConfiguration' => 'validator_configuration',
56 4
            'fieldTypeIdentifier' => 'type',
57 4
        );
58
59 4
        $this->arrayToStruct($createStruct, $keys);
60 4
    }
61
62
    /**
63
     * @param FieldDefinitionUpdateStruct $updateStruct
64
     */
65 33 View Code Duplication
    public function mapObjectToUpdateStruct(FieldDefinitionUpdateStruct $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...
66
    {
67
        // Name collection (ez => transfer)
68
        $keys = array(
69 33
            'names' => 'names',
70 33
            'descriptions' => 'descriptions',
71 33
            'fieldGroup' => 'field_group',
72 33
            'position' => 'position',
73 33
            'isTranslatable' => 'is_translatable',
74 33
            'isRequired' => 'is_required',
75 33
            'isInfoCollector' => 'is_info_collector',
76 33
            'isSearchable' => 'is_searchable',
77 33
            'fieldSettings' => 'field_settings',
78 33
            'defaultValue' => 'default_value',
79 33
            'identifier' => 'identifier',
80 33
            'validatorConfiguration' => 'validator_configuration',
81 33
        );
82
83 33
        $this->arrayToStruct($updateStruct, $keys);
84 33
    }
85
86
    /**
87
     * @param FieldDefinitionCreateStruct|FieldDefinitionUpdateStruct $struct
88
     * @param array                                                   $keys
89
     */
90 36 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...
91
    {
92 36
        foreach ($keys as $ezKey => $transferKey) {
93 36
            if (isset($this->fieldDefinitionObject->data[$transferKey])) {
94 36
                $struct->$ezKey = $this->fieldDefinitionObject->data[$transferKey];
95 36
            }
96 36
        }
97 36
    }
98
}
99