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

LanguageMapper   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 68
Duplicated Lines 11.76 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 8
c 2
b 0
f 0
lcom 1
cbo 2
dl 8
loc 68
ccs 30
cts 30
cp 1
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A languageToObject() 0 8 1
A mapObjectToCreateStruct() 0 13 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\Language;
12
use eZ\Publish\API\Repository\Values\Content\LanguageCreateStruct;
13
use Transfer\EzPlatform\Repository\Values\LanguageObject;
14
15
/**
16
 * Language mapper.
17
 *
18
 * @author Harald Tollefsen <[email protected]>
19
 */
20
class LanguageMapper
21
{
22
    /**
23
     * @var LanguageObject
24
     */
25
    public $languageObject;
26
27
    /**
28
     * @param LanguageObject $languageObject
29
     */
30 39
    public function __construct(LanguageObject $languageObject)
31
    {
32 39
        $this->languageObject = $languageObject;
33 39
    }
34
35
    /**
36
     * @param Language $language
37
     */
38 39
    public function languageToObject(Language $language)
39
    {
40 39
        $this->languageObject->data['code'] = $language->languageCode;
41 39
        $this->languageObject->data['name'] = $language->name;
42 39
        $this->languageObject->data['enabled'] = $language->enabled;
43
44 39
        $this->languageObject->setProperty('id', $language->id);
45 39
    }
46
47
    /**
48
     * @param LanguageCreateStruct $createStruct
49
     */
50 2
    public function mapObjectToCreateStruct(LanguageCreateStruct $createStruct)
51
    {
52
        // Name collection (ez => transfer)
53
        $keys = array(
54 2
            'enabled' => 'enabled',
55 2
            'languageCode' => 'code',
56 2
            'name' => 'name',
57 2
        );
58
59 2
        $this->arrayToStruct($createStruct, $keys);
60
61 2
        $this->callStruct($createStruct);
62 2
    }
63
64
    /**
65
     * @param LanguageCreateStruct $struct
66
     * @param array                $keys
67
     */
68 2 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...
69
    {
70 2
        foreach ($keys as $ezKey => $transferKey) {
71 2
            if (isset($this->languageObject->data[$transferKey])) {
72 2
                $struct->$ezKey = $this->languageObject->data[$transferKey];
73 2
            }
74 2
        }
75 2
    }
76
77
    /**
78
     * @param LanguageCreateStruct $struct
79
     */
80 2
    private function callStruct($struct)
81
    {
82 2
        if ($this->languageObject->getProperty('struct_callback')) {
83 1
            $callback = $this->languageObject->getProperty('struct_callback');
84 1
            $callback($struct);
85 1
        }
86 2
    }
87
}
88