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

LanguageMapper::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\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